Results 1 to 7 of 7

Thread: Why is the output different?

  1. #1
    Join Date
    Mar 2011
    Beans
    56

    Red face Why is the output different?

    Look at the follwing code both are same(atleast to some extent) but the output seems to be different. Why???
    Code:
    #include<stdio.h>
    //#include<iostream>
    //#include<conio.h>
    
    void main()
    {
        //clrscr();
    
        int c, a = 7;
        int b = a++ + ++a + a++ + a++ - ++a;
        printf("\n%d",b);
        printf("\n%d",a);
        a = 7;
        c = a++ + ++a + a++ + a++ - ++a;
        a = 7;
        printf("\n%d",c);
        printf("\n%d",a);
        printf("\n%d %d %d %d %d", a++, ++a , a++ , a++ , ++a);
    
    }
    Code:
    23
    12
    23
    7
    11 12  9 8  12
    Code:
    #include<stdio.h>
    //#include<iostream>
    //#include<conio.h>
    
    void main()
    {
        //clrscr();
    
        int c, a = 7;
        int b = a++ + ++a + a++ + a++ - ++a;
        printf("\n%d",b);
        printf("\n%d",a);
        a = 7;
        c = a++ + ++a + a++ + a++ - ++a;
        a = 7;
        printf("\n%d",c);
        printf("\n%d",a);
        printf("\n%d", a++);
        printf(" %d", ++a);
        printf(" %d", a++);
        printf(" %d", a++);
        printf(" %d", ++a);
    
    }
    Code:
    23
    12
    23
    7
    7 9 9 10 12
    can anyone explain why???
    Vanangamudi
    Minniyal Poriyalan
    Puduvai,Bharatham.

  2. #2
    Join Date
    Jan 2008
    Beans
    1,532

    Re: Why is the output is different??

    Welcome to the wonderful world of undefined behavior! The compiler is allowed to evaluate arguments in any order it chooses. So just don't write code that depends on the output of argument evaluation.

  3. #3
    Join Date
    Aug 2007
    Location
    Novocastria, Australia
    Beans
    751
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: Why is the output is different??

    Recompile your code with warnings turned on, and the compiler will give you some hints. As Simian Man said, there's lots of undefined behaviour in your code. For example, your first program:

    Code:
    ps@pris:~$ vim test.c
    ps@pris:~$ gcc -Wall test.c 
    test.c:5:6: warning: return type of ‘main’ is not ‘int’
    test.c: In function ‘main’:
    test.c:10:37: warning: operation on ‘a’ may be undefined
    test.c:10:37: warning: operation on ‘a’ may be undefined
    test.c:10:37: warning: operation on ‘a’ may be undefined
    test.c:10:37: warning: operation on ‘a’ may be undefined
    test.c:14:28: warning: operation on ‘a’ may be undefined
    test.c:14:28: warning: operation on ‘a’ may be undefined
    test.c:14:28: warning: operation on ‘a’ may be undefined
    test.c:14:28: warning: operation on ‘a’ may be undefined
    test.c:18:55: warning: operation on ‘a’ may be undefined
    test.c:18:55: warning: operation on ‘a’ may be undefined
    test.c:18:55: warning: operation on ‘a’ may be undefined
    test.c:18:55: warning: operation on ‘a’ may be undefined
    ps@pris:~$

  4. #4
    Join Date
    Aug 2007
    Location
    Novocastria, Australia
    Beans
    751
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: Why is the output is different??

    Quote Originally Posted by NovaAesa View Post
    Recompile your code with warnings turned on, and the compiler will give you some hints. As Simian Man said, there's lots of undefined behaviour in your code. For example, your first program:

    Code:
    ps@pris:~$ vim test.c
    ps@pris:~$ gcc -Wall test.c 
    test.c:5:6: warning: return type of ‘main’ is not ‘int’
    test.c: In function ‘main’:
    test.c:10:37: warning: operation on ‘a’ may be undefined
    test.c:10:37: warning: operation on ‘a’ may be undefined
    test.c:10:37: warning: operation on ‘a’ may be undefined
    test.c:10:37: warning: operation on ‘a’ may be undefined
    test.c:14:28: warning: operation on ‘a’ may be undefined
    test.c:14:28: warning: operation on ‘a’ may be undefined
    test.c:14:28: warning: operation on ‘a’ may be undefined
    test.c:14:28: warning: operation on ‘a’ may be undefined
    test.c:18:55: warning: operation on ‘a’ may be undefined
    test.c:18:55: warning: operation on ‘a’ may be undefined
    test.c:18:55: warning: operation on ‘a’ may be undefined
    test.c:18:55: warning: operation on ‘a’ may be undefined
    ps@pris:~$

    EDIT: I'll elaborate. When you do something such as:
    Code:
    foo++;
    the value of the expression is simply "foo + 1", however foo then gets incremented. When does it get incremented though? It gets incremented *some* time before the next sequence point. Exactly when it gets incremented before the next sequence point is undefined. Have a read about it here http://en.wikipedia.org/wiki/Sequence_point

  5. #5
    hakermania's Avatar
    hakermania is offline Τώρα ξέρεις τι γράφω εδώ!
    Join Date
    Aug 2009
    Location
    Greece
    Beans
    1,705
    Distro
    Ubuntu Development Release

    Re: Why is the output is different??

    Normally, as I know it,
    Code:
    int a=0;
    cout << a++ <<endl;
    will show
    Code:
    0
    but value of 'a' will be 1 after this.

    Conversely, if you do
    Code:
    int a=0;
    cout << ++a << endl;
    then it will show 1 and the value of 'a' will be 1 afterwards...

    Screenshot:

  6. #6
    Join Date
    Feb 2009
    Beans
    1,469

    Re: Why is the output is different??

    Quote Originally Posted by NovaAesa View Post
    EDIT: I'll elaborate. When you do something such as:
    Code:
    foo++;
    the value of the expression is simply "foo + 1", however foo then gets incremented. When does it get incremented though? It gets incremented *some* time before the next sequence point. Exactly when it gets incremented before the next sequence point is undefined. Have a read about it here http://en.wikipedia.org/wiki/Sequence_point
    I'm sure you mean that the value of the expression

    Code:
    ++foo;
    is (foo + 1). The value of foo++ is still just foo.

  7. #7
    Join Date
    Aug 2007
    Location
    Novocastria, Australia
    Beans
    751
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: Why is the output is different??

    Quote Originally Posted by trent.josephsen View Post
    I'm sure you mean that the value of the expression

    Code:
    ++foo;
    is (foo + 1). The value of foo++ is still just foo.
    You're absolutely right, my mistake! I should really reread my posts more thoroughly before hitting submit.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •