What will be the output of the following program in UNIX OS with CC compiler and TC compiler?int main(){ int i=5; printf("\n%d",++i + ++i + ++i + ++i + ++i );} If any difference then Why it is difference?

Questions by sachinp1979   answers by sachinp1979

Showing Answers 1 - 51 of 51 Answers

AJAY

  • Feb 22nd, 2006
 

CORRECT 10

  Was this answer useful?  Yes

sashank

  • Feb 22nd, 2006
 

this gave me an answer of 41 on unix box , i dont know how :(

  Was this answer useful?  Yes

Rohit Sachan

  • Feb 23rd, 2006
 

its 40 because6+7+8+9+10

Rabindra Nath Mandal

  • Feb 24th, 2006
 


  Output:  41.


  For different compiler ther will be same output.

  Expression will be evaluated in following manner.
  (((++i + ++i) + ++i) + ++i) + ++i
       6
          7
     7 +  7 = 14
              8
   14  +   8 = 22
                 9
      22 + 9 = 31
                   10
       31 + 10 = 41.

  Was this answer useful?  Yes

sucheta

  • Feb 26th, 2006
 

firstly in preincrement i value will be increased evry iteration and then the final value of i will be assigned to each iteration. that isfor first ++i , i will become six and then for next ++i , i value will become 7 and then finally the end value of i vil be 10. then in the program ++i + ++i + ++i + ++i + ++i 10 + 10 + 10 + 10 + 10 =50 so the final answer will be 50

  Was this answer useful?  Yes

Richa Yadav

  • Feb 26th, 2006
 

its 40 because 10+9+8+7+6.

  Was this answer useful?  Yes

vssunil

  • Feb 28th, 2006
 

ya!the correct answer is 40... 6+7+8+9+10.

  Was this answer useful?  Yes

sam

  • Mar 1st, 2006
 

Ans i got is 41

  Was this answer useful?  Yes

Anil Saini

  • Mar 3rd, 2006
 

I executed this program in turboc++3 complier.And I got the answer 40.

So, the Expression will be evaluated in following manner.
  (((++i + ++i) + ++i) + ++i) + ++i
       6  +   7   +  8    +   9   + 10=40.
    

shailesh

  • Mar 5th, 2006
 

Hi I checked it in both UNIX OS with CC compiler and In Windows with TC compiler, and I got 41 for UNIX and 40 for TC compiler. In UNIX compiler we are getting 41 for it the reason stated by "Rabindra Nath Mandal" it totally correct and for TC compiler we are getting 40 because of 6+7+8+9+10. So both compiler give different results.

  Was this answer useful?  Yes

pooja

  • Mar 8th, 2006
 

Output : 41

  Was this answer useful?  Yes

pramod kumar ravula

  • Mar 9th, 2006
 

printf("%d",++i+ ++i + ++i+ ++i);

           ((++i)+ (++i )+(++i) +(++i)  )

                                      <-----  ++ 

           +----->

              10+9+8+7+6       

it will print 40  becouse

1.priority of the unary operator (++) is > priority(+)

2.unary (++ )is right associative ,binary (+) is left associative 

3.unary ++ here we used is pre incrementor so it will return the values after incrementing only that means first we r sending 5 as in put to ++i it will return 6 as return value  

  Was this answer useful?  Yes

haripanda

  • Apr 17th, 2006
 

its answar is 10+9+8+7+6=40

since it will excted from right toleft ;

as all are pree increment statements

so the answar is 40.in case of turbocompiler

the answar  will vary from compiler to compiler;

its totaly compiler dependent

  Was this answer useful?  Yes

Subikar Burman

  • May 2nd, 2006
 

Output will be 40 but the value of i should be inthis way 10+9+8+7+6

  Was this answer useful?  Yes

Atul

  • May 16th, 2006
 

The output for above program is

in Unix(solaris) environment  is 41

and window environment is 40

  Was this answer useful?  Yes

Sunil Rohilla

  • May 19th, 2006
 

The output of the program should b 50;

that is b'caz, first of all the preincrements in i will be completed.

given i=5;

after all increments i will be 10

so 10+10+10+10+10=50 will get printed.

plz correct if any mistake done

 

Amol.

  • Jun 13th, 2006
 

According to me the expression involves the side effect. Check link http://c-faq.com/expr/ieqiplusplus.htmlcorrect me if ANSI standard answers this question.

  Was this answer useful?  Yes

neverd

  • Jun 11th, 2007
 

I tried with VC++ 6.0,

int _tmain(int argc, _TCHAR* argv[])
{
    int i=5;
    printf("n%d",++i + ++i + ++i + ++i + ++i );

    getchar();
    return 0;
}

Ans is 50,

  Was this answer useful?  Yes

megeek

  • Apr 23rd, 2008
 

Those who are getting answer as 40, i agree with them and on checking its correct atleast in C

but
the proper method of computing it is as follows

               ++i + ++i + ++i + ++i + ++i
exec statrs from
the right most variable by the compiler
so its                     6
                         +
                    7
               +
           8
        +
     9
   +
10
and hence answer is 40

and you can check the 'i' value at the end
It Is e= 10.

  Was this answer useful?  Yes

in TC compiler the output is 40
in unix compiler the output is 41

in unix calculation goes left to right
in TC it goes to right to left

in unix
the caluclation is evaluated in the following form
(((++i + ++i) + ++i) + ++i) + ++i
       6
          7
     7 +  7 = 14
              8
   14  +   8 = 22
                 9
      22 + 9 = 31
                   10
       31 + 10 = 41.

in TC the calculation  is
(++i + (++i + (++i + (++i + ++i)))
  10       9       8      7       6
ans=40

  Was this answer useful?  Yes

kernel32

  • May 22nd, 2008
 

i compiled with gcc and found result to be 41
                   ++i  + ++i  + ++i + ++i + ++i
===>       [ 7 + 7 = 14 ]
========> [ 14 + 8 = 22]
=============> [ 22 + 9 = 31]
==================> [31 + 10 = 41]

  Was this answer useful?  Yes

paragmalshe

  • May 29th, 2008
 

Undefined behavior
output may vary from compiler to compiler
value of a variable is getting modified more than once between 2 sequence points
http://en.wikipedia.org/wiki/Sequence_point

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions