Hi a strange code ....

D

dhaval.bhanu

Hi ..

Well I have a strange code here which i am not usre have a clear
understanding.

I have compiled it on turbo C++3.0 compiler and Devc++ compiler ..


here is the code ..


int main(void)
{
int i=0;,j=10,k=20;
if(i,j,k)
printf("welcome");
else
printf(" Home");
return 0;
}

the output is welcome.


What i found out after a little experimenting is i,j,k taking
individually as expressions are evaluated .. where as k is used to
evaluate the if condition ....

Let me know if iam wrong ..
 
P

pibru

Hi ..

Well I have a strange code here which i am not usre have a clear
understanding.

I have compiled it on turbo C++3.0 compiler and Devc++ compiler ..


here is the code ..


int main(void)
{
int i=0;,j=10,k=20;
if(i,j,k)
printf("welcome");
else
printf(" Home");
return 0;
}

the output is welcome.


What i found out after a little experimenting is i,j,k taking
individually as expressions are evaluated .. where as k is used to
evaluate the if condition ....

Let me know if iam wrong ..
<
I think it is correct... the result of (i,j,k) is k.... you can see it with
#include <stdio.h>
int main()
{
int i=(2,3,7);
printf("%d\n",i);
}
 
C

Christopher Benson-Manica

if(i,j,k)
printf("welcome");
What i found out after a little experimenting is i,j,k taking
individually as expressions are evaluated .. where as k is used to
evaluate the if condition ....

Look up the comma operator in your friendly C reference. Much shall
thus be made clear to you.
 
B

Ben Pfaff

if(i,j,k)

This is the "comma operator". The comma operator evaluates its
left-hand operand, then its right-hand operand. Its result is
the value of the right-hand operand. It is most often used in
the expressions in a "for" statement.

If the left-hand operand to a comma operator has no side effect,
as here, then there's no point to doing it at all. When I was
new to the C language, this first surprised me when I was trying
to access a multidimensional array element: a[1,2] is quite
different from a[1][2].
 
K

Knemon

Hi ..

Well I have a strange code here which i am not usre have a clear
understanding.

I have compiled it on turbo C++3.0 compiler and Devc++ compiler ..


here is the code ..


int main(void)
{
int i=0;,j=10,k=20;
if(i,j,k)
printf("welcome");
else
printf(" Home");
return 0;
}

the output is welcome.

Of course it is: k != 0.
 

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
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top