2

I came across the following puzzle somewhere

#include <stdio.h> int main() { { /*Fill in something here to make this code compile ........... */ ooOoO+=a; } #undef ooOoO printf("%d",ooOoO); return 0; } 

In short I want to ask how can I use ooOoO in printf after it has been #undef ed?

5
  • 2
    You have two opening braces after main(); is that intended? Commented Feb 10, 2011 at 18:28
  • I think it is. That one makes the puzzle even interesting. Commented Feb 10, 2011 at 20:37
  • @James It's intended, and prevents trivial solutions like that of VJo below. Commented Feb 11, 2011 at 5:12
  • @Jim: Since a block can be empty, it doesn't really add anything to the problem: any solution would just need to start with a closing brace. Commented Feb 11, 2011 at 5:19
  • @James Thanks, you're right, adding a } to VJo's "trivial" solution would be a "trivial" modification. Sorry for the brainfart. Commented Feb 11, 2011 at 5:36

5 Answers 5

11

You need to declare it as a variable:

#define ooOoO int ooOoO = 42; int a = 1; { ooOoO 

Macro-replacement is non-recursive; while ooOoO is being replaced, the identifier ooOoO will not be treated as a macro name.


If you are looking for a solution that does not use a macro, then you can simply ignore the #undef directive and never declare ooOoO as a macro. It is permitted in both C and C++ to #undef an identifier that is not defined as a macro.

Sign up to request clarification or add additional context in comments.

3 Comments

why the opening bracket? There number of opening brackets in the OPs question matches the number of closing brackets. Am I missing something?
@Atmoscreations: I assumed (perhaps incorrectly) that the second left brace following main() was a typo; if it isn't, then the block it introduces would need to be closed anyway before declaring ooOoO as a variable, otherwise it would not be in scope when the printf statement is reached.
I think the bracket is intended... But you're right. This causes the variable to be out of scope otherwise.
6

After reformatting the code (indent) and adding the solution, that's what I receive:

#include <stdio.h> int main() { { /*-Insert starts here-*/ } int ooOoO = 0, a=3; { /*-Insert ends here-*/ ooOoO+=a; } #undef ooOoO printf("%d",ooOoO); return 0; } 

compiles and prints 3

Comments

3

How about this?

#include <stdio.h> int main(){ int ooOoO = 0; { int a = 3; ooOoO+=a; } #undef ooOoO printf("%d",ooOoO); return 0; } 

1 Comment

the second opening bracket is missing after main. doesn't match the question though
1
#include <stdio.h> int main(){ { /*Fill in something here to make this code compile */ } int a = 0, ooOoO=0; #define ooOoO ooOoO { /* */ ooOoO+=a; } #undef ooOoO printf("%d",ooOoO); return 0; } 

1 Comment

you seem to miss that there's a } missing at the end and therefore doesn't compile.
0

The #undef undefines the symbol to the preprocessor so that it does not get substituted with something else, but ooOoO still gets to the compiler.

2 Comments

Of course, but how did ooOoO get defined within scope? That's what the other answers provide.
Sure, but this was explicitly asked. James McNeils gave the solution but only explained this in an edit after I posted (and I cannot comment yet).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.