6

#define TML_ID - No value is assigned to TML_ID. There's no problem in compilation or building executables. will this assign any default value like null to TML_ID or will TML_ID be considered undefined ?

songyuanyao
  • 169,198
  • 16
  • 310
  • 405
JSRK
  • 61
  • 1
  • 6

4 Answers4

7

This simply says that the macros is defined, so you can do this in main or any other function:

#ifdef TML_ID
printf("Defined!\n");
#else
printf("Undefined!\n");
#endif
ForceBru
  • 43,482
  • 10
  • 63
  • 98
  • 1
    `#endif` (had to) – qxz Dec 16 '16 at 07:33
  • 1
    @qxz, yep, I forgot that, thanks for pointing out. Edited now. – ForceBru Dec 16 '16 at 07:36
  • Thanks. yes, it's considered defined even if no value is assigned to it. – JSRK Dec 16 '16 at 09:13
  • @ForceBru Before compilation C preprocessor will expand the macro, replacing their textual appearances with their values. If a macro is defined w/o value, like other answers said, their values will be garbage (undefined), in such case how does preprocessor know which value to substitute? – torez233 Feb 28 '22 at 03:15
  • 1
    @torez233, none of the other answers said that their values will be garbage. If a macro has no value, the use of this macro will be simply deleted (that use will be replaced with nothing). – ForceBru Feb 28 '22 at 12:01
  • @ForceBru yes you're right, I misunderstood the other answers. Thanks for the info – torez233 Mar 02 '22 at 06:42
1

#define doesn't assign a value to the macro. In fact, it's considered as a flag to tell the compiler that a specific macro has been defined.

You can imagine it as if you declare a variable without assigning any values. It will have a garbage value but it will reserve a space in the memory. But in case of a macro, the definition won't reserve a space. Only a hint for the compiler.

Sam
  • 71
  • 7
0

Without assigned a value, macros in this way are usually used to prevent including same .h file multiple times, this way:

#ifndef _FILENAME
#define _FILENAME

//declaration code

#endif

If some .cpp file includes, for example, two different .h files, which both include our .h file, then .cpp will have only one copy of declaration code, since second time _FILENAME macro will be DEFINED, and declaration code will be skipped.

  • 1
    Names that begin with an underscore followed by a capital letter (`_FILENAME`) and names that contain two consecutive underscores are reserved for use by the implementation. Don't use them in your code. – Pete Becker Dec 16 '16 at 13:37
0
#define MACRO

defines a macro named MACRO. It has no content, so if you wrote something like std::cout << MACRO you'd get an error because there's nothing there. This is often used to conditionally use new keywords:

#if CPP_VERSION_C11
    #define NOEXCEPT noexcept
#else
    #define NOEXCEPT
#endif

void f() NOEXCEPT {
    // whatever
}

There are two other ways you can use such a macro. You can check whether it's defined:

#ifdef MACRO
    // code for one branch
#else
    // code for other branch
#endif

For that code, since MACRO has been defined, the preprocessor will look at the code in the first branch, and skip the code in the second branch. If MACRO had not been defined, it would skip the code in the first branch rather than the second. You can also do the same thing this way:

#if defined(MACRO)

or you can use it in a constant expression in a preprocessor directive:

#if MACRO
    // code for one branch
#else
    // code for other branch
#endif

here, MACRO gets the value 0, the #if sees the value 0, and the preprocessor skips the first branch. The same thing occurs in more complex expressions:

#if MACRO + 1 > 0
    // code for one branch
#else
    // code for other branch
#endif

Here, MACRO has the value 0, MACRO + 1 has the value 1, and the first branch will be selected.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
  • Re: "here, MACRO gets the value 0, the #if sees the value 0". In case anyone's wondering, this behaviour is a corollary of section 6.10.1 of the C standard. (same section in all three of C99, C11 and C17.) – AJM Jun 13 '22 at 16:24