multiple include doesn't give compile error

This is a discussion on multiple include doesn't give compile error within the C Programming forums, part of the General Programming Boards category; Hi All I have a main file (main.c) which includes show.h and show1.h But show1.c also includes show.h. show.h looks ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    72

    multiple include doesn't give compile error

    Hi All

    I have a main file (main.c) which includes show.h and show1.h
    But show1.c also includes show.h. show.h looks like

    Code:
    void init() ;
    void show() ;
    void set(int x) ;
    int *get() ;
    
    extern int  *y ;
    Because it is included twice I was expecting compile errors, but the program compiles/runs fine, why ?
    I thought, in this situation, I needed things like #ifndef .... #endif!

    Below are the listings of all the files

    main.c
    Code:
    #include "show.h"
    #include "show1.h"
    
    int main(int argc, char *argv[])
     {
       init() ;
       set(10) ;
       show() ; 
    
       set(20) ;
       show() ; 
    
       // -------
       show1() ;
    
       set(303) ;
       show1() ;
       return 0 ;
     }
    show.c
    Code:
    #include <stdlib.h> // malloc
    #include <stdio.h>  // printf
    
    #include "show.h"
    
    int *y ;
    
    void init() {
      y = malloc(sizeof(int)) ;
    }
    
    void set(int x) {
      *y = x ;
    }
    
    int * get() {
      return y ;
    }
    
    void show() {
      printf("value is %d\n", *y) ;
    }
    show.h (see above)
    show1.c
    Code:
    #include <stdio.h>  // printf
    #include "show.h"
    
    extern int* y ;
    
    void show1() {
      printf("the value is %d\n", *y) ;
      show();
    }
    show1.h
    Code:
    void show1() ;

  2. #2
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    If two .c files includes one .h file, there will be no problem and its usual.

    If one .c includes one .h more than once, there will be problems. You should use precompiler directives to make sure each file is included only once.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    72
    I see, so it has todo with header files including each other

    cheers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  4. using c++ in c code
    By hannibar in forum C Programming
    Replies: 17
    Last Post: 10-28-2005, 09:09 PM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM

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