?
Solved

IF case explanation and reference please

Posted on 2003-03-24
13
Medium Priority
?
213 Views
Last Modified: 2010-04-15
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
Comment
Question by:CrypToniC
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
  • +2
13 Comments
 
LVL 30

Accepted Solution

by:
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

by:dimitry
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

by:AlexFM
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 30

Expert Comment

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

Cheers,

Mayank.
0
 
LVL 48

Expert Comment

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

Assisted Solution

by:vamshi_pavan
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

by:Mayank S
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

by:AlexFM
ID: 8202001
I made a simple program:

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)
00401080   add         esp,4
18:       else
00401083   jmp         main+42h (00401092)
19:           printf("No\n");
00401085   push        offset string "No\n" (0042201c)
0040108A   call        printf (004010c0)
0040108F   add         esp,4


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)
00401080   add         esp,4
18:       else
00401083   jmp         main+42h (00401092)
19:           printf("No\n");
00401085   push        offset string "No\n" (0042201c)
0040108A   call        printf (004010c0)
0040108F   add         esp,4


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

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

Expert Comment

by:vamshi_pavan
ID: 8202128
thats for AlexFm to answer.

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

by:vamshi_pavan
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

by:jmcg
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.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10206698
Please proceed with that recommendation.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
Suggested Courses
Course of the Month10 days, 10 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question