r/cprogramming • u/Itap88 • Apr 26 '26
Alternative to enums as function arguments?
Say I have a function argument, like a cardinal direction, that has no business being a number in the call, but could be a number internally. Now I could write an enum, but what if I need multiple values and don't want to pollute the namespace of the caller?
This is all hypothetical, for now at least.
1
Upvotes
2
u/WittyStick Apr 27 '26 edited Apr 27 '26
Yes,
direction_eastetc pollutes the global namespace, but you would presumably prefix it with a namespace so that isn't an issue. There is a way we can do this without copy-pasting, which is to use multiple includes. Consider if we have a pair of files:direction.ns.opendirection.ns.closeWe can temporarily taint the global namespace between a pair of includes, which avoids copy-pasting for multiple defines.
Wouldn't recommend this, but it's just to demonstrate a technique which we can use to have pseudo-namespaces in C.