What is the .c file for?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • iLL
    New Member
    • Oct 2006
    • 63

    What is the .c file for?

    I know, stupid question. But I’m in a data structure class, and it requires me to use an “accepted convention” of setting up a program. I have always set up my programs with the .cpp having the main, and the .h files having the classes and class’s member’s definitions. However, according to the convention, the .h file is not to have the definitions in them. So I’m guessing they go into the .c file. Is that true?

    If so, them I’m confused about something else. The convention also states that you are to include any .c file in any files. What!!!!!!!!! Then how are you suppose to use the damn functions.
  • iLL
    New Member
    • Oct 2006
    • 63

    #2
    The convention also states that you are to include any .c file in any files.

    should be

    The convention also states that you are NOT to include any .c file in any files.

    Comment

    • Remington
      New Member
      • Jan 2007
      • 20

      #3
      Im still a newbie programer, but I believe .c is a Language C file. Atleast, when I went through C, we put .c at the end of our files, and .cpp at the end of C++ files.

      *shrugs*

      Comment

      • ramudukamudu
        New Member
        • Jan 2007
        • 16

        #4
        Originally posted by Remington
        Im still a newbie programer, but I believe .c is a Language C file. Atleast, when I went through C, we put .c at the end of our files, and .cpp at the end of C++ files.

        *shrugs*
        Hi,
        In general practice nobody will keep function definitions in ".h" files. Definitions will be provided in ".c" files. You can spread the definitions over multiple ".c" files. During complilation time objects files(.o or .obj) are generated for each ".c" file ,same will be referred during linking time. So you need not include ".c" files in your main c file.
        Ex:

        a.h
        int sum(int,int);

        a.c
        #include "a.h"
        int sum(int a,int b)
        {
        return a+b;
        }

        b.c
        #include"a.h"
        void main()
        {
        printf("%d",sum (1,2);
        }


        Compilation
        gcc -c a.c -->[compilation] will generate a.o
        gcc -c b.c --> [compilation] will generate b.o
        gcc a.o b.o -o myApp -->[linking] will use a.o, b.o to generate executable myApp

        Comment

        • yogesh_anand
          New Member
          • Apr 2006
          • 26

          #5
          in general

          All the class declaration and func. declaration in .h files.

          All func. defination and func. calling in .c files.

          Comment

          Working...