0

Say I have a header file header.h with an include guard that I #include into my main.c file:

#ifndef header_h
#define header_h

/* Maybe I define some things here, or mark some functions as extern "C", doesn't really matter */

#define PI 3.1415926535897932384

#ifndef COOL_PEOPLE
#define COOL_PEOPLE \
    you,            \
    me
#endif

extern "C"
{
    void a(void);
    void b(int number);
}

#endif // header_h

Why is it that the definition of header_h can be multiline without backslashes while I have to use backslashes to escape the newlines in the definition of COOL_PEOPLE? For example, I don't need to do the following to get the include guard to work:

#ifndef header_h
#define header_h                                                                                   \
                                                                                                   \
/* Maybe I define some things here, or mark some functions as extern "C", doesn't really matter */ \
                                                                                                   \
#define PI 3.1415926535897932384                                                                   \
                                                                                                   \
#ifndef COOL_PEOPLE                                                                                \
#define COOL_PEOPLE \
    you,            \
    me                                                                                             \
#endif                                                                                             \
                                                                                                   \
extern "C"                                                                                         \
{                                                                                                  \
    void a(void);                                                                                  \
    void b(int number);                                                                            \
}                                                                                                  \

#endif // header_h

This has been confusing me ever since I found out about them, because in this way, include guards do not seem to act similar to other preprocessor macros. I tried looking around, but it was always about needing backslashes to make multiline macros in general. Is there something about the concept I'm not understanding?

muff1nOS
  • 1
  • 1
  • 1
    The include guards don't define any value to the macro. `#define header_h` is it, it is blank. Try using the `header_h` in some statement and use `gcc -E /path/to/file` to see how it gets substituted by the pre-processor – Rahul Bharadwaj Sep 08 '21 at 04:06
  • 1
    Does this answer your question? [Are empty macro definitions allowed in C? How do they behave?](https://stackoverflow.com/questions/13892191/are-empty-macro-definitions-allowed-in-c-how-do-they-behave) – Rahul Bharadwaj Sep 08 '21 at 04:07
  • 3
    `header_h` is not a multiline macro, so why would it need backslashes? – Barmar Sep 08 '21 at 04:09
  • It seems I made a mistake in thinking that `header_h` was multiline rather than empty; I completely forgot about defining empty macros. – muff1nOS Sep 08 '21 at 06:37

0 Answers0