I am trying to grasp how actually multiple definitions of include files collaborate and sometimes collide. So I have a custom header file file.h
, where some functions are declared within an #include guard
. Example:
#ifndef FILE_Ariew7OGJknw00
#define FILE_Ariew7OGJknw00
#include <stdlib.h> //assume minimal
//interfaces of functions
#endif
Now I have several questions.
1. Since #include <stdlib.h>
is already included in the header file.h
, I don't need to include it in my file.c
. Because file.c
includes the file.h
which includes stdlib.h
. However when I include <stdlib.h>
after file.h
in my .c
, it still works properly. Why is that so? Like:
#include <stdio.h>
#include "file.h"
#include <stdlib.h>
//code
2. Also what happens if I do:
#include <stdio.h>
#include <stdlib.h>
#include "file.h"
//code
In reality nothing but why isn't file.h
definition skipped (or is it?) based on the guards, since the library stdlib
is already included.
Lastly, a more general question: Why are #include guards more widespread used than #pragma once
even though the latter has several advantages such as:
• less code
• avoidance of name clashes (i.e.FILE_Ariew7OGJknw00
)
• improvement in compilation speed