Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Comma inside if statement in C

Reply
Thread Tools

Comma inside if statement in C

 
 
Adee
Guest
Posts: n/a
 
      02-14-2013
Well yesterday I came across a strange behaviour of C program i.e.

let suppose we have three integers
int a=5, b=6, c=7;

if(c>b , b>a)
printf("C is greater %d",c);


this if statment is working, I am surprised about it. Can anyone explain this feature to me. Thanks
 
Reply With Quote
 
 
 
 
John Gordon
Guest
Posts: n/a
 
      02-14-2013
In <(E-Mail Removed)> Adee <(E-Mail Removed)> writes:

> Well yesterday I came across a strange behaviour of C program i.e.


> let suppose we have three integers
> int a=5, b=6, c=7;


> if(c>b , b>a)
> printf("C is greater %d",c);



> this if statment is working, I am surprised about it. Can anyone explain
> this feature to me. Thanks


When you have two or more expressions sepatated by commas, the rightmost
expression value is used, and all the other values are discarded.

So in this case, the expression 'c>b' is evaluated and the result is
discarded, then the expression 'b>a' is evaluated and used for the if()
statement.

--
John Gordon A is for Amy, who fell down the stairs
http://www.velocityreviews.com/forums/(E-Mail Removed) B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      02-14-2013
Adee <(E-Mail Removed)> writes:

> Well yesterday I came across a strange behaviour of C program i.e.
>
> let suppose we have three integers
> int a=5, b=6, c=7;
>
> if(c>b , b>a)
> printf("C is greater %d",c);
>
>
> this if statment is working, I am surprised about it. Can anyone
> explain this feature to me. Thanks


In this context, the comma is an operator used for sequencing the
evaluation of two expressions (the comma symbol has other meanings in
other contexts). What happens is this: c>b is (pointlessly) evaluated
and the result is "thrown away". The result of the whole expression --
that of the condition being tested -- is simply the result of b>a. Did
you test with other values of c?

--
Ben.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      02-14-2013
John Gordon <(E-Mail Removed)> writes:
> In <(E-Mail Removed)> Adee <(E-Mail Removed)> writes:
>> Well yesterday I came across a strange behaviour of C program i.e.

>
>> let suppose we have three integers
>> int a=5, b=6, c=7;

>
>> if(c>b , b>a)
>> printf("C is greater %d",c);

>
>
>> this if statment is working, I am surprised about it. Can anyone explain
>> this feature to me. Thanks

>
> When you have two or more expressions sepatated by commas, the rightmost
> expression value is used, and all the other values are discarded.
>
> So in this case, the expression 'c>b' is evaluated and the result is
> discarded, then the expression 'b>a' is evaluated and used for the if()
> statement.


That's true only when the comma is a comma *operator*. The comma symbol
is also used in other contexts, e.g., to saparate function arguments.

The syntax of an if statement specifies that the tokens between the
parentheses are a single expression, so in

if (c>b, b>a)

that's a comma operator; `c>b` is evaluated, the result is discarded,
then `b>a` evaluated and the result is used as the condition. (The
compiler is likely to generate code that doesn't bother to evaluate
`c>b`, since it has no side effects and its result is ignored). So the
printf statement lies; the output of the program is not affected by
whether C is greater than anything.

On the other hand, if you write:

some_func(c>b, b>a)

you're passing two arguments to some_func; the first is the result of
`c>b` and the second is the result of `b>a` (each of which is either 0
or 1). That's a delimiter between arguments, *not* a comma operator.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Xavier Roche
Guest
Posts: n/a
 
      02-14-2013
Le 14/02/2013 20:10, Keith Thompson a écrit :
> That's true only when the comma is a comma *operator*. The comma symbol
> is also used in other contexts, e.g., to saparate function arguments.


It is especially important with respect to the sequence point(s):
some_func(a++, a++)
will have an undefined behavior (comma is not a sequence point)

