| | |
i++ and ++i
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
that is simple
++i means pre increment.
i++ means post increment.
consider the programme
..
main()
{
int a=7;
printf("%d\t%d\t%d\t%d\t",++a,a++,++a,a++);
printf("%d\n"a);
getch();
}
the ot put is
10 10 8 8
11
a++ here increments the value and shows it but ++a increments the value and gives it for next operation.ie ++a increments but not show the value.
++i means pre increment.
i++ means post increment.
consider the programme
..
main()
{
int a=7;
printf("%d\t%d\t%d\t%d\t",++a,a++,++a,a++);
printf("%d\n"a);
getch();
}
the ot put is
10 10 8 8
11
a++ here increments the value and shows it but ++a increments the value and gives it for next operation.ie ++a increments but not show the value.
•
•
Join Date: Aug 2004
Posts: 7
Reputation:
Solved Threads: 0
if you are using void main()function at the beginning do not use return 0 at the end.
if you use int main()function at the startup use return 0 at the the end..
#include<iostream.h>
int main()
{
int x=5;
cout<<x<<endl; //print 5
cout<<x++<<endl;// the 5 is print first .then the 1 is added
cout<<x<<endl;//the x get 6 becoz 1 is been added
}
----------------------------------
2 program-
#include<iostream.h>
int main()
{
int x=5;
cout<<x<<endl; //print 5
cout<<++x<<endl; //first add 1 and add 5 to it.so the answer is 6
cout<<x<<endl; //the variable now is 6
return 0;
}
if you use int main()function at the startup use return 0 at the the end..
#include<iostream.h>
int main()
{
int x=5;
cout<<x<<endl; //print 5
cout<<x++<<endl;// the 5 is print first .then the 1 is added
cout<<x<<endl;//the x get 6 becoz 1 is been added
}
----------------------------------
2 program-
#include<iostream.h>
int main()
{
int x=5;
cout<<x<<endl; //print 5
cout<<++x<<endl; //first add 1 and add 5 to it.so the answer is 6
cout<<x<<endl; //the variable now is 6
return 0;
}
•
•
•
•
Originally Posted by let us c
that is simple
++i means pre increment.
i++ means post increment.
consider the programme
..
main()
{
int a=7;
printf("%d\t%d\t%d\t%d\t",++a,a++,++a,a++);
printf("%d\n"a);
getch();
}
the ot put is
10 10 8 8
11
a++ here increments the value and shows it but ++a increments the value and gives it for next operation.ie ++a increments but not show the value.
http://www.eskimo.com/~scs/C-faq/q3.2.html
•
•
•
•
Originally Posted by kalinga
if you are using void main()function at the beginning do not use return 0 at the end.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
•
•
•
•
Originally Posted by cscgal
Can someone please explain to me the difference between ++i and i++ when written as part of an expression (i.e. within loops and if statements) ? Thanks
Covers tiny stuff like performance details and the new C++ standard very well,bit advanced but very good if you want details.
Postfix(p++) and Perfix(++p).
Rules
p++ - First use value then increment.
++p - First increment then use value.
If you still dint understand after all the stuff the other guys gave.Then check this out.
You are mad at a guy, he shouts at you,making you madder.With p++ you shout at the guy then slap him and with ++p you slap him first, then shout at him.
The end result is that on the first case he would have got a piece of your mind before getting the physical part for what ever he did, and the other way around with the second.
Which method you want to use depends on the situation and your choice.
(Hint: p++, if you plan on running)
,no offense anyone.Also,val++ (real c++ here ok) is what is commonly used by many programmers.
As far as loops go it depends on which type of loop you re going to use like
for(), while() , and do while() ,where it's declared and where the expression is placed.Just apply the rules and you will have the result.
•
•
•
•
Originally Posted by cscgal
Can someone please explain to me the difference between ++i and i++ when written as part of an expression (i.e. within loops and if statements) ? Thanks
and i++ first does the computation and then increments i.
•
•
Join Date: Aug 2004
Posts: 24
Reputation:
Solved Threads: 0
try this:
//---------------------------------
// try_i.cpp
#include <iostream.h>
void main()
{
int i = 1;
int last_value;
last_value = i++;
cout << "i++ = " << i << endl;
last_value = ++i;
cout << "++i = " << i << endl;
cout << "Again:" << endl;
cout << "i++ = " << i++ << endl;
cout << "++i = " << ++i << endl;
}
//---------------------------------
// try_i.cpp
#include <iostream.h>
void main()
{
int i = 1;
int last_value;
last_value = i++;
cout << "i++ = " << i << endl;
last_value = ++i;
cout << "++i = " << i << endl;
cout << "Again:" << endl;
cout << "i++ = " << i++ << endl;
cout << "++i = " << ++i << endl;
}
•
•
Join Date: Aug 2004
Posts: 2
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by cscgal
Can someone please explain to me the difference between ++i and i++ when written as part of an expression (i.e. within loops and if statements) ? Thanks
++i means first increement and then process the instruction
i++ means first process the instruction and then increment.
![]() |
Other Threads in the C Forum
- Previous Thread: Compiling Multiple files in a single Project
- Next Thread: Information on function pointers
| Thread Tools | Search this Thread |
Tag cloud for C
#include * append array arrays asterisks binarysearch calculate changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc database directory dynamic execv feet fgets file fork forloop framework function givemetehcodez grade graphics gtkwinlinux hacking histogram homework inches include incrementoperators input intmain() iso kernel keyboard km lazy license linked linkedlist linux list lists locate logical_drives looping loopinsideloop. lowest matrix microsoft motherboard mqqueue number oddnumber odf opendocumentformat opensource overwrite owf pdf performance pointer posix problem probleminc process program programming radix recursion recv recvblocked research reversing scanf scripting segmentationfault sequential socket socketprograming spoonfeeding standard string student systemcall testing threads turboc unix user variable wab whythiscodecausesegmentationfault windowsapi







