A Stupid Question. Pls Help.

This is a discussion on A Stupid Question. Pls Help. within the C Programming forums, part of the General Programming Boards category; Given the following program Code: #include <stdio.h> int main(void) { int a = 5; printf( "%d\n", ++a * --a ); ...

  1. #1
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    212

    A Stupid Question. Pls Help.

    Given the following program
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int a = 5;
    
        printf( "%d\n", ++a * --a );
    }
    Will it guaranteed to print 25 ?
    I compiled it using vc6 and gcc3.2, and the result were 25.
    But I doubt if it's guaranteed.
    The standard didn't define whether ++a or --a is evaluated first, right? So, if ++a is evaluated first, then it will be a temperory variable hold 6 at the left-hand-side of operator *, right? Ater that, --a is evaluated and generates a temperory variable hold 5, right? So the result will be 6 * 5 = 30.
    If --a is evaluated first, then the result will be 5 * 4 = 20, right?

  2. #2
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    a = 5;

    I was expecting 30 was the output but you said its 25.

    I compiled it with turbo C i got 30.

    Devc++ gave me 25 .
    Last edited by loko; 09-15-2005 at 08:58 PM.

  3. #3
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Have a look at this.

  4. #4
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    To reiterate the above FAQ link, it's worse than just an ambiguous order, the expression is undefined. The compiler could make the code format your hard drive and still be conforming, however unlikely that is.

  5. #5
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    212
    hmm...
    Then what about the order of the evaluation? Is it guaranteed that ++a will be evaluated first? Or is the order of evaluation undefined?

  6. #6
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    The whole expression is undefined, the order of evaluation is irrelevant, and yes, undefined. Once the expression is undefined, everything is undefined.
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int a = 5;
    
        printf( "%d\n", ++a * --a );
    }
    If I ran that code, and it outputted "-42", I couldn't complain my compiler was buggy.

  7. #7
    Registered User
    Join Date
    Sep 2005
    Posts
    12
    In Case of Visual c++, the order of evaluation will from right to left and on the precedence of operators

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int a = 5;
    
        printf( "%d\n", ++a * --a );
        return 0;
    }
    from right to left and higher precedence for pre incrementing operators than multiplication operator.so compiler will execute like this
    value of "a" after decrementing will be 4
    value of "a" after incrementing will be 5

    now value of a is 5

    so the result is 25.

    regards
    prasath

  8. #8
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by prasath
    In Case of Visual c++, the order of evaluation will from right to left and on the precedence of operators

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int a = 5;
    
        printf( "%d\n", ++a * --a );
        return 0;
    }
    from right to left and higher precedence for pre incrementing operators than multiplication operator.so compiler will execute like this
    value of "a" after decrementing will be 4
    value of "a" after incrementing will be 5

    now value of a is 5

    so the result is 25.

    regards
    prasath
    Visual C++ documents this behaviour? That's disgusting, it's undefined in C. Don't expect the code to work on another compiler. Is there an online link of this documentation? I'm interested to read it. I don't have Visual C++, or Windows.

  9. #9
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by prasath
    In Case of Visual c++, the order of evaluation will from right to left and on the precedence of operators
    Not true:

    from msdn

    Order of operations is not defined by the language. The compiler is free to evaluate such expressions in any order, if the compiler can guarantee a consistent result.

    Only the sequential-evaluation (,), logical-AND (&&), logical-OR (||), conditional-expression (? :), and function-call operators constitute sequence points and therefore guarantee a particular order of evaluation for their operands.
    So Microsoft documentation agrees with the C standard: the order of evaluation for operators ++ and -- is undefined.

    There is room for confusion between "order of evaluation" and "order of assignment" of components of an expression. The order of evaluation for things like ++ and -- is undefined.

    As far as Microsoft compilers: consider the following:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int a = 5;
        int b = 5;
    
        printf("astuff = %d\n", ++a * --a);
        printf("bstuff = %d\n", --b * ++b);
    
        return 0;
    }
    My output with Visual C++ from Visual Studio 6:

    astuff = 25
    bstuff = 25
    It doesn't matter what the compiler prints here, the values of the expressions are undefined. Any compiler can give whatever answer it wants to.

    Borland bcc32 gives this:

    astuff = 30
    bstuff = 20
    GNU gcc (cygwin on Windows XP) gives this
    astuff = 25
    bstuff = 25
    None of these is the "right" answer. None of these is the "wrong" answer. The values of the expressions are undefined.

    Regards,

    Dave
    Last edited by Dave Evans; 09-16-2005 at 12:40 PM.

  10. #10
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,171
    The values of the expressions are undefined.
    Not just the values. People tend to underestimate what the C standard means when it says something is "undefined." It doesn't just mean it can set the value to whatever, it means the program could make your computer do any arbitrary thing. It's much worse than just having an unpredictable end value. It's like trying to divide a number by 0.
    If you understand what you're doing, you're not learning anything.

  11. #11
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by itsme86
    Not just the values. People tend to underestimate what the C standard means when it says something is "undefined." It doesn't just mean it can set the value to whatever, it means the program could make your computer do any arbitrary thing. It's much worse than just having an unpredictable end value. It's like trying to divide a number by 0.

    The order of evaluation is unspecified. Unspecified behavior is not the same as undefined behavior. I should have been more careful with my terminology. Since the order of evaluation is unspecified, the values of the expressions are unspecified.


    ISO/IEC 9899



    3.4.3
    1 undefined behavior
    behavior, upon use of a nonportable or erroneous program construct or of erroneous data, for which this International Standard imposes no requirements

    2 NOTE Possible undefined behavior ranges from ignoring the situation completely with unpredictable results, to behaving during translation or program execution in a documented manner characteristic of the environment (with or without the issuance of a diagnostic message), to terminating a translation or execution (with the issuance of a diagnostic message).

    3 EXAMPLE An example of undefined behavior is the behavior on integer overflow.


    3.4.4
    1 unspecified behavior
    behavior where this International Standard provides two or more possibilities and imposes no further requirements on which is chosen in any instance

    2 EXAMPLE An example of unspecified behavior is the order in which the arguments to a function are evaluated.

    .
    .
    .

    J.1 Unspecified behavior
    1 The following are unspecified:

    .
    .
    .

    The order in which subexpressions are evaluated and the order in which side effects take place, except as specified for the function-call (), &&, ||, ?:, and comma operators (6.5).
    So the compliant compiler is allowed to use any order of evaluation that it wants to, but is not free to format your hard drive.

    Also, note that no matter what order of evaluation you apply, the value of neither of the expressions in my example will be 42, so we must continue to look for the Question.

    Regards,

    Dave
    Last edited by Dave Evans; 09-16-2005 at 01:20 PM.

  12. #12
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    But even with the new 64 bit processors, the computers are still not big/fast enough to figure out exactly what the question is.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stupid Question Probably
    By Kyrin in forum C Programming
    Replies: 2
    Last Post: 05-07-2006, 01:51 AM
  2. Replies: 7
    Last Post: 11-04-2005, 12:17 AM
  3. Stupid Question
    By digdug4life in forum C++ Programming
    Replies: 22
    Last Post: 05-17-2005, 12:43 PM
  4. stupid, stupid question
    By xelitex in forum C++ Programming
    Replies: 5
    Last Post: 12-22-2004, 08:22 PM
  5. Stupid question: What does Debugger do?
    By napkin111 in forum C++ Programming
    Replies: 6
    Last Post: 05-02-2002, 11:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21