some_func((a++, a++))
will have a defined behavior (comma is a sequence point)
(some_func takes one argument here)

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      02-14-2013
Xavier Roche <(E-Mail Removed)> writes:
> Le 14/02/2013 20:10, Keith Thompson a écrit :
>> That's true only when the comma is a comma *operator*. The comma symbol
>> is also used in other contexts, e.g., to saparate function arguments.

>
> It is especially important with respect to the sequence point(s):
> some_func(a++, a++)
> will have an undefined behavior (comma is not a sequence point)
>
> some_func((a++, a++))
> will have a defined behavior (comma is a sequence point)
> (some_func takes one argument here)


Yes, but the latter is still poor style. IMHO it's better written as:

some_func(a+1);
a += 2;

If you're writing a compiler, you have to get this stuff right. If
you're writing code, clarity is important.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Xavier Roche
Guest
Posts: n/a
 
      02-14-2013
Le 14/02/2013 21:13, Keith Thompson a écrit :
> Yes, but the latter is still poor style. IMHO it's better written as:


Yes, of course. The only rather reasonable location of parenthesis is
probably conditions ; ie.

int success;
if (foo_is_successful()
&& (bar_process(&success), success) == TRUE
) {
....

which in many situations is simplifying the visual flow.

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      02-14-2013
Xavier Roche <(E-Mail Removed)> writes:
> Le 14/02/2013 21:13, Keith Thompson a écrit :
>> Yes, but the latter is still poor style. IMHO it's better written as:

>
> Yes, of course. The only rather reasonable location of parenthesis is
> probably conditions ; ie.
>
> int success;
> if (foo_is_successful()
> && (bar_process(&success), success) == TRUE
> ) {
> ...
>
> which in many situations is simplifying the visual flow.


What I suggested was poor style was function arguments with side
effects; it wasn't particularly about parentheses. I'm not sure your
example is any better. (And the "== TRUE" is a really bad idea. If
"success" is a condition, just use it as one.)

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      02-15-2013
pete <(E-Mail Removed)> writes:
> Adee wrote:
>> Well yesterday I came across a strange behaviour of C program i.e.
>>
>> let suppose we have three integers
>> int a=5, b=6, c=7;
>>
>> if(c>b , b>a)
>> printf("C is greater %d",c);
>>
>> this if statment is working, I am surprised about it.
>> Can anyone explain this feature to me. Thanks

>
>
> I think that the output of the printf statement
> was most likely intended to indicate transitivity,
> and therefore I also think that
>
> if(c>b && b>a)
>
> is what was most likely intended to be written.


Perhaps, but printing "C is greater 7" is not the clearest way to
express that condition. (Then again, it's not the clearest way to
express anything else.)

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
glen herrmannsfeldt
Guest
Posts: n/a
 
      02-15-2013
pete <(E-Mail Removed)> wrote:
> Keith Thompson wrote:
>> Xavier Roche <(E-Mail Removed)> writes:


(snip)

>> > some_func((a++, a++))
>> > will have a defined behavior
>> > (comma is a sequence point)
>> > (some_func takes one argument here)


(snip)
>> some_func(a+1);
>> a += 2;


>> If you're writing a compiler, you have to get this stuff right. If
>> you're writing code, clarity is important.


> If (a) has file scope and
> if (a) is also accessed within the some_func function,
> then
> a += 2;
> some_func(a - 1);
> would be the semantically equivalent form.


There is also:

a++;
some_func(a);
a++;

which might be more readable in some contexts.

In written formats like PDF, you have to keep track of how
many bytes you have written up to certain points to put into
the TOC at the end.

-- glen
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2) Test C Programming 40 12-20-2012 06:40 PM
csv.reader has trouble with comma inside quotes inside brackets Bret Python 3 06-09-2009 09:27 PM
Comma inside macro Vincent Rivière C++ 2 04-13-2008 01:50 PM
delineating by comma where commas inside quotation marks don't count Junior Python 4 10-27-2007 08:16 PM
break inside of case- statement inside of loop Alexander Korsunsky C Programming 25 02-27-2007 05:39 AM



Advertisments