By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
462,067 Members | 616 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 462,067 IT Pros & Developers. It's quick & easy.

how to create header files in c/c++

P: 1
hi, i m sourabh....... Can any1 tell me how to create a header file in c/c++
Oct 4 '08 #1
Share this Question
Share on Google+
10 Replies

Expert 10K+
P: 11,448
Simply create a text file with the extension .h and voila.

kind regards,

Jos
Oct 4 '08 #2

weaknessforcats
Expert Mod 5K+
P: 9,197
The text file doesn't even need the .h you just need to #include the text file in your source file.
Oct 4 '08 #3

100+
P: 152
Exactly what the above guys said...

A typical example of a header file named Example.h is:
Expand|Select|Wrap|Line Numbers
  1. #ifndef EXAMPLE_H
  2. #define EXAMPLE_H
  3.  
  4. /* make definitions, declarations here... */
  5.  
  6. #endif /* EXAMPLE_H */
  7.  
Note that if you want to be strictly ANSI the above code is wrong!!! Visual Studio gives an error and gcc a warning. And the reason is the last comment...
Oct 5 '08 #4

weaknessforcats
Expert Mod 5K+
P: 9,197
Note that if you want to be strictly ANSI the above code is wrong!!! Visual Studio gives an error and gcc a warning. And the reason is the last comment...
I'll bite. My Visual Studio sees no error.
Oct 5 '08 #5

100+
P: 152
I'll bite. My Visual Studio sees no error.
Visual Studio gives an error as i said only with ANSI. You must put the /Za flag.

Specifically, the error is:
Expand|Select|Wrap|Line Numbers
  1. fatal error C1004: unexpected end-of-file found
  2.  
and that is because the logical source does not end with a new-line.. something like that..
Oct 5 '08 #6

weaknessforcats
Expert Mod 5K+
P: 9,197
Yes, the file must end with a null record. That is, enter-key only.
I don't know exactly what the ANSI standard says about this.
Oct 5 '08 #7

dumparun
P: 26
thats interesting to know..
thanks ...
Oct 6 '08 #8

Expert 10K+
P: 11,448
Yes, the file must end with a null record. That is, enter-key only.
I don't know exactly what the ANSI standard says about this.
The C99 Standard:

5.1.1.2. Environment

A source file that is not empty
shall end in a new-line character, which shall not be
immediately preceded by a backslash character before
any such splicing takes place.
By 'splicing' they mean the trailing backslash immediately followed by a new-line
character which concatenates two physical lines.

kind regards,

Jos
Oct 6 '08 #9

100+
P: 152
I actually thought this was a bug in Visual Studio 2008 and asked a friend to send a bug report; the answer we got amazed us!!! :-P
Oct 6 '08 #10

Banfa
Expert Mod 5K+
P: 8,968
I worked on a multi-platform project once where a new platform was introduced and its compiler produced an error where source files did not end in new-line. The compilers on the 3 existing platforms did not produce an error or even a warning in this case so lots of the code files did not end in a new-line.

Some lucky person (not me) got the job of adding a new-line to the end of all the (several 100) source files.
Oct 6 '08 #11

Post your reply

Sign in to post your reply or Sign up for a free account.