Connecting Tech Pros Worldwide Forums | Help | Site Map

how to create header files in c/c++

Newbie
 
Join Date: Oct 2008
Posts: 1
#1: Oct 4 '08
hi, i m sourabh....... Can any1 tell me how to create a header file in c/c++

JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#2: Oct 4 '08

re: how to create header files in c/c++


Simply create a text file with the extension .h and voila.

kind regards,

Jos
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,379
#3: Oct 4 '08

re: how to create header files in c/c++


The text file doesn't even need the .h you just need to #include the text file in your source file.
Member
 
Join Date: Aug 2008
Posts: 124
#4: Oct 5 '08

re: how to create header files in c/c++


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...
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,379
#5: Oct 5 '08

re: how to create header files in c/c++


Quote:

Originally Posted by Tassos Souris

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.
Member
 
Join Date: Aug 2008
Posts: 124
#6: Oct 5 '08

re: how to create header files in c/c++


Quote:

Originally Posted by weaknessforcats

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..
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,379
#7: Oct 6 '08

re: how to create header files in c/c++


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.
dumparun's Avatar
Newbie
 
Join Date: Feb 2007
Posts: 26
#8: Oct 6 '08

re: how to create header files in c/c++


thats interesting to know..
thanks ...
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#9: Oct 6 '08

re: how to create header files in c/c++


Quote:

Originally Posted by weaknessforcats

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:

Quote:

Originally Posted by C99

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
Member
 
Join Date: Aug 2008
Posts: 124
#10: Oct 6 '08

re: how to create header files in c/c++


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
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,195
#11: Oct 6 '08

re: how to create header files in c/c++


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.
Reply