943,770 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 11806
  • C RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Aug 26th, 2004
0

Re: i++ and ++i

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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
let us c is offline Offline
17 posts
since Aug 2004
Aug 26th, 2004
0

Re: i++ and ++i

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;
}
Reputation Points: 11
Solved Threads: 0
Newbie Poster
kalinga is offline Offline
7 posts
since Aug 2004
Aug 26th, 2004
0

Re: i++ and ++i

Quote 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.
You picked the worst example you could -- it is purely undefined behavior.
http://www.eskimo.com/~scs/C-faq/q3.2.html

Quote originally posted by kalinga ...
if you are using void main()function at the beginning do not use return 0 at the end.
If you are using void main(), and are on a hosted implementation, you are incorrect. (It seems to me I may have mentioned this already?)
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Aug 26th, 2004
0

Re: i++ and ++i

Quote 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
Could I suggest a book?: C++ Primer 3rd Edition

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.
Reputation Points: 108
Solved Threads: 7
Posting Whiz in Training
FireNet is offline Offline
256 posts
since May 2004
Aug 27th, 2004
0

Re: i++ and ++i

Quote 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
Simply ++i increments i first and then does any computation in the loop ...
and i++ first does the computation and then increments i.
Team Colleague
Reputation Points: 45
Solved Threads: 56
Unauthenticated Liar
nanosani is offline Offline
1,767 posts
since Jul 2004
Aug 27th, 2004
0

Re: i++ and ++i

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;
}
Reputation Points: 15
Solved Threads: 0
Newbie Poster
XianBin is offline Offline
24 posts
since Aug 2004
Aug 29th, 2004
0

Re: i++ and ++i

Quote 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
The functionality of both the syntexes are same but difference lies in preference of operators.
++i means first increement and then process the instruction
i++ means first process the instruction and then increment.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
varun_sonkerr is offline Offline
2 posts
since Aug 2004
Aug 29th, 2004
0

Re: i++ and ++i

practical is the key to understand the concept in a better way.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
varun_sonkerr is offline Offline
2 posts
since Aug 2004
Sep 30th, 2007
0

Re: i++ and ++i

One thing that is good to remember is that ++i is returned by reference while i++ is returned by value.

You should use ++i to increment where the old value of i is not needed.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Wonder is offline Offline
1 posts
since Sep 2007
Sep 30th, 2007
0

Re: i++ and ++i

Click to Expand / Collapse  Quote originally posted by Wonder ...
One thing that is good to remember is that ++i is returned by reference while i++ is returned by value.
Never heard that one before.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in C Forum Timeline: Switch Statement & char loop
Next Thread in C Forum Timeline: hello;





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC