0

following code is given:

#define CELSIUS *10

void main()
{
    int testVar=0;

    if(!testVar) testVar = 10CELSIUS;
}

My IDE gives me following message: Constant "CELSIUS" has an invalid suffix. But it seems so that the program compiles. Do I have to care about this message?

xy36
  • 315
  • 1
  • 11

2 Answers2

2

A suffix is something "added at the end". So when the compiler complains about an invalid suffix at the end of the integer constant, it's upset by the single token

10CELSIUS

as it recognises the CELSIUS within as integer suffix. Allowed suffixes are (N1570 § 6.4.4.1/1):

u
ul
ull
l
lu
ll
llu

These are small L letters, the up case version of each letter is allowed, too (though not lL or Ll in any combination)

They mean unsigned, long and long long and affect the type of the preceding integer constant.

Thus, to achieve macro substitution you need to separate the integer constant with a space, thus producing the token CELSIUS which can then be replaced with *10 by the preprocessor:

#include <stdio.h>
#define CELSIUS *10

int main()
{
    int testVar=0;

    if(!testVar) testVar = 10 CELSIUS;
    printf("%d\n", testVar);

    return 0;
}

Note: This is a more modern signature for the main function. Even better (IMO) would be int main(int argc, char **argv);

Daniel Jour
  • 15,896
  • 2
  • 36
  • 63
1

Becareful. If you use macros like that, you will have a lot of problems in the future. Better use:

/*....*/

#define CELSIUS(x)    ((x)*10)

/*....*/

if(!testVar) testVar = CELSIUS(10);
    printf("%d\n", testVar);

/*....*/

If you don't do it, you will have a lot of undesiderable behaviours in your code, like

Good Programming Practices for Macro Definitions (#define) in C

Community
  • 1
  • 1
Alexi
  • 389
  • 1
  • 8