DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C (http://www.daniweb.com/forums/forum118.html)
-   -   i++ and ++i (http://www.daniweb.com/forums/thread9362.html)

let us c Aug 26th, 2004 9:07 am
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.

kalinga Aug 26th, 2004 9:40 am
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;
}

Dave Sinkula Aug 26th, 2004 11:25 am
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?)

FireNet Aug 26th, 2004 1:51 pm
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.

nanosani Aug 27th, 2004 4:54 am
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.

XianBin Aug 27th, 2004 3:23 pm
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;
}

varun_sonkerr Aug 29th, 2004 6:06 am
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.
:cool:

varun_sonkerr Aug 29th, 2004 6:08 am
Re: i++ and ++i
 
practical is the key to understand the concept in a better way.

Wonder Sep 30th, 2007 4:52 pm
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.

Ancient Dragon Sep 30th, 2007 9:22 pm
Re: i++ and ++i
 
Quote:

Originally Posted by Wonder (Post 443302)
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.:-O


All times are GMT -4. The time now is 5:21 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC