views:

396

answers:

9

I have some code that looks like this:

someFunc(value)
{

    switch(value){
        case 1:
        case 2:
        case 3:
#ifdef SOMEMACRO
        case 4:
        case 5:
#endif
           return TRUE;
    }
return FALSE;
}

SOMEMACRO is defined, and let's say the value is 4.. Why does case 4 and 5 get skipped and FALSE is returned instead? :(

Is it because I don't have a default case or am I not allowed to use an ifdef in the switch statement?

+1  A: 

Are you sure your module is compiled after you have defined SOMEMACRO?

GvS
A: 

Why not put "return false" in the default case?

MainID
A: 

Something in your assumptions isn't true, change the code like so and work backwards from there.

#define SOMEMACRO 1

someFunc(value)
{

    value = 4;

    switch(value){
        case 1:
        case 2:
        case 3:
#ifdef SOMEMACRO
        case 4:
        case 5:
#endif
           return TRUE;
    }
return FALSE;
}

If this doesn't return true you've got real problems.

Tony Lambert
+3  A: 

You don't specify which compiler/precompiler you're using, but most have an option to retain the result of the preprocessor -- The first thing I'd check would be what is being generated there.

Admittedly, I'd prefer to maintain code that looks a little more like this:

    someFunc(value)
    {
        switch(value){
            case 1:
            case 2:
            case 3:
                return TRUE;
#ifdef SOMEMACRO
            // Special build for SOMEMACRO Inc.
            case 4:
            case 5:
                return TRUE;
#endif
            default:
                return FALSE;
        }
    }
Rowland Shaw
+2  A: 

Depending on your compiler, there should be a way for you to read and inspect the preprocessed code. This way, you are freed from the need to guess and poke at it from a distance, and can see the exact code you're asking the compiler to compile. With gcc, the relevant option is -E.

unwind
+7  A: 

Try the following:

someFunc(value)
{
  switch(value){
    case 1:
    case 2:
    case 3:
#ifdef SOMEMACRO
#error macro is defined
    case 4:
    case 5:
#else
#error macro is not defined
#endif
       return TRUE;
  }
  return FALSE;
}

Compile this and see which of the two errors the compiler gives you.

Carl Seleborg
I use this sort of technique frequently when the preprocessor isn't doing what I think it should. It's sure a lot faster to diagnose than having to run the program to find out what's up.
Greg Hewgill
This is good :) I checked and the macro IS defined.. So the error might be elsewhere.. however if I just remove the ifdef, the code works :(.. Any ideas?
krebstar
Weird, now the code works without a problem.. I didn't even change the code.. I guess it wasn't the macro that was the problem..Thanks..
krebstar
+1 anyway.. Good tip.. Wish I could accept two answers..
krebstar
A: 

Others have already specified how to check if a macro is defined or not.

If the macro is being defined by your build system, a good way to find it out would be; to pipe the build output to grep (or similar text searching tool) to search for macro definition (something like: make | grep "-DSOMEMACRO").

If you don't have grep on your system; redirect the build output to a file and open it in your text editor, to inspect manually using text search.)

xk0der
A: 

Like the others I think one of your assumptions must be incorrect. You don't say which compiler you're using, but if it's MS Visual C++, try putting the following inside your #ifdef to verify that SOMEMACRO really is defined.

#ifdef SOMEMACRO

#pragma message("SOMEMACRO is defined")

    case 4:
    case 5:


#endif
Paul Mitchell
+2  A: 

"switch" Isn't Broken

to, more or less, quote The Pragmatic Programmer.

Go ahead and look somewhere else for the error. To convince yourself add value = 4 and #define SOMEMACRO right there in someFunc.

Make a clean build to make sure every dependancy is resolved.

gclj5
I think this one fixed my problem, but I guess my question wasn't phrased right..
krebstar