Typically, header files are written similar to the below example to prevent this problem.
Up vote 1 down vote favorite share g+ share fb share tw.
Include #include int main () { printf ("hello world"); return 0; } when I compile this, the compiler doesn't give any warning/error for including stdio. H twice. Why is it so?
Aren't the functions scanf, printf etc. declared and defined twice now? Thanks, in advance c header-files link|improve this question edited Jan 21 '11 at 3:32Mark Tolonen12.8k2719 asked Jan 21 '11 at 3:24hue36619 73% accept rate.
Typically, header files are written similar to the below example to prevent this problem: #ifndef MYHEADER #define MYHEADER ... #endif Then, if included more than once, then 2nd instance skips the content.
2 To add to this answer, the convention here is known as an include guard. – chrisaycock Jan 21 '11 at 3:29 You can also you the keywords #pragma once at the top of the file if it is a newer compiler (C++ only too I think). – gamernb Jan 21 '11 at 3:30 thanks for the info.. – hue Jan 21 '11 at 3:31 #pragma once is some nonstandard extension.
It's not a valid way to prevent multiple inclusion. – R.. Jan 21 '11 at 4:11 #pragma once works just fine for this purpose on Microsoft compilers. It's debatable whether it's "optimized" and therefore preferable, but it's certainly "valid".
If you're using GCC, #ifndef is a better way to go. – Cody Gray Jan 21 '11 at 4:21.
In addition to the use of include guards, as pointed out by Mark Tolonen's answer, there is no problem with declaring a function more than once, as long as the declarations are compatible. This is perfectly fine: int foo(int, char *); int foo(int a, char *p); extern int foo(int x, char y); In fact, since every definition is also a declaration, whenever you "forward-declare" a function declared in the same file, you are declaring the function twice. What is not OK is to create multiple external definitions of a function; but well-written header files should not create external definitions - only declarations.
The (single) definition of the printf and scanf functions should be in an object file, which is linked with your program when it is built.
No, the header files usually define a flag and then use #ifndef to include themselves only if the flag was undefined. Open one up and see. PS: #include include 4 spaces at the beginning.
– Cody Gray Jan 21 '11 at 4:17 That was because you said typing with the makes them disappear. Indenting (on this forum) causes the indented lines to appear as code, and the won't disappear. – Satya Jan 21 '11 at 4:50.
As an aside, doing the "#ifndef" trick is appropriate for headers used by other people (like the standard headers). If you need the #ifndef for a "private" program, then you are doing it "wrong". That is, you can and should organize headers within a project so they are not included more than once.
One reason that this is helpful is that keeps headers you think you have deleted from popping up again. Since you can't control how public headers are used, this trick is reasonable for public headers. This trick is completely unnecessary for private headers.
2 -1. What nonsense. What could possibly be the downside of using header guards in a private project?
Why would you want to restrict your use of headers in this way? – Lightness Races in Orbit Jan 21 '11 at 23:14 "One reason that this is helpful is that keeps headers you think you have deleted from popping up again. " I have no idea how this could happen.
– DSimon Jan 21 '11 at 23:27 I had to clean up a bunch of libraries where the original authors thought the excluding header trick was a "great" idea. What it let them do is be sloppy about where they included the headers. That is, they included various headers all over the place.
In working on porting this set of libraries to other architectures, I would remove one header to have other calls to it become active. It was like "wack a mole"! – davep Jan 22 '11 at 14:57 you need better source management.
Consider grepping for the offending include statement. – Satya Jan 22 '11 at 17:03.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.