Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

#define struct union and #define else still compiles any C program?

Someone claimed in a presentation that if you add

#define struct union
#define else

at the beginning of any valid C program, that program would still compile. That seems like a bold claim. Any counterexample you have in mind and prove that guy wrong?

like image 854
webuster Avatar asked Feb 05 '14 14:02

webuster


2 Answers

I managed to find a counterexample (C99):

for (int i = 0; i < 5; ++i)
    if (i > 2)
        do_smth();
    else if (i < 4)
        do_smth_else();

This one doesn't compile because if you #define else the variable i goes out of scope. But that doesn't involve the #define struct union thing. Any other ideas?

like image 195
webuster Avatar answered Sep 19 '22 13:09

webuster


#define struct union

struct OBJ
{
    int i1;
    double d1;
};

int foo()
{
    struct OBJ obj = { 1, 2.0 };

  return 0;
}

C2078: too many initializers

like image 37
Serve Laurijssen Avatar answered Sep 17 '22 13:09

Serve Laurijssen