use of ++i, i++

G

Greenhorn

hi,
From reading some guides i understood that prefix, postfix operate
this way, former executes the increment right away, latter executes the
increment after the immediately after the immediately following end of
statement. Is that correct ?

If i go by that rule, the below two statements are equivalent

for(i=0;i < 1;i++)
printf("The first value printed for i is %d", i);

for(i=0;i < 1;++i)
printf("The first value printed for i is %d", i);

The outputs are same here as printf(...) statement above is the
immediately following end of the first statement after the 'increment
operator(s)'

Thanks in advance.

greenhorn.
 
B

Ben Pfaff

Greenhorn said:
From reading some guides i understood that prefix, postfix operate
this way, former executes the increment right away, latter executes the
increment after the immediately after the immediately following end of
statement. Is that correct ?

Actually, it is unspecified when the increment occurs (except
that it occurs between the previous and next sequence point).
The actual difference is that the prefix increment operator
yields the modified value whereas the postfix increment operator
yields the original value.
If i go by that rule, the below two statements are equivalent

for(i=0;i < 1;i++)
printf("The first value printed for i is %d", i);

for(i=0;i < 1;++i)
printf("The first value printed for i is %d", i);

Yes, these are equivalent. (I would want a new-line in my own
printf output though, and I would not use a 1-iteration loop.)
 
G

Gordon Burditt

From reading some guides i understood that prefix, postfix operate
this way, former executes the increment right away, latter executes the
increment after the immediately after the immediately following end of
statement. Is that correct ?

No.

The value returned from a postfix increment is the value before the
increment. The value returned from a prefix increment is the value
after the increment. If the value is not used, there is no difference
between the two.

Nothing is guaranteed about the order of incrementing, returning a
value, and storing the modified result back into the variable.
If i go by that rule, the below two statements are equivalent

for(i=0;i < 1;i++)
printf("The first value printed for i is %d", i);

for(i=0;i < 1;++i)
printf("The first value printed for i is %d", i);

Since the value is not used, there is no difference between the two.

Gordon L. Burditt
 
R

Richard Cavell

hi,
From reading some guides i understood that prefix, postfix operate
this way, former executes the increment right away, latter executes the
increment after the immediately after the immediately following end of
statement. Is that correct ?

If i go by that rule, the below two statements are equivalent

for(i=0;i < 1;i++)
printf("The first value printed for i is %d", i);

for(i=0;i < 1;++i)
printf("The first value printed for i is %d", i);

Yes, but consider this:

int a = ++i;
int b = i++;

And also consider what happens when i is not an integer. i can be any
given class, which might do anything at all.
 
D

DHOLLINGSWORTH2

Greenhorn said:
hi,
From reading some guides i understood that prefix, postfix operate
this way, former executes the increment right away, latter executes the
increment after the immediately after the immediately following end of
statement. Is that correct ?

If i go by that rule, the below two statements are equivalent

for(i=0;i < 1;i++)
printf("The first value printed for i is %d", i);

for(i=0;i < 1;++i)
printf("The first value printed for i is %d", i);

The outputs are same here as printf(...) statement above is the
immediately following end of the first statement after the 'increment
operator(s)'

Thanks in advance.

greenhorn.
The "immediate" value passed for i is what your talking about. --i, passes
i, as (i0-1), where i--, passes i, as i0;

change your code to :

for(i=0;i < 1; printf("The first value printed for i is %d", i++) );

for(i=0;i < 1; printf("The first value printed for i is %d", ++i));

to see the difference.
 
M

Mac

Yes, but consider this:

int a = ++i;
int b = i++;

And also consider what happens when i is not an integer. i can be any
given class, which might do anything at all.


What do you mean by "class," above? You aren't talking about c++ classes
in a c newsgroup, are you?

--Mac
 
G

Greenhorn

Probably, the example i took is a bad one since in for statement the
i++, ++i come into play only after the execution of the printf() as can
be observed in the following example with the values give for j

main()
{
int i = 0,j = 0,k,l;

for(i=0; i < 1;j = i++)
printf("The first value printed for i is %d, j is %d \n", i,j);

for(i=0; i < 1; j = ++i)
printf("The first value printed for i is %d, j is %d \n", i,j);

getch();
}

perhaps the below is better example
----------------------
main()
{
int i = 0,j = 0,k,l;

for(i=0; i++ < 1; i++)
printf("The first value printed for i is %d, \n", i);

for(i=0; ++i < 1; ++i)
printf("The first value printed for i is %d \n", i);

getch();
}

