hi,
I am having some confusions regarding the following lines of code:
Code:
#include <stdio.h>
int main(void) {
int i;
i = 5;
printf("%d%d%d\n", i, ++i, i++);
}
It outputs : 766. Could you explain why ?
Best,
Thu Mar 25, 04 7:45 am
dinangkur Super Moderator
Joined: 24 Mar 2004 Posts: 491
Location: Dhaka, Bangladesh
Post subject: Solution
/*If you want to understand your output you've to understand increment and decrment operator
in C. How it works? How the varible works in both situation.
Let me try if I can clear it to you. The increment and decrement operators don't need to follow
the variable, they can precede it. Although the effect on the variable is the same, the position
of the operator does affect the operation.
Let's see another example:*/
#include<stdio.h>
int main(void)
{
int i,j;
i = 5;
j = i++;
/*This will print 6 5*/
printf("i and j:%d %d %d",i,j);
return 0;
}
/*This statement works like this:first the current value of i assigned to j then i is incremented.
When the increment or decrement operator follows the variable, the operation is performed after
its value has been obtained for use in the expression. Now if the variable is preceded by the
increment or decrement operator, the opeartion is performed first, and then the value of the
variable is obtained for use in the expression.*/
/*Rewrite the following program*/
#include<stdio.h>
int main(void)
{
int i,j;
i = 5;
j = ++i;
/*This will print 6 6*/
printf("i and j: %d %d",i,j);
return 0;
}
/*For your kind information, you stated wrong output. Check with your computer, that should be
665 not 766. If your program still shows 766 then you'd call pabna mental, your processor
need shock therapy
Happy Hacking.
-DK.*/
Thu Mar 25, 04 12:42 pm
Belal User
Joined: 08 Mar 2004 Posts: 84
Location: Dhaka, Bangladesh
Post subject:
hi Tousif,
i think ur code is sufficient to make u understand the nature of incremental operator. u should print three values(i,++i,i++) in different line an u can see the difference.
in fact, ++ operator before any variable does it's operation in the same statement where it is being used, on the otherhand, ++ operator after any variable does it's operation in next statement not where it is being used.
so, when u write the command "printf("%d%d%d\n", i, ++i, i++); ", i++ changed it's value 5 to 6 but not printed in this line, whereas ++i changed it's value 5 to 6 and printed too and finally i gets the value 6, so u should have get 665.
u can write following two lines individually and check the result---
printf("%d%d\n", i++,i);
or, printf("%d%d\n", ++i,i);
see, c++ gets the values frm right to left and then print them frm left to right. Just this is the secret, nothing to fear........[u result should b 665 not 766]
thanks
Fri Mar 26, 04 3:25 am
Guest
Post subject:
Hi guys,
First, thanks for the reply. I already know how the ++ operator works. However, I don't think you guys really understood the problem. It's a bit tricky and has to do with how the printf function is being implemented.
For Digankur, your immaturity is clearly obvious, for you failed to even understand the subtlety and yet you are making fun.
I don't know what system are you guys running for I am using Solaris on a Sun PC running SunOS 5.9. And I get 766. Here is the output.
Code:
tardis-b01:~/misc>cat temp1.c
#include <stdio.h>
int main(void) {
int i;
i = 5;
printf("%d%d%d\n", i, ++i, i++);
}
tardis-b01:~/misc>gcc temp1.c
tardis-b01:~/misc>./a.out
766
very simple nothing special about it.
Well, now concerning the output you guys are saying(665), it completely makes no sense - How could it be ? That means you guys are saying the value of i to be 6 ? If printf scans things serially and assigns value immediately then the value of i should be 5, or if it assigns value after evaluation at the end then it should be 7 but it can never be 6. Because you increment the value of i twice
Consider this code:
Code:
tardis-b01:~/misc>cat temp1.c
#include <stdio.h>
int main(void) {
int i,j;
i = 5;
printf("%d%d%d%d%d\n", i, i++, j = i, i, i++);
// printf("%d%d%d\n", i, ++i, i++);
}
tardis-b01:~/misc>gcc temp1.c
tardis-b01:~/misc>./a.out
75676
tardis-b01:~/misc>
Interesting, ha ?
I was just trying to understand why printf produces such output.
Here is what I think:
First printf scans the argument and evaluates the varuables value in a LIFO manner. After that it goes on to print it (while printing it checks the time-stamp on each variable and prints the latest one)... since the time stamp for the variable (j, i++) didn't change, it prints them as it evaluated them before..but for i, since it was accessed and modified (by i++ and ++i), that invalidates the previous value assined to it and thus it prints the new value.
Thats the explanation I could think of right now. Any "constructive" suggestions or comments would be appreciated.
Cheers,
/Tousif
Fri Mar 26, 04 8:20 am
dinangkur Super Moderator
Joined: 24 Mar 2004 Posts: 491
Location: Dhaka, Bangladesh
Post subject:
It seems like you're offering me a moronic award before trying it on different complier and understand C standard.
Code:
[dkundu@ns misc]$ cat test.c
#include<stdio.h>
int main(void)
{
int i;
i = 5;
printf("%d%d%d\n",i,++i,i++);
return 0;
}
[dkundu@ns misc]$ ./a.out
775
[dkundu@ns misc]$ gcc -v
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/3.3.2/specs Configured with: ../configure --prefix=/usr --mandir=/usr/share/man
--infodir=/usr/share/info --enable-shared --enable-threads=posix --disable-checking --with-system-zlib --enable-__cxa_atexit
--host=i386-redhat-linux Thread model: posix gcc version 3.3.2 20031022 (Red Hat Linux 3.3.2-1)
[dkundu@ns misc]$
Thats I get on my Linux PC(Fedora Core 1). And on VC complier 665. Not 766. But Your question was wrong.
because that is undefined behaviour. it basicly means your code is wrong lol. it is a piece of code not defined by the c standard, it could for instnace just print "please review your code" instead of printing any numbers at all.
printf prints the values of the vars at the current sequence point, that about it... those values are just set differently by every compiler in that case because its not defined what values they should have.
-DK.
Fri Mar 26, 04 1:04 pm
dude Power User
Joined: 10 Mar 2004 Posts: 376
Location: Savar, Dhaka
Post subject: Weird Output
Hi Guys,
I agree with DK. It certainly is undefined. Also could be a problem with the compiler and OS version?
The value could actually be 766 if the Compiler first computes from the right. In that case i++ would make i=6. ++i would make i=7. So now the compiler goes to print it from the left. i=7, ++i=6, i++=6.
It is some weird way to behave indeed. :roll:
Sat Mar 27, 04 3:39 am
dinangkur Super Moderator
Joined: 24 Mar 2004 Posts: 491
Location: Dhaka, Bangladesh
Post subject: No problem with compiler
C programming defined with a standard. If we want right output of a problem domain, we've to code according to standard. So, compiler can understand and process the math in proper way. Compiler is not a human being. e.g. We can goto Hatirpul in several way, but if you give complier 2 legs and ask it to go hatirpul, it will go according to define road which is set into its core. It can't choose the way dynamically. At that point AI comes. So, correct code = correct output. :)