help: please explain the output

This is a discussion on help: please explain the output within the C Programming forums, part of the General Programming Boards category; Code: #include<stdio.h> #include<conio.h> void main() { int a=1; clrscr(); printf("%d,%d,%d",a++ - a,a - a--,a++ - ++a); getch(); } for the ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    3

    Unhappy help: please explain the output

    Code:
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    int a=1;
    clrscr();
    printf("%d,%d,%d",a++ - a,a - a--,a++ - ++a);
    getch();
    }

    for the above code i am getting the output -1,-1,2
    I heard the printf takes the arguments from right to left.
    Can anyone tell me how this output is obtained?
    Attached Files Attached Files

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,914
    There is undefined behaviour because the same variable is modified more than once between consecutive sequence points, i.e., the sequence point just before the call of printf, and then the sequence point just after the evaluation of the arguments to printf.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    The behaviour is undefined as it is not known how printf() takes the arguments. It is compiler dependent whether it takes from right to left or the other way round.
    Also main should return int
    Code:
    int main(void)
    {
    anything.....................
    return 0;
    }
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,914
    Quote Originally Posted by BEN10
    The behaviour is undefined as it is not known how printf() takes the arguments. It is compiler dependent whether it takes from right to left or the other way round.
    Yes, but it would be more accurate to call that behaviour (as in order of evaluation of arguments) unspecified rather than undefined.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    When I saw the title, I knew this was an i++ ++i thread.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Dec 2009
    Posts
    3

    still didn't get it

    Thank you all
    But i am not still able to understand how i am getting the output -1,-1,2(right-left or left-right, in either case)

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by soniclavier View Post
    Thank you all
    But i am not still able to understand how i am getting the output -1,-1,2(right-left or left-right, in either case)
    Neither do you want to nor do you need to - so stop worrying!

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,914
    Quote Originally Posted by soniclavier
    But i am not still able to understand how i am getting the output -1,-1,2(right-left or left-right, in either case)
    We're telling you to not to bother understanding, because when you compile the code on another compiler, the results could be different... or the program might simply not compile, or format your hard disk drive when you run it, etc. It may even be the case that a different level of optimisation on the same compiler will lead to different results.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Dec 2009
    Posts
    3
    okie thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. execl()/fork() output
    By tadams in forum C Programming
    Replies: 19
    Last Post: 02-04-2009, 02:29 PM
  2. plz explain the output of the program.
    By raj_ksrt in forum C++ Programming
    Replies: 2
    Last Post: 05-26-2008, 03:43 AM
  3. Input & Output Explain?
    By 98dodgeneondohc in forum C Programming
    Replies: 5
    Last Post: 04-24-2005, 06:13 PM
  4. Trying to store system(command) Output
    By punxworm in forum C++ Programming
    Replies: 5
    Last Post: 04-20-2005, 06:46 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 04:27 AM

Tags for this Thread


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