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

4

u/actguru 22d ago

The practical difference is less code / typing for "#pragma once" which should be "#once" (even less).

2

u/torsten_dev 22d ago

It was proposed in n2896 but didn't gain consensus see section 5.38 and it hasn't been brought up again.

2

u/FedUp233 22d ago

Too bad you canโ€™t put something like this in a macro. Then you could just type

#include โ€œonce.hโ€
#once

In each file! ๐Ÿ˜๐Ÿ˜

2

u/WittyStick 22d ago

I've not tried, but you can use _Pragma in a macro. Maybe

#define once _Pragma("once")

1

u/actguru 22d ago

Worked for me. But I like ' $once '.

1

u/actguru 22d ago

Maybe a gcc flag: cc foo.c '-D$once=_Pragma("once")'