s********1 发帖数: 581 | 1 请教有关header file的几个问题
假设sub.h 和 sub.c 是header file 和与其对应的source file.
(1) 在sub.c开始都有 #include "sub.h"
我试着delete 以上的语句,compile和run时都没有问题。#include "sub.h"
在sub.c中,#include "sub.h" 是否多余??
(2) 试着将sub.h的name 改成 subbbbb.h, 结果也没有问题,是否header file 和与其
对应的source file 名字可以完全不一样?
(3) 又试着去掉.h, 并把heade file 的名字改成:
sub.h ----〉 subbbbb.c
再把main program 中开头的部分改成 #include "subbbbb.c"。 结果也没有问题。
.c 和 .h file 到底有没有不同?
(4) 将#include "subbbbb.c" (或者#include "subbbbb.h", #include "sub.h")
|
c*****t 发帖数: 1879 | 2 You really should get a C/C++ book (such as K&R) and read it. This
board is not a good place for learning the extreme basics. It is a
place to ask questions after you get at least some basic understandings
of the languages.
p.s. It is rude and in violation the BBS rule to spam your post in
more than 3 boards.
其
题。
【在 s********1 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 请教有关header file的几个问题 : 假设sub.h 和 sub.c 是header file 和与其对应的source file. : (1) 在sub.c开始都有 #include "sub.h" : 我试着delete 以上的语句,compile和run时都没有问题。#include "sub.h" : 在sub.c中,#include "sub.h" 是否多余?? : (2) 试着将sub.h的name 改成 subbbbb.h, 结果也没有问题,是否header file 和与其 : 对应的source file 名字可以完全不一样? : (3) 又试着去掉.h, 并把heade file 的名字改成: : sub.h ----〉 subbbbb.c : 再把main program 中开头的部分改成 #include "subbbbb.c"。 结果也没有问题。
|
s********1 发帖数: 581 | 3 You should not respond to other people's question in this manner. You may
well ignore it if you are not interested. My question is carefully proposed.
I asked some engineers about it they were not sure either. It is not a spam
as you called it.
【在 c*****t 的大作中提到】![](/moin_static193/solenoid/img/up.png) : You really should get a C/C++ book (such as K&R) and read it. This : board is not a good place for learning the extreme basics. It is a : place to ask questions after you get at least some basic understandings : of the languages. : p.s. It is rude and in violation the BBS rule to spam your post in : more than 3 boards. : : 其 : 题。
|
h***z 发帖数: 233 | 4 Including a file A in another file B is equivalent to copying the contents
of A and pasting it in B. All included files are treated the same, no
matter what their names are.
其
题。
【在 s********1 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 请教有关header file的几个问题 : 假设sub.h 和 sub.c 是header file 和与其对应的source file. : (1) 在sub.c开始都有 #include "sub.h" : 我试着delete 以上的语句,compile和run时都没有问题。#include "sub.h" : 在sub.c中,#include "sub.h" 是否多余?? : (2) 试着将sub.h的name 改成 subbbbb.h, 结果也没有问题,是否header file 和与其 : 对应的source file 名字可以完全不一样? : (3) 又试着去掉.h, 并把heade file 的名字改成: : sub.h ----〉 subbbbb.c : 再把main program 中开头的部分改成 #include "subbbbb.c"。 结果也没有问题。
|
k**f 发帖数: 372 | 5 I think your questions are follow-ups to your earlier post.
Yes. sub.h is not necessary for sub.c to compile.
其
Yes.
题。
The extension of the header file can be anything. In fact, C++ standard
header files have no extension, e.g.
Now the real answer to your questions. Header files usually contain the
prototype of functions and definitions of data types that are shared among
source files.
Declaring function prototype is necessary in ANSI C. Using the same header
file allows the compiler
【在 s********1 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 请教有关header file的几个问题 : 假设sub.h 和 sub.c 是header file 和与其对应的source file. : (1) 在sub.c开始都有 #include "sub.h" : 我试着delete 以上的语句,compile和run时都没有问题。#include "sub.h" : 在sub.c中,#include "sub.h" 是否多余?? : (2) 试着将sub.h的name 改成 subbbbb.h, 结果也没有问题,是否header file 和与其 : 对应的source file 名字可以完全不一样? : (3) 又试着去掉.h, 并把heade file 的名字改成: : sub.h ----〉 subbbbb.c : 再把main program 中开头的部分改成 #include "subbbbb.c"。 结果也没有问题。
|
y****e 发帖数: 23939 | 6 其实他说得没错,你的这些问题确实非常那个基本,
名字没有关系的,有的平台上还把头文件叫 .hpp
你说的那些engineers是做C/C++的吗,如果是的话,他们都应该被fired
proposed.
spam
【在 s********1 的大作中提到】![](/moin_static193/solenoid/img/up.png) : You should not respond to other people's question in this manner. You may : well ignore it if you are not interested. My question is carefully proposed. : I asked some engineers about it they were not sure either. It is not a spam : as you called it.
|
O******e 发帖数: 734 | 7 Take coconut's advice, spend the money and get a C or C++ book. Given that
you know nothing about programming, any book with a lot of example would
do. Kernighan and Ritchie (suggested by coconut) is the standard reference
for all C programmers, but judging from the type of questions you have been
asking, it might not be suitable for you because it really is a reference
book with few examples and not a textbook with many illustrative examples.
Without a book, you are trying to learn how to pro
【在 s********1 的大作中提到】![](/moin_static193/solenoid/img/up.png) : You should not respond to other people's question in this manner. You may : well ignore it if you are not interested. My question is carefully proposed. : I asked some engineers about it they were not sure either. It is not a spam : as you called it.
|
O******e 发帖数: 734 | 8
If the contents of sub.h are not needed by sub.c, then #include "sub.h"
is redundant.
其
You can call the header file by any name you want.
题。
You can call the header file by any name you want, although some
preprocessors might make certain assumptions regarding the .h or .c
file extensions.
(4) 将#include "subbbbb.c" (或者#include "subbbbb.h", #include "sub.h")
完全从main program里除去, 只compile main program 和sub.c。
在linux 上compile,run 一切正常。 visual studio 2005 上也只在compile 时给
出
warning, run
【在 s********1 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 请教有关header file的几个问题 : 假设sub.h 和 sub.c 是header file 和与其对应的source file. : (1) 在sub.c开始都有 #include "sub.h" : 我试着delete 以上的语句,compile和run时都没有问题。#include "sub.h" : 在sub.c中,#include "sub.h" 是否多余?? : (2) 试着将sub.h的name 改成 subbbbb.h, 结果也没有问题,是否header file 和与其 : 对应的source file 名字可以完全不一样? : (3) 又试着去掉.h, 并把heade file 的名字改成: : sub.h ----〉 subbbbb.c : 再把main program 中开头的部分改成 #include "subbbbb.c"。 结果也没有问题。
|
t****t 发帖数: 6806 | 9 别人回你就不错了, 你还挑三拣四的
刚才有人说得不错, 你问的那些"engineers", 如果是C/C++的程序员, 那他们都该被炒
鱿鱼
proposed.
spam
【在 s********1 的大作中提到】![](/moin_static193/solenoid/img/up.png) : You should not respond to other people's question in this manner. You may : well ignore it if you are not interested. My question is carefully proposed. : I asked some engineers about it they were not sure either. It is not a spam : as you called it.
|