Exforsys
+ Reply to Thread
Results 1 to 3 of 3

What is the ouput?

This is a discussion on What is the ouput? within the C and C++ forums, part of the Programming Talk category; int main() { int i=5; printf("\n%d",++i + ++i + ++i + ++i + ++i ); } What will be the ...

  1. #1
    Adrian is offline Senior Member Array
    Join Date
    Apr 2006
    Answers
    124

    What is the ouput?

    int main()
    {
    int i=5;
    printf("\n%d",++i + ++i + ++i + ++i + ++i );
    }

    What will be the result of this code using UNIX OS with CC compiler? Howabout if a TC compiler is used, what will it display? If there is any difference, why is it that way?


  2. #2
    blenda is offline Member Array
    Join Date
    May 2006
    Answers
    36
    Hi,

    The answer for both would be the same. The output for the program is 50. It gets executed as follows:
    I gets initialized to 5 and then among + and the increment operator ++, the increment operator ++ takes higher priority. And so i get increment as many times as ++ is encountered. So gets incremented 5 times which gives value of i as 10. Then get added five times which results in output 50 to be printed.

    Regards,
    Blenda


  3. #3
    svs_aditya's Avatar
    svs_aditya is offline Aditya Array
    Join Date
    Jul 2006
    Location
    India
    Answers
    10

    Thumbs up

    Hi Adrian,

    I belive this is the answer for your question.


    int main()
    {
    int i=5;
    printf("\n %d", ++i + ++i + ++i + ++i + ++i );
    // 6 + 7 + 8 + 9 + 10 =40

    printf("\n %d", ++i + ++i + ++i + ++i + i++ );
    // 6 + 7 + 8 + 9 + 9 =39

    }


    So, the preincrement operator works first and then ant other task..... from L to R


    regards,
    SVS.Aditya


    •    Sponsored Ads



Latest Article

Network Security Risk Assessment and Measurement

Read More...