Can you C the trick ? [Problem of the week]

 Untitled How to write “X” so that:

int a= 0;
cout<<"a = "<<a;
//console says a = 5 Positive five!

a= 0;
cout<<"a = "<<a;
//console says a = -5 Negative five!

waiting for the answers in the comments (comments are moderated) .. i will show them after a couple of right answers..

if your answer is wrong “which is fine”, i will pass the comment..

if it is right .. i will reply on the comment or maybe to your mail telling you to stop trying..


now to the Winners (in order of who replied first):

  1. me 😀

16 thoughts on “Can you C the trick ? [Problem of the week]

  1. #define X(p)

    Actually, I didn’t try that solution… but seems it may do the job 🙂

  2. A few choices…

    #define X(stmt) do { stmt; stmt; stmt; stmt; stmt; } while (0)

    #define X(stmt) do { stmt; a *= 5; } while (0)

    #define X(stmt) crap(&a)
    static void crap(int *x)
    static int value = 5;
    *x = value;
    value = -value;

  3. @ Tecno ..
    no.. not that easy .. try again .. and give me the code .. and try it before you send it unless you are very sure 😛

  4. some people are trying .. 😀
    others seem to have got it already
    i cant compile your code .. can you please send a line that will compile ?
    and no need to be generic just for the +5 -5 case

  5. @Fouad:

    The problem is that my comment got corrupted as it seems that WordPress has considered the less-than and greater-than signs in the code as the beginning and end of an Html tag 😀

    I will rewrite the code differently so that I can hopefully avoid the tag problem.
    X will be a macro that repeats the statement passed to it (whether a++ or a–) for 5 times.

    #define X(N) while( abs(N) != 4 );

    where the function abs( ) is defined as:

    inline int abs(int x) { return x>0?x:-x;}

  6. thanks for all who replied
    the model answer “according to myself” 😀
    #define X(m) m; m; m; m; m

    the logic behind is in the next comment..

  7. .. since the ++ suffix operator “a++” is evaluated -usually- the last thing in the whole expression
    int a = 0;
    v = (a++) + 3;
    //v = 3 , a = 1; .. so a++ equals 0 not 1

    so if you define X as a function that accepts by reference .. or any other way .. it will not work .. it will receive (Zero) as an argument

    we need something that accepts an “expression” not a variable
    nothing does that “in C” except Macros

    a Macro is a kind of function that is executed in pre-compilation time
    #define constant 8
    means (Replace every word “constant” in the coming code with “8”)

    other examples are:
    #define min(x,y) (x>=y)?x:y

    there are other features in Macros that are simply awesome string literals are not the least ..

    then we get “a++” in “m” and we replace the macro with “a++; a++; a++; a++; a++”

    #define X(m) m; m; m; m; m
    the last expression is not “;” terminated because the macro is written in a terminated line:

  8. a Macro is a kind of function that is executed in pre-compilation time
    #define constant 8
    Sorry but Idon’t understand whatit’s mean??
    Is it akind of standard functions??

  9. ok let me explain farther..
    1- a macro is NOT a function by the meaning we know .. that is …
    #it Replaces Some Code with other code while the function evaluates some input and returns some output
    # a function accepts a type .. a macro accepts a string literal .. its like copy and paste
    “#define macro(x) x + 1” replaces macro(5) as a string in the code .. with 5+1 in the code .. then the compiler starts

    2- some macros are standard .. like min, max, size_t, wchar .. some are not.. some are Microsoft specific .. some are GCC specific

    3- when to choose a macro over a function
    “From MSDN”
    If you need to choose between a function and a macro implementation of a library routine, consider the following trade-offs:

    *Speed versus size The main benefit of using macros is faster execution time. During preprocessing, a macro is expanded (replaced by its definition) inline each time it is used. A function definition occurs only once regardless of how many times it is called. Macros may increase code size but do not have the overhead associated with function calls.

    *Function evaluation A function evaluates to an address; a macro does not. Thus you cannot use a macro name in contexts requiring a pointer. For instance, you can declare a pointer to a function, but not a pointer to a macro.

    *Type-checking When you declare a function, the compiler can check the argument types. Because you cannot declare a macro, the compiler cannot check macro argument types; although it can check the number of arguments you pass to a macro.

Leave a Reply