Home > OS >  Is it possible to redefine "." using macros in C?
Is it possible to redefine "." using macros in C?

Time:01-08

I have a (possibly faulty) school assignment regarding the C-preprocessor, in which I should essentially define a macro which allows

Today is the 9.

to compile to

int a = 9;

Note the "." after the 9. The rest of the program is similar, I have no problem with that.

Now I replaced "Today" by int (#define Today int), "is" by a, "the" by = but I don't know what to do with the ".", given if I just blindly replace it by doing

#define . ;

I get a compile-time error. Is it even possible to do something with the dot?

CodePudding user response:

Is it possible to redefine "." using macros in C?

No.

given if I just blindly replace it by doing

#define . ;

I get a compile-time error. Is it even possible to do something with the dot?

No, it is not possible.

In the first place, the . in the text presented is not a separate token according to C's rules. It is part of 9., a floating-point constant. Macro replacement operates only on complete tokens.

In the second place, macro replacement is not a general search / replace. Macro names must be C identifiers, which start with either an underscore or a Latin letter, and contain only underscores, Latin letters, and decimal digits. Thus, it is not possible to define either . by itself or the full 9. as a macro name.

CodePudding user response:

Cheating but you can :)

#include <stdio.h>

#define MACRO(...) int a = 9;

int main(void)
{
MACRO(
    Today is the 9.
)
    printf("%d\n", a);
    return 0;
}

CodePudding user response:

#define Today int
#define is a
#define the = (int)


void foo(void)
{
    Today is the 9.;

    printf("%d\n", is);
}
  •  Tags:  
  • Related