------------------or more simply as stated by you to have i++, ++i in
printf()


--------

main()
{
int i = 0,j = 0,k,l;
sample(++i);
i = 0;
sample(i++);
printf("The post increment is done exactly after the execution of
current statment as shown by the value of %d", i);
getch();
}

void sample(int k)
{
printf("The value of i is %d \n \n", k);
return 0;
}
 
M

Mark McIntyre

i++ complied to
int tmp = i;
i = i + 1;
return tmp;


++i complied to
i = i + 1;
return i;

Is that true?

No. Yes. Maybe. Sometimes.

Its entirely dependent on your compiler, the optimization settings and on
what you're doing with i.
 
E

E. Robert Tisdale

Mac said:
What do you mean by "class," above?
You aren't talking about c++ classes in a c newsgroup, are you?

It is a good habit to use the prefix instead of the postfix operator
in C code as well as C++ code if, like most C programmers,
you sometimes write C++ code as well as C code.
 
D

DHOLLINGSWORTH2

In most cases the "Actual Code Generated" would be like machine code.

And in fact the INC operation is in effect whether you Prefix, or postfix.

In Post fix A new data element is needed to return the value immediate
before the inc. The new data is then destroyed. such as ( somefunc(
val++); )
 
C

Chris Dollin

LLL said:
i++ complied to
int tmp = i;
i = i + 1;
return tmp;


++i complied to
i = i + 1;
return i;

Is that true?

No.

`i++` compiles to something that delivers the current
value of `i` and arranges that `i` will be incremented
before the next sequence point.

`++i` compiles to something that delivers `i+1` and
arranges that that value is written back to `i` before
the next sequence point.

The write-back doesn't have to happen immediately.
 
G

grid

`i++` compiles to something that delivers the current
value of `i` and arranges that `i` will be incremented
before the next sequence point.

`++i` compiles to something that delivers `i+1` and
arranges that that value is written back to `i` before
the next sequence point.

Why is ++i always faster than i++ ? Is it because i++ will have to store
the initial value of i in a temporary memory location.

TIA
 
R

Richard Bos

grid said:
Why is ++i always faster than i++ ?

Who says it is? It may be with C++-specific features in C++, but there
is nothing in C that should lead you to think that either might be
faster than the other, let alone always.

Richard
 
E

E. Robert Tisdale

grid said:
Why is ++i always faster than i++?

It isn't always faster.
Is it because i++ will have to store
the initial value of i in a temporary memory location?

There is no such requirement
in a for loop increment of an [unsigned] int:
> cat f0.c
void f(const unsigned int n, int a[n]) {
for (int unsigned j = 0; j < n; ++j)
a[j] = j;
}
> gcc -Wall -std=c99 -pedantic -S f0.c
> cat f1.c
void f(const unsigned int n, int a[n]) {
for (int unsigned j = 0; j < n; j++)
a[j] = j;
}
> gcc -Wall -std=c99 -pedantic -S f1.c
> diff f0.s f1.s
1c1
< .file "f0.c"
---
> .file "f1.c"

As you can plainly see,
the compiler emits exacly the same [assembler] code
for both preincrement and postincrement operators
on an [unsigned] int iterator in a for loop.
 
M

Mark McIntyre

And in fact the INC operation is in effect whether you Prefix, or postfix.

and what about on hardware that has a prefix increment operator, or more
efficient ways to add one to a value? Hint: the way that hardware
implements ++ is not defined by C.
In Post fix A new data element is needed to return the value immediate
before the inc. The new data is then destroyed. such as ( somefunc(
val++); )

This is only a half truth. As I said, it totally depends on how you're
using the value. Please think more carefully.
 
V

Villy Kruse

and what about on hardware that has a prefix increment operator, or more
efficient ways to add one to a value? Hint: the way that hardware
implements ++ is not defined by C.

You might even find that ++i; i++; i += 1; i = i + 1; will produce the
exact same opcode. This is very simple for the compiler's peephole
optimizer. A special case would be *i++ on MC68000 type hardware, where
the dereferencing and postincrement can be done within one machine
instruction.


Villy
 
D

DHOLLINGSWORTH2

i believe the INCrement [Emmediate ] does the same thing as the Moto machine
 
K

Keith Thompson

DHOLLINGSWORTH2 said:
i believe the INCrement [Emmediate ] does the same thing as the Moto machine

Please don't top-post. Your response should follow, or be
interspersed with, any quoted text.

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,160
Messages
2,570,889
Members
47,421
Latest member
StacyTaver

Latest Threads

Top