r/C_Programming • u/Yuki_pop • 23d ago
Could someone explain the practical differences between #pragma once and traditional include guards (#ifndef, #define, and #endif)?
81
Upvotes
r/C_Programming • u/Yuki_pop • 23d ago
View all comments
Show parent comment
8
u/FedUp233 23d ago
Yep, can make a difference!
Don't know what compiler you are using but gcc and g++ can recognize the include guards and behave just like you used the pragma in some cases. See
https://gcc.gnu.org/onlinedocs/cppinternals/Guard-Macros.html
https://gcc.gnu.org/onlinedocs/cpp/Once-Only-Headers.html
I think most modern compilers behave in similar fashion. Perhaps your files have something that prevents recognizing the guard macros.
Also, just as a note, the pragma does not cause the compiler to "early out" on the file when it sees the pragma in the second inclusion. In the case of the pragma or recognized include guards the header is not even opened other than the first time.