newbie question

J

junky_fellow

i have a short C program as follows:

main()
{

int i=0;
i = ++i;
printf("\ni=%d",i);
}

when i compile the program, i get the following warning.
cc: Warning: test.c, line 6: In this statement, the expression "i=++i"
modifies the variable "i" more than once without an intervening
sequence point.
This behavior is undefined.
i = ++i;
^

I don't what causes the undefined bahaviour. For me the variable i
should
be incremented by 1. Also when i run the executable the value for i
printed
is 1.
 
M

Miika Karanki

junky_fellow said:
main()
{

int i=0;
i = ++i;
printf("\ni=%d",i);
}

when i compile the program, i get the following warning.
cc: Warning: test.c, line 6: In this statement, the expression "i=++i"
modifies the variable "i" more than once without an intervening
sequence point.
This behavior is undefined.
i = ++i;
^

int main()
{
int i=0;
++i; /* or i = i+1 or i+=1 (or i++) */
printf("\ni=%d", i); /* or printf("\ni=%d", ++i) without
the previous line */
return 0;
}

++i increments the value of i by one, you shouldn't assign its
result back to i.

++i is not the same as "i+1" which doesn't increment the
variable i, it just returns the result of calculation
"i plus one".
 

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,139
Messages
2,570,806
Members
47,352
Latest member
Maricruz09

Latest Threads

Top