-
#ifndef
when we create header files i know you can do it like this..
blah.h
Code:
#ifndef BLAH_H
#define BLAH_H
void blah();
#endif
in main.c if i was to include this header file i would do
#include "blah.h"
but if i didnt have the #ifndef stuff, this could still work couldnt it?
so then whats the point for the #ifndef stuff?
and say if blah was named blah.c i could do #include "blah.c" without the ifndef stuff aswell right?
-
its so that it is not included more than once.
Code:
#ifndef BLAH_H //if BLAH_H is not defined (BLAH_H is just a label for the following code)
#define BLAH_H //if BLAH_H is not previously defined then define it :)
...
#endif //self explanatory (i hope)
read this faq entry
http://faq.cprogramming.com/cgi-bin/...&id=1043284392
-
Simiple test:
header1.h
Code:
#ifndef HEADER1_H_
#define HEADER1_H_
void foo (void)
{
printf("I'm in foo()\n");
}
#endif
header2.h
Code:
void bar (void)
{
printf("I'm in bar()\n");
}
main.c
Code:
#include <stdio.h>
#include "header1.h"
#include "header2.h"
#include "header1.h"
#include "header2.h"
int main(void)
{
foo();
bar();
return 0;
}
Compile and look at the results
-
I did exactly what Thantos said, but included #defines and int/char declartions.
Why doesn't #define and int/char declaration in header2.h file give error? Why only functions??
Please let me know..
-
Way to bump an old thread.
#defines don't give an error because the preprocessor doesn't care if there are multiple declartions, it just uses the newest one