simple c++ program

Status
Not open for further replies.

nikhil ramteke

s,b+..u cn..
watch the following code:-

#include<conio.h>
#include<iostream.h>
int main()
{
int c=0,a=0,b=0;
clrscr();
cout<<"c="<<c++<<"\n";
cout<<"c="<<c<<"\n";
b=a++ +c++ +c++;
cout<<"a="<<a<<"\n";
cout<<"b="<<b <<"\n";
cout<<"c="<<c;
getch();
return 0;
}




o/p:-
c=0
c=1
a=1
b=2
c=3

plz tell about the last two values....
thanks in adavance
 

QwertyManiac

Commander in Chief
Code:
#include<conio.h>
#include<iostream.h>
int main()
{
int c=0,a=0,b=0;
clrscr();
cout<<"c="<<c++<<"\n";
[B] /*c's printed first as 0
and then incremented to 1*/[/B]
cout<<"c="<<c<<"\n"; 
[B] //current c = 1 is print[/B]
b=a++ +c++ +c++; 
[B] /*b = 0 + 1 + 1 
(Initial values of a and c are added and stored in b), 
and then a is incremented once, and c twice*/[/B]
cout<<"a="<<a<<"\n"; 
[B] //incremented a = 1 is print[/B]
cout<<"b="<<b <<"\n"; 
[B] //calculated b=2 is print[/B]
cout<<"c="<<c; 
[B] //twice incremented c=(1) + 1 +1 = 3 is print[/B]
getch();
return 0;
}
[B]//All increments are post increments and hence
//incremented only after the 
//expression its called in
//is executed[/B]
 

casanova

The Frozen Nova
#include<conio.h>
#include<iostream.h>
int main()
{
int c=0,a=0,b=0;
clrscr();
a=b=c=0

cout<<"c="<<c++<<"\n";
C is post incremented. Prints 0 and c becomes 1


cout<<"c="<<c<<"\n";
c=1

b=a++ +c++ +c++;
cout<<"a="<<a<<"\n";
cout<<"b="<<b <<"\n";
cout<<"c="<<c;
getch();
return 0;
}

b=a++ +c++ +c++;
b= 0++ + 1++ + 1++
b=0+1+1 //a =1 c= 3 as a is postincremented once and c twice
b=2
a=1
c=3

Hope this helps :)
 
OP
nikhil ramteke

nikhil ramteke

s,b+..u cn..
thanx frnz.....it ws the concept of post increment,,,,it will help me in solving technical interviews of my campus...thanks alot.....:)
 
Status
Not open for further replies.
Top Bottom