Thread: how difference between including the header file and source file in main file

  1. #1
    kotin
    Join Date
    Oct 2009
    Posts
    132

    how difference between including the header file and source file in main file

    Hi,

    i have two files. 1.c and 2.h like below


    2.h file

    Code:
    int fun()
    {
            printf ("hai how are you\n");
            return 0;
    }
    1.c file

    Code:
    #include<stdio.h>
    #include"2.h"
    int i;
    int j;
    int main()
    {
            int fun();
            printf ("%d\n",i);
            fun();
            printf ("%d\n",i);
    }
    if run the above program by using #cc 1.c , i should get the output as

    "0
    hai how are you
    0".

    if i rename the 2.h to 2.c and reflect the same change in include file on 1.c , then also i get the same output.


    actualy what is the correct procedure?

    in which condition i include .h file and in which condition i need to include .c file?

    i would appreciate any help.
    Last edited by nkrao123@gmail.; 09-12-2011 at 10:57 AM.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Header files should never have any executable code in them. They just define the interface to your .c files. A .h file should basically contain function prototypes and extern'ed variable declarations, and struct, enum and type definitions that are needed in other .c files. You should only #include a .h file, never a .c file. The .c file will be compiled with the other .c files to make the final executable. Fixing your example:

    1.c
    Code:
    #include <stdio.h>
    #include "2.h"
    int main(void)
    {
        int i = 0;  // globals are bad
        printf("%d\n", i);
        fun();
        printf("%d\n", i);
        return 0;
    }
    2.h
    Code:
    #ifndef include_guard_2_h
    #ifndef include_guard_2_h
    void fun(void);
    #ifndef include_guard_2_h
    2.c
    Code:
    void fun(void)
    {
        printf("hai how are you\n");
    }
    The bold code is things you need to fix:
    1. Global Variables Are Bad
    2. You said main would return an int, so make it do so
    3. You should always use include guards with your header files: Include guard - Wikipedia, the free encyclopedia
    4. There's no need for fun() to return int, since it has no significance (it's always 0), and you don't use the return value.
    5. Be explicit. If you don't want your function to take parameters, then put a void in the param list.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by anduril462
    You said main would return an int, so make it do so
    Note that anduril462's suggestion is unnecessary if you are compiling with respect to C99, but generally it is indeed better to be explicit than implicit.

    The include guard example should be:
    Code:
    #ifndef include_guard_2_h
    #define include_guard_2_h
    
    void fun(void);
    
    #endif
    As a common convention, macro names should be fully capitalised, so your actual include guard names should likewise be fully capitalised.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by laserlight View Post
    The include guard example should be:
    Doh! Got a little overzealous with the copy-paste. Thanks.

  5. #5
    kotin
    Join Date
    Oct 2009
    Posts
    132
    Hi All,

    Thanks for your explanations.

  6. #6
    kotin
    Join Date
    Oct 2009
    Posts
    132
    Hi ,

    I have one more small doubt,

    i have ran the below program

    //extern.c
    Code:
    int i;
    //main.c
    Code:
    #include<stdio.h>
    int mian()
    {
             printf("%d\n",i);
             return 0;
    }
    I am getting below error if i compile the program with even i including the all files as input to compiler

    #cc main.c extern.c

    # cc main.c extern.c
    main.c: In function `main':
    main.c:4: error: `i' undeclared (first use in this function)
    main.c:4: error: (Each undeclared identifier is reported only once
    main.c:4: error: for each function it appears in.)


    why i am getting error even if i am including all the defined files.?




    Note:
    1.if i have function fun() in 4th line of main.c and defined in extern.c. compilation is success(please see below code) with #cc main.c extern.c. why its working for functions and its not working for integers?

    //extern.c
    Code:
    void fun()
    {
           printf("hai");
    }
    //main.c
    Code:
    #include<stdio.h>
    int main()
    {
          fun();
          return 0;
    }
    #cc main.c extern.c
    Last edited by nkrao123@gmail.; 09-24-2011 at 08:01 AM.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by nkrao123@gmail.
    I am getting below error if i compile the program with even i including the all files as input to compiler
    Focus on main.c. It is obvious that i was not declared anywhere. Maybe you object: i is defined in extern.c! Yes, but when the compiler compiles main.c, it does not consider extern.c. Therefore, you must declare i in main.c. You don't want to define i again, so you declare i extern, e.g.,
    Code:
    #include<stdio.h>
    
    extern int i;
    
    int mian()
    {
        printf("%d\n", i);
        return 0;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    kotin
    Join Date
    Oct 2009
    Posts
    132
    Hi Laserlight,

    From your earlier reply's, i understand that the usage of "extern". i can understand that main.c will compile about if we use "extern int i" in main file.

    my doubt was fun() also not defined in main.c and even we did not included extern.c in main file. but its working.

    please give comment on why its working for functions and its not working for variables?

    why compiler did not considering extern.c for variable? and why it is considering extern.c for functions?
    Last edited by nkrao123@gmail.; 09-24-2011 at 10:36 PM.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    I hope Laze doesn't mind me taking a shot at this...

    I'll bet your last try where fun() from the second file is not included ended in an error saying it could not find the function.
    The code did not compile, leaving the earlier version where you did include fun() into the main file in the folder.

    Try this... go into your project's folders and delete all object (.o or .obj) and executable (.exe) files and try that again.

    Then when you compile keep a close eye on your compiler's messages ... you ignore them at your own peril.

  10. #10
    kotin
    Join Date
    Oct 2009
    Posts
    132
    Hi,

    I am doing the below steps

    1. I am created new folder and created below files and executed.
    1. created extern.c file
    //extern.c
    Code:
    #include<stdio.h>
    void fun()
    {
            printf("hai\n");
    }
    2.created main.c file
    //main.c
    Code:
    #include<stdio.h>
    int main()
    {
         fun();
         return 0;
    }
    3.compile the above program with #cc main.c extern.c
    4.The program compiling with out errors and creates binary file
    4.after running the binary file , we will get the output hai
    5.Please see the below execution stpes
    Code:
    $ vi extern.c 
    $ cat extern.c 
    #include<stdio.h>
    void fun()
    {
    	printf("hai\n");
    }
    $ vi main.c 
    $ cat main.c 
    #include<stdio.h>
    int main()
    {
    	fun();
    	return 0;
    } 
    $ cc main.c extern.c  -Wall
    main.c: In function ‘main’:
    main.c:4: warning: implicit declaration of function ‘fun’ 
    $ ls
    a.out		extern.c	main.c
    $ ./a.out 
    hai
    $
    At least it showing output as wt i expected by warning messages at compilation time.

    my question is

    why it it compiling for functions with waring messages. why it is not compiling(showing error messages) for variables with out using "extern int i"? is it limitaion?. For variables i should include that "extern.c" file?

    thanks in advance.
    Last edited by nkrao123@gmail.; 09-25-2011 at 12:07 AM.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by nkrao123@gmail.
    3.compile the above program with #cc main.c extern.c
    I am not sure which compiler you are using, but increase your warning level and you should find the compiler complaining about what CommonTater has been talking about. For example, if cc is gcc, you can pass the -Wall flag.

    Basically, what you are observing here is the use of an implicit declaration of foo: since no declaration for foo has been seen by the compiler in the translation unit up to that point, foo is assumed to be a function with no parameters, that returns an int. It so happens that this is really the case, but you should not rely on that.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by nkrao123@gmail. View Post
    Code:
    $ cc main.c extern.c  -Wall
    main.c: In function ‘main’:
    main.c:4: warning: implicit declaration of function ‘fun’ 
    $ ls
    a.out		extern.c	main.c
    $ ./a.out 
    hai
    $
    As Lase has pointed out... the compiler is taking it's best guess trying to finish it's job, and in this case you got lucky.

    This is probably the worst kind of "undefined behaviour", because there's a very high probability the compiler will guess wrong and will either bail or produce severely flawed code.

    IOW... your experiment worked by virture of pure dumb luck.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. header file including
    By kapil1089thekin in forum C Programming
    Replies: 3
    Last Post: 01-16-2011, 08:31 AM
  2. including header file
    By Delia in forum C Programming
    Replies: 1
    Last Post: 03-20-2010, 04:52 PM
  3. Replies: 3
    Last Post: 02-10-2009, 02:51 PM
  4. Including header file with in the header file
    By Arafat_211184 in forum C Programming
    Replies: 13
    Last Post: 12-19-2005, 10:03 AM
  5. Replies: 4
    Last Post: 12-14-2005, 02:21 PM