Preprocessor Directive Question

closed account (zb0S216C)
In the sys/types.h header, I've noticed some preprocessor directive defines( #define ) that haven't been assigned a value. Here's what I'm on about if you don't already:

1
2
3
#define __need_wchar_t
#define __need_size_t
#define __need_ptrdiff_t  

If a preprocessor define directive hasn't been assigned a value, what does it do? What can I use it for?
You use it to check if some condition is present at compile time:

1
2
3
#ifdef __need_wchar_t
// do something related to the need for wchar_t
#endif 

You also use that form of define for include guards:

1
2
3
4
#ifndef MY_HEADER_H
#define MY_HEADER_H
// ...
#endif 

which allows you to avoid including the same header more than once into a translation unit.
Last edited on
#defines arent always used to define constants, doing this just defines those values, perhaps so that they can be used for conditional compilation. For example when I make a project in visual studio, running on windows, and check the project properties under c++ -> preprocessor, _DEBUG and WIN32 are defined. when in release NDEBUG and WIN32 are defined. You can define your own things too then check whether they're defined

1
2
3
#ifdef _DEBUG
....
#endif 


so that the project is compiled differently depending on the solution configuration or platform
Last edited on
closed account (zb0S216C)
Thanks for your replies.
Topic archived. No new replies allowed.