topkek
topkek

Reputation: 169

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

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?

Upvotes: 1

Views: 126

Answers (2)

John Bollinger
John Bollinger

Reputation: 180351

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.

Upvotes: 3

0___________
0___________

Reputation: 67546

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


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

    printf("%d\n", is);
}

Upvotes: 0

Related Questions