Killing the comma operator
Steven Schveighoffer via Digitalmars-d
digitalmars-d at puremagic.com
Wed May 11 07:08:28 PDT 2016
On Wednesday, 11 May 2016 at 13:29:56 UTC, Gopan wrote:
> On Tuesday, 10 May 2016 at 09:52:07 UTC, Mathias Lang wrote:
>
>>
>> Do you like comma expressions, ...
>
> I am a student.
> In C, one scenario where I like comma is this.
>
>
> int x;
> while( scanf("%d", &x), x!= 0) // until user input 0.
> {
> //do something with x
> }
>
> Without the comma operator, I would have to repeat the scanf
> statement.
> int x;
> scanf("%d", &x);
> while(x != 0)
> {
> //do something with x
> scanf("%d", &x);
> }
>
> Does anybody think that this is a useful case of comma operator?
What if scanf fails or hits eof?
A better call:
while(scanf("%d", &x) > 0 && x != 0)
-Steve
More information about the Digitalmars-d
mailing list