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