LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-29-2012, 12:55 AM   #1
tushar_pandey
Member
 
Registered: Jun 2012
Location: ghaziabad , delhi , india
Posts: 105

Rep: Reputation: Disabled
Header file in C


#ifndef datest
#define datest

int i ;
void loadline();

#endif

can you please tell me the meanings of 3 bolded lines
 
Old 07-29-2012, 02:08 AM   #2
tushar_pandey
Member
 
Registered: Jun 2012
Location: ghaziabad , delhi , india
Posts: 105

Original Poster
Rep: Reputation: Disabled
why this ?

#include<stdio.h>
int i ; // default :: 0
int main()
{
int i ; // garbage
}
 
Old 07-29-2012, 02:20 AM   #3
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Quote:
Originally Posted by tushar_pandey View Post
#ifndef datest
#define datest

int i ;
void loadline();

#endif

can you please tell me the meanings of 3 bolded lines
http://www.cplusplus.com/doc/tutorial/preprocessor/.

Quote:
Originally Posted by tushar_pandey View Post
why this ?

#include<stdio.h>
int i ; // default :: 0
int main()
{
int i ; // garbage
}
Separate threads for separate questions, please.
 
1 members found this post helpful.
Old 07-29-2012, 03:25 AM   #4
tushar_pandey
Member
 
Registered: Jun 2012
Location: ghaziabad , delhi , india
Posts: 105

Original Poster
Rep: Reputation: Disabled
sorry ! but i think why to waste two threads when only i have to ask two different questions !

& if it is against the forums than , really sorry !

Last edited by tushar_pandey; 07-29-2012 at 04:07 AM.
 
Old 07-29-2012, 05:57 AM   #5
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by tushar_pandey View Post
#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 View Post
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 View Post
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.

Last edited by johnsfine; 07-29-2012 at 06:18 AM.
 
Old 07-29-2012, 06:41 AM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Quote:
Originally Posted by tushar_pandey View Post
can you please tell me the meanings of 3 bolded lines
These line are meant to prevent warnings caused by multiple inclusions. Note: if the name of the file is datest.h, then symbol DATEST_H would be a better choice.

Your other question: it's by design. All you have to do is learning the rule: global variables are automatically initialized with zero, local variables aren't.
 
Old 07-31-2012, 02:22 AM   #7
kauuttt
Member
 
Registered: Dec 2008
Location: Atlanta, GA, USA
Distribution: Ubuntu
Posts: 135

Rep: Reputation: 26
1) For your 1st Question:
a) You create 2 C/C++ files and include a single header file (with the header guard - the 3 lines in bold) in both the C files. Compile both the C file together to create a single executable. See what warnings/errors you get?
b) Include the 3 lines and try it agian. See the behavior.

2) 2nd question - Its the characteristics of global and automatic variable.

The best way to answer all these questions of yours is - Code, compile and run!
 
Old 07-31-2012, 07:04 AM   #8
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by kauuttt View Post
a) You create 2 C/C++ files and include a single header file (with the header guard - the 3 lines in bold) in both the C files. Compile both the C file together to create a single executable. See what warnings/errors you get?
I think you have misunderstood the purpose of those three lines.

They are irrelevant in the situation that you described (include the same header in two different C files and link into one executable).

See my earlier post for a better explanation of the purpose of those three lines.

Edit: Or maybe you have an unusual meaning for "together". Normally telling the C compiler to compile two .c files causes it to compile them separately and then link the results together, in which case what I said above applies. It is possible to make the C compiler process two .c files "together" as if they had been concatenated into one .c file. In that case, the include guards would be relevant. But strange operations like that are not the intended purpose of the include guards.

Last edited by johnsfine; 07-31-2012 at 07:31 AM.
 
Old 07-31-2012, 11:47 PM   #9
kauuttt
Member
 
Registered: Dec 2008
Location: Atlanta, GA, USA
Distribution: Ubuntu
Posts: 135

Rep: Reputation: 26
@johnsfine:

Quote:
It is possible to make the C compiler process two .c files "together" as if they had been concatenated into one .c file. In that case, the include guards would be relevant.
Exactly I meant the above, but anyway thanks for a cleaner explanation.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to check missing header files included from another header file adisan82 Linux - Software 1 01-28-2011 03:57 AM
file header with timestamp groking Programming 4 02-25-2009 04:36 AM
header file saadiaomar Programming 4 03-23-2006 04:18 PM
Problem with c header file alaios Programming 4 09-03-2005 09:06 PM
Create header file for existing .c file hk_michael Programming 5 02-25-2005 05:26 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:11 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration