Quote:
Originally Posted by tushar_pandey
#ifndef datest
#define datest
int i ;
void loadline();
#endif
can you please tell me the meanings of 3 bolded lines
|
The
purpose of those three lines is to make the compiler process the rest of the header file only once, even if it is included more than once.
In large projects, it is very common for header file x to define things needed by each of header files y and z, then some .c files use y and others use z and others use both.
It is messy to make inclusion of x the responsibility of each .c file that includes y or z. In well structured programming, the fact that y or z needs x should be a private characteristic of y or z and should not need to be known by the clients of y or z.
So instead, it is best for header y to include header x and for header z to include header x. But if some .c file includes both y and z, we want x processed only once (by whichever included it first).
Since that situation is so common in large projects, it is good practice to have the include guards (the three lines you asked about) in every header file.
Quote:
Originally Posted by Nylex
http://www.cplusplus.com/doc/tutorial/preprocessor/.
|
You could find the
meaning of each of those three lines there. But unless I missed something on that page, I don't see anything there that would help with a beginner's confusion about what those three lines are doing in the header file. So I chose to explain their purpose, rather than answer the literal question about their meaning.
Quote:
Originally Posted by tushar_pandey
why this ?
#include<stdio.h>
int i ; // default :: 0
int main()
{
int i ; // garbage
}
|
"Why" is a tricky question in issues of programming language standards. The only real answer is because those defining the language decided it should be that way.
In the typical implementation, the second i you declared would be allocated on the stack on entry to the function. In general (ignoring the fact that this particular function is "main") a function can get entered many times during execution of the program and extra work (such as zeroing the uninitialized variables) could add a lot to the program's run time. That decision is better left to the programmer (initialize the variable if it needs to be initialized).
But static or global (such as the first i you declared) are allocated once (generally at the start of the program and generally all together). Automatically zeroing a whole area of memory (where the compiler/linker grouped all the uninitialized static and global variables) is efficient to do once at the start of the program. For security reasons, the OS needs to zero memory when memory that was used by one process is given to another process, so when a C program is compiled to run under an OS that does that, it costs nothing extra to zero all the uninitialized static and global variables. By contrast, stack memory is reused within the process (rather than from another process) so the OS is not involved when stack space is reused, so there is no reason to zero that memory on reuse.