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

 
 
Ben Bacarisse
Guest
Posts: n/a
 
      02-15-2013
glen herrmannsfeldt <(E-Mail Removed)> writes:

> 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.


Just in case it's been missed, pete's point was about the semantics, not
the readability. All three re-writes have different meanings if, as
pete stipulates, 'a' is used inside 'some_func'. His version has the
same meaning (under these conditions) as the original.

Of course, code that meets these conditions has problems that go beyond
using (a++, a++) as a single function argument!

<snip>
--
Ben.
 
Reply With Quote
 
 
 
 
glen herrmannsfeldt
Guest
Posts: n/a
 
      02-15-2013
Ben Bacarisse <(E-Mail Removed)> wrote:

(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;


(snip)
>>> 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.


> Just in case it's been missed, pete's point was about the semantics, not
> the readability. All three re-writes have different meanings if, as
> pete stipulates, 'a' is used inside 'some_func'. His version has the
> same meaning (under these conditions) as the original.


Yes, I did miss that one. I had thought about replying after Keith,
but then didn't do it. Keith commented about readability.

> Of course, code that meets these conditions has problems that
> go beyond using (a++, a++) as a single function argument!


Yes, don't do that!

As far as readability, if I was calling, say, 100 (different)
functions with successively higher values of a, I might go with
the a++ argument. If there was any question about readability,
one comment should apply to all of them. Seems nicer than 100
function calls and 100 a++ statements.

-- glen
 
Reply With Quote
 
 
 
 
Tim Rentsch
Guest
Posts: n/a
 
      02-16-2013
Ben Bacarisse <(E-Mail Removed)> writes:

> glen herrmannsfeldt <(E-Mail Removed)> writes:
>
>> 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.

>
> Just in case it's been missed, pete's point was about the semantics,
> not the readability. All three re-writes have different meanings
> if, as pete stipulates, 'a' is used inside 'some_func'. His version
> has the same meaning (under these conditions) as the original.
> [snip]


In fact none of the three transformations mentioned are guaranteed
to be semantics preserving, pete's included. More information is
needed.

(And no, not because of something obscure like 'a' being volatile
or using the C preprocessor.)
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      02-16-2013
pete <(E-Mail Removed)> writes:

> Tim Rentsch wrote:
>>
>> Ben Bacarisse <(E-Mail Removed)> writes:
>>
>> > glen herrmannsfeldt <(E-Mail Removed)> writes:
>> >
>> >> 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.
>> >
>> > Just in case it's been missed, pete's point was about the semantics,
>> > not the readability. All three re-writes have different meanings
>> > if, as pete stipulates, 'a' is used inside 'some_func'. His version
>> > has the same meaning (under these conditions) as the original.
>> > [snip]

>>
>> In fact none of the three transformations mentioned are guaranteed
>> to be semantics preserving, pete's included. More information is
>> needed.
>>
>> (And no, not because of something obscure like 'a' being volatile
>> or using the C preprocessor.)

>
> What did I miss?


The peculiarities of floating point arithmetic.

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

> Ben Bacarisse wrote:
>>
>> pete <(E-Mail Removed)> writes:
>>
>> > Tim Rentsch wrote:
>> >>
>> >> Ben Bacarisse <(E-Mail Removed)> writes:
>> >>
>> >> > glen herrmannsfeldt <(E-Mail Removed)> writes:
>> >> >
>> >> >> 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.
>> >> >
>> >> > Just in case it's been missed,
>> >> > pete's point was about the semantics,
>> >> > not the readability. All three re-writes have different meanings
>> >> > if, as pete stipulates, 'a' is used inside 'some_func'.
>> >> > His version
>> >> > has the same meaning (under these conditions) as the original.
>> >> > [snip]
>> >>
>> >> In fact none of the three transformations mentioned are guaranteed
>> >> to be semantics preserving, pete's included. More information is
>> >> needed.
>> >>
>> >> (And no, not because of something obscure like 'a' being volatile
>> >> or using the C preprocessor.)
>> >
>> > What did I miss?

>>
>> The peculiarities of floating point arithmetic.

>
> The post by Keith Thompson,
> which introduced the some_func function to this thread,
> showed (a) as being declared as type int in the original post.
> The scope of (a) was not specifed in the original post.


It's clear that Tim Rentsch is considered the code to be separated from
the original declaration of 'a' (now long snipped) or there would have
been no need to rule out the possibility of 'a' being declared volatile.

However, my reply suggested that I knew what he was thinking of and
of course I don't. I should have ended with a question mark, not a full
stop. He will, no doubt, explain it all soon.

--
Ben.
 
Reply With Quote
 
Tim Rentsch
Guest
Posts: n/a
 
      02-17-2013
Ben Bacarisse <(E-Mail Removed)> writes:

> pete <(E-Mail Removed)> writes:
>
>> Ben Bacarisse wrote:
>>>
>>> pete <(E-Mail Removed)> writes:
>>>
>>> > Tim Rentsch wrote:
>>> >>
>>> >> Ben Bacarisse <(E-Mail Removed)> writes:
>>> >>
>>> >> > glen herrmannsfeldt <(E-Mail Removed)> writes:
>>> >> >
>>> >> >> 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.
>>> >> >
>>> >> > Just in case it's been missed,
>>> >> > pete's point was about the semantics,
>>> >> > not the readability. All three re-writes have different meanings
>>> >> > if, as pete stipulates, 'a' is used inside 'some_func'.
>>> >> > His version
>>> >> > has the same meaning (under these conditions) as the original.
>>> >> > [snip]
>>> >>
>>> >> In fact none of the three transformations mentioned are guaranteed
>>> >> to be semantics preserving, pete's included. More information is
>>> >> needed.
>>> >>
>>> >> (And no, not because of something obscure like 'a' being volatile
>>> >> or using the C preprocessor.)
>>> >
>>> > What did I miss?
>>>
>>> The peculiarities of floating point arithmetic.

>>
>> The post by Keith Thompson,
>> which introduced the some_func function to this thread,
>> showed (a) as being declared as type int in the original post.
>> The scope of (a) was not specifed in the original post.

>
> It's clear that Tim Rentsch is considered the code to be separated from
> the original declaration of 'a' (now long snipped) or there would have
> been no need to rule out the possibility of 'a' being declared volatile.


Right. There wasn't any mention of int, or declaration of a, in
Xavier Roche's post, which I took to be the head of the subthread
(and consequently is as far back as I checked). Moreover, the
mention of 'a' possibly being global seems to assume a different
context than those earlier postings, which gave the impression
(to me anyway) that 'a' was declared in block scope. (Or there
may have been a nested declaration hiding the outer 'a'.)

> However, my reply suggested that I knew what he was thinking of and
> of course I don't. I should have ended with a question mark, not a full
> stop. He will, no doubt, explain it all soon.


Actually I didn't think of floating point types. If I had I
might have mentioned them along with volatile etc -- a problem in
theory, but unlikely to occur in practice.

A more typical case, and one relatively likely to occur in actual
code, is when 'a' has a type like unsigned char or unsigned
short. If the value of a is about to wrap around, 'a++' is (in
most implementations) not the same as '(a += 1)-1', because of
integer type promotion rules.
 
Reply With Quote
 
Tim Rentsch
Guest
Posts: n/a
 
      02-17-2013
pete <(E-Mail Removed)> writes:

> Tim Rentsch wrote:
>>
>>> [.. alternative was of expressing a call like foo( (a++,a++) ) ..]

>>
>> In fact none of the three transformations mentioned are guaranteed
>> to be semantics preserving, pete's included. More information is
>> needed.
>>
>> (And no, not because of something obscure like 'a' being volatile
>> or using the C preprocessor.)

>
> What did I miss?


Explanation given in response to Ben Bacarisse's post downthread.
 
Reply With Quote
 
Shao Miller
Guest
Posts: n/a
 
      02-17-2013
On 2/14/2013 13:41, 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
>


A comma operator is a binary operator, with a left and a right operand.
The value of the result of the comma operator is the value of the
right operand. So:

(1, 0) == 0
(0, 1) == 1
(13, 42) == 42

So:

(c > b, b > a) == (b > a)

Maybe you would be more interested in the logical operators, such as
'&&' and '||'.

(c > b && b > a) == 1

in your code example, above.

--
- Shao Miller
--
"Thank you for the kind words; those are the kind of words I like to hear.

Cheerily," -- Richard Harter
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      02-17-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


Where did you come across this? Did you write the code yourself? What
exactly did you expect it to do?

C makes it very easy to make mistakes that radically alter the meaning
of a piece of code without making it illegal.

We can speculate that what you really wanted to write was:

if (c > b && b > a)

but we can't be sure of that if you don't tell us.

We can tell you exactly what a given chunk of C code actually means,
but that's only part of the picture. A programmer's task more
commonly starts with something you want to do, and asks what code
to write to accomplish it. If your goal is to learn the language
rather than to solve random little puzzles, we can be far more
helpful if you provide us with more context.

For example, if my guess about what you were trying to do is correct,
you might have asked:

Given
int a=5, b=6, c=7;
I want to test whether the value of b is between the values of a
and c. I was surprised to find that this:
if (c > b, b > a)
actually works. Can you explain why?

We could have told you both why your code behaves the way it does, and
what you should have written.

But I'm still not sure what you were really trying to accomplish. Only
you can tell us that.

The more context you can give us, the better job we can do of both
answering your direct question and giving you the knowledge that
will let you answer the next question yourself.

Also, the output of your printf call:
printf("C is greater %d",c);
will be:
C is greater 7
which is confusing (even ignoring that "c" and "C" are two different
identifiers).

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(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
 
 
 
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