construction of source code

This is a discussion on construction of source code within the C Programming forums, part of the General Programming Boards category; Hi all! I've two n00b questions concerning the praxis of writing source code: 1. I've understood that the source files ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    61

    construction of source code

    Hi all!

    I've two n00b questions concerning the praxis of writing source code:

    1. I've understood that the source files can either have the file extension .h or .c, but when should I use which extension?

    2. I've got an idea for a game engine and want to use some kind of metadata publishing (XML for example) for doing the levels, characters etc. My question is: how to make the engine understand XML? or should I use any other form of metadata instead of XML? or perhaps I should use python or lua as scripting language -- but isn't that difficult? I want the program to be cross-platform.

    Thanks in advance for answers, ideas and comments!

  2. #2
    Registered User
    Join Date
    Feb 2010
    Posts
    36
    .h files are used to store the list of functions which can be used as a library.You can't compile these files.But,you can include these files in your .c file to use the functions in that file.

    .c files are used to do some operation using the functions in .h files.You can compile these files to create an executable.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,598
    Quote Originally Posted by sababa.sababa
    1. I've understood that the source files can either have the file extension .h or .c, but when should I use which extension?
    By convention, .h would be used to name a header file, whereas .c would be used to name a source file.

    Quote Originally Posted by sababa.sababa
    2. I've got an idea for a game engine and want to use some kind of metadata publishing (XML for example) for doing the levels, characters etc. My question is: how to make the engine understand XML?
    Write the code You probably would want to use an existing parser library though, e.g., Expat.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    61
    Now I've studied some source code, and it seems that there're only void functions in .c files (except the main function) and returning functions in .h files.

    Expat is what I searched for.

    Thanks a lot!

  5. #5
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,422
    there're only void functions in .c files
    bit hard to see what you are saying there, but you can certainly return data types from functions in c and cpp, maybe you mean you have only seen function prototypes?

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by sababa.sababa View Post
    Now I've studied some source code, and it seems that there're only void functions in .c files (except the main function) and returning functions in .h files.
    Well, you have that COMPLETELY WRONG.

    Normatively, a .c file contains function definitions (that is the functions themselves). .h files contain function declarations (aka prototypes), defines, and typedefs.

    example.h
    Code:
    int myXfunc(int x, char *ptr);
    example.c
    Code:
    int myXfunc(int x, char *ptr) {
         int i, len = strlen(ptr);
         for(i=0;i<len;i++) ptr[i] = 'A' + x;
         return len;
    }
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Sep 2009
    Posts
    61
    ...so a good rule of thumb would be to put the function definitions in the .c files and all other stuff in the .h files. But why should I declare the function before I define it? Isn't it enough with a definition?

  8. #8
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,422
    Headers and see section 'declaring functions' in functions(II)
    Last edited by rogster001; 03-12-2010 at 08:17 AM.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by sababa.sababa View Post
    But why should I declare the function before I define it? Isn't it enough with a definition?
    Generally speaking, no. When the compiler comes across a function name, it doesn't need to know right that minute how the function is defined, but it does need to know that the name represents a function. This is what your prototype does for you: tells the compiler "I have a function with this name, that takes these kinds of arguments, and returns this sort of thing."

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by sababa.sababa View Post
    ...so a good rule of thumb would be to put the function definitions in the .c files and all other stuff in the .h files. But why should I declare the function before I define it? Isn't it enough with a definition?
    You can get away without prototypes by defining all your functions before main() and placing them in the right order, but

    1) sometimes that is not possible; if func_b() can call func_a() AND func_a() can call func_b(), you need prototypes so that both functions are already declared when they are referenced in one another's definitions. Otherwise you get a compiler error, the same as just defining a function after main().
    2) it is less comprehensible

    Of course, that doesn't mean you actually need an .h file, it just means you should declare all your prototypes before main() or anything else is defined. The "secret purpose" of .h files is actually so you can compile .c files seperately, eg. on a large project. That's how the standard headers work -- they just contain prototypes referring to the functions already contained in precompiled libraries, so you don't have to recompile the entire C library all the time.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    Registered User
    Join Date
    Sep 2009
    Posts
    61
    Thanks a lot for all the answers and comments!

    MK27, your explanation is great.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seven Kingdoms I: Ancient Adversaries for Linux
    By MIH1406 in forum Projects and Job Recruitment
    Replies: 13
    Last Post: 01-17-2010, 04:03 PM
  2. How do you call another source code file?
    By nifear4 in forum C Programming
    Replies: 2
    Last Post: 10-28-2008, 12:16 PM
  3. DxEngine source code
    By Sang-drax in forum Game Programming
    Replies: 5
    Last Post: 06-26-2003, 05:50 PM
  4. Lines from Unix's source code have been copied into the heart of Linux????
    By zahid in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 05-19-2003, 03:50 PM
  5. Source Code Beautifier
    By Hammer in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 05-05-2002, 09:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21