我是新來的這個領域。 我正在嘗試閱讀一個示例程序。同名.h和.c文件以及這些文件之間的撤銷關係?
首先是team.c
#include "support.h"
struct team_t team = {
"", /* first member name
"", /* first member email
"", /* second member name
"" /* second member email
};
它包括support.h
,其是:
#ifndef SUPPORT_H__
#define SUPPORT_H__
/*
* Store information about the team who completed the assignment, to
* simplify the grading process. This is just a declaration. The definition
* is in team.c.
*/
extern struct team_t {
char *name1;
char *email1;
char *name2;
char *email2;
} team;
/*
* This function verifies that the team name is filled out
*/
void check_team(char *);
#endif // SUPPORT_H__
的check_team
功能是在support.c
:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "support.h"
/*
* Make sure that the student names and email fields are not empty.
*/
void check_team(char * progname) {
if ((strcmp("", team.name1) == 0) || (strcmp("", team.email1) == 0)) {
printf("%s: Please fill in the team struct in team.c\n",
progname);
exit(1);
}
printf("Student 1 : %s\n", team.name1);
printf("Email 1 : %s\n", team.email1);
printf("Student 2 : %s\n", team.name2);
printf("Email 2 : %s\n", team.email2);
printf("\n");
}
最後,在part1a。 c:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include "support.h"
int main(int argc, char **argv) {
check_team(argv[0]);
/*some other code*/
return 0;
}
我用makefile生成目標文件後。當我在終端運行. some forlder/part1a
,它運行良好,輸出內容爲team
我有兩個令人困惑的地方。 1.我對團隊的定義感到困惑,其價值在team.c
中給出,但在support.h
和extern
中定義的值用於再次獲取,程序運行時的順序是什麼? 2.如果support.h和support.c有任何其他關係,它們的名稱是否相同?
這些是通過閱讀C書或教程更好地解答的基本C問題。或者進行搜索,因爲這些基本概念已經處理過很多次了。 – kaylum