Solved

# IF case explanation and reference please

Posted on 2003-03-24
Medium Priority
214 Views
Hi

I have the following piece of code

if( a=func(), a )

I know what it does but I cannot find
any references in the litterature
to how valid the statement is and
why a , is used (any special meaning?)

K&R references if possible thanks
0
Question by:CrypToniC
• 4
• 3
• 3
• +2

LVL 30

Accepted Solution

Mayank S earned 256 total points
ID: 8194727
a = func () assigns the value returned by func () to a and then the expression evaluates to that value since you have ( a = func (), a ). It is same as doing:

if ( a = func () )

and the if statement will execute if func () does not return 0.

However, if you'd written:

if ( a = func (), 0 )

then you'll notice that it won't go inside the if statement (because it ultimately evaluates to 0, no matter what value func () returns).

Hope that much helps!

Mayank.
0

LVL 11

Assisted Solution

dimitry earned 248 total points
ID: 8194830
Actually if( a = func(), a ) is the same as
if( a = func(), a != 0 ) and therefore same as
if( a = func() )
In C you can write any number of expressions separated
by ',' and result will be result of last expression.
It is usually used in for() loop:
for(i=0,sum=0;( i < n );i++)
sum += a[i];

In Ansi-C grammer definition you can find:
http://www.lysator.liu.se/c/ANSI-C-grammar-y.html

expression
: assignment_expression
| expression ',' assignment_expression
;
0

LVL 48

Assisted Solution

AlexFM earned 248 total points
ID: 8194948
The following program:

int main(int argc, char* argv[])
{
int x, y;

y = (x = 2, x = 3);

cout << "x = ", cout << x << "\n", cout << "y = ", cout << y << "\n";

return 0;
}

prints:

x = 3
y = 3

Operators delimited with "," are executed from left to right; result of last operator is returned.

if( a=func(), a )

is the same as

if( a=func() )

0

LVL 30

Expert Comment

ID: 8200236
Alex.... this is the C programming section, buddy! Pls don't use cout <<....

Cheers,

Mayank.
0

LVL 48

Expert Comment

ID: 8200630
I am really sorry. Please don't tell to Computer101.
0

LVL 1

Assisted Solution

vamshi_pavan earned 248 total points
ID: 8201629
if( a=func(), a )

A comma operator is used to string several operations together.
In the above code a=func() gets evaluated first because '=' operator has higher precedence than comma operator','.
In expressions that use the ',' operator the value of the right most expression is taken as the final value of the expression Therefore in the above code
if( a=func(), a ) the rightmost expression is 'a'
so the if()evaluates according to the values in a.

You  needn't use use only 'a'.You can use anything

0

LVL 30

Expert Comment

ID: 8201653
>> because '=' operator has higher precedence than comma operator

>> the value of the right most expression is taken as the final value of the expression

Exactly. That's what I said too. I wonder why somebody said:

>> if( a = func(), a ) is same as if( a = func() )

Mayank.

0

LVL 48

Expert Comment

ID: 8202001

int func()
{
return 0;
}

int main(int argc, char* argv[])
{
int a;

if( a=func(), a )
printf("Yes\n");
else
printf("No\n");

return 0;
}

and opened Assembly code generated by VC++ compiler:

16:       if( a=func(), a )
00401068   call        @ILT+5(func) (0040100a)
0040106D   mov         dword ptr [ebp-4],eax
00401070   cmp         dword ptr [ebp-4],0
00401074   je          main+35h (00401085)
17:           printf("Yes\n");
00401076   push        offset string "Yes\n" (00422020)
0040107B   call        printf (004010c0)
18:       else
00401083   jmp         main+42h (00401092)
19:           printf("No\n");
00401085   push        offset string "No\n" (0042201c)
0040108A   call        printf (004010c0)

This is Assembly code without ", a":

16:       if( a=func() )
00401068   call        @ILT+5(func) (0040100a)
0040106D   mov         dword ptr [ebp-4],eax
00401070   cmp         dword ptr [ebp-4],0
00401074   je          main+35h (00401085)
17:           printf("Yes\n");
00401076   push        offset string "Yes\n" (00422020)
0040107B   call        printf (004010c0)
18:       else
00401083   jmp         main+42h (00401092)
19:           printf("No\n");
00401085   push        offset string "No\n" (0042201c)
0040108A   call        printf (004010c0)

Assembly code is exactly the same for both versions, as expected.

Sorry for using VC++ in this forum :-)
0

LVL 1

Expert Comment

ID: 8202128

This is for you CrypTonic.Might be this example will clear your doubts of comma operator.

x=y=3,y+1;
what is the value of x? ans=3
Since '=' has higher precedence than ',' x and y will have the value of '3'(i.e.multiple assignments are allowed in C)
and the then y+1 takes place but there is no place to store the result so it lost or ignored.

x=(y=3,y+1);
what is the value of x? ans=4
Since '=' has higher precedence than ',' we have introduced the parentheses and Inside the parentheses 'y=3' takes place first because of the precedence and then  y+1 takes place.The value of this expression is the rightmost expression which'y+1' which gets assigned to 'x' and so x='4'

Imagine
z=3;
y=1;
x=(y+2,z+y)
what is the value of x? ans=4
The value of this expression is the rightmost expression which is 'z+y' which is '3+1' but the previous expression 'y+2' is treated as a void and is ignored and 'y' will still have the value '1'
Bec

0

LVL 1

Expert Comment

ID: 8245246
You could find an explanation for the above problem
in "Complete Reference C++" by Herb Schildt and in"Pointers in C" by Yashwanth Kanithkar
0

LVL 20

Expert Comment

ID: 10189744
Nothing has happened on this question in more than 9 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
split points between mayankeagle, dimitry, AlexFM and vamshi_pavan.

jmcg
EE Cleanup Volunteer
0

LVL 30

Expert Comment

ID: 10206698
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and infâ€¦
There's never been a better time to become a computer scientist. Employment growth in the field is expected to reach 22% overall by 2020, and if you want to get in on the action, itâ€™s a good idea to think about at least minoring in computer science â€¦
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
###### Suggested Courses
Course of the Month8 days, 11 hours left to enroll