r/C_Programming • • 22d ago

Could someone explain the practical differences between #pragma once and traditional include guards (#ifndef, #define, and #endif)?

81 Upvotes

58 comments sorted by

View all comments

19

u/Alex_1503 22d ago

Pragma once is not (or was not) supported on all compilers, ifndev is more traditional, if I understand correctly. Nowadays idk if it matters, but the approaches are kind of identical if supported. Probably ifndef is more portable if that is a concern

6

u/mikeblas 22d ago

It's not standardized, all mainstream compilers support it. (Sorry, I know it's lame to use Wikipedia as a reference, but even cppreference.com links there!)

-5

u/Feer_C9 22d ago

since when gcc supports pragma once? Last time I checked it didn't. Maybe I'm old

50

u/Gutrix_HD 22d ago

GCC has supported #pragma once since v3.4 (2004).

You should probably update your compiler.

16

u/UltimaN3rd 22d ago

It's been supported since GCC 3.4 (2004): https://gcc.gnu.org/gcc-3.4/changes.html

11

u/Feer_C9 22d ago

lol I thought it didn't, looks like I didn't search right