Exforsys
+ Reply to Thread
Results 1 to 2 of 2

Result of Program

This is a discussion on Result of Program within the C and C++ forums, part of the Programming Talk category; I have idea about prefix and postfix operators and operations using these operators in C programming language. But I am ...

  1. #1
    ashlee is offline Senior Member Array
    Join Date
    Apr 2006
    Answers
    108

    Thumbs up Result of Program

    I have idea about prefix and postfix operators and operations using these operators in C programming language. But I am confused about the following program?
    main()
    {
    int a=5,b=0;
    y=a+++a+++a;
    printf(" a=%d b=%d ",a,b);
    }

    How will the above get solved? I am confused on the operations. Kindly someone help me out.


  2. #2
    Adrian is offline Senior Member Array
    Join Date
    Apr 2006
    Answers
    124
    The precedence of operators is first Postfix increment/decrement operator and the associativity for this is from left-to-right.
    So we have a++ gets evaluated first and so the expression first becomes
    y=a+++a+++a;
    y=5+5+5=15
    and then a gets increment twice and so a becomes 7

    So your output would be
    a=7 b=15;

    But a small typo error you have made is instead of b=a+++a+++a; You have given as y=a+++a+++a;


Latest Article

Network Security Risk Assessment and Measurement

Read More...