how to create header files in c/c++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bhandarisourabh
    New Member
    • Oct 2008
    • 1

    how to create header files in c/c++

    hi, i m sourabh....... Can any1 tell me how to create a header file in c/c++
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

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

    kind regards,

    Jos

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      The text file doesn't even need the .h you just need to #include the text file in your source file.

      Comment

      • Tassos Souris
        New Member
        • Aug 2008
        • 152

        #4
        Exactly what the above guys said...

        A typical example of a header file named Example.h is:
        Code:
        #ifndef EXAMPLE_H
        #define EXAMPLE_H
        
        /* make definitions, declarations here... */
        
        #endif /* EXAMPLE_H */
        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...

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          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.

          Comment

          • Tassos Souris
            New Member
            • Aug 2008
            • 152

            #6
            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:
            Code:
            fatal error C1004: unexpected end-of-file found
            and that is because the logical source does not end with a new-line.. something like that..

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

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

              Comment

              • dumparun
                New Member
                • Feb 2007
                • 26

                #8
                thats interesting to know..
                thanks ...

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  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:

                  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

                  Comment

                  • Tassos Souris
                    New Member
                    • Aug 2008
                    • 152

                    #10
                    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

                    Comment

                    • Banfa
                      Recognized Expert Expert
                      • Feb 2006
                      • 9067

                      #11
                      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.

                      Comment

                      Working...