r/C_Programming • • 28d ago

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

80 Upvotes

58 comments sorted by

View all comments

Show parent comment

2

u/FedUp233 28d 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 28d ago

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

#define once _Pragma("once")

1

u/actguru 28d ago

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

1

u/actguru 28d ago

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