What does this code mean???

M

Mars

I am reading the code of a program from the examples, but really don't
understand this one........

int i,j;

.....................................................

while (scanf("%d",&i),j)
{
........................................
}

So I think the question should become,
how does the compiler evaluate the expression, (int i,int j)??
(am I right?? 'cause scanf function returns an integer as I know)
 
U

Ulrich Eckhardt

Mars said:
I am reading the code of a program from the examples, but really don't
understand this one........

int i,j;

....................................................

while (scanf("%d",&i),j)
{
.......................................
}

So I think the question should become,
how does the compiler evaluate the expression, (int i,int j)??

That's a typical application of the comma-operator, look it up in your
books.
(am I right?? 'cause scanf function returns an integer as I know)

You really should not need to ask what scanf() returns and what that
returnvalue means, you should know where to find that info. You are right
with your assumption though.

Uli
 
J

Jonathan Burd

Mars said:
I am reading the code of a program from the examples, but really don't
understand this one........

int i,j;

....................................................

while (scanf("%d",&i),j)
{
.......................................
}

Break it down like this:

scanf("%d", &i), j

The value returned by scanf is discarded and the whole expression
evaluates to the value of j. A sequence point occurs
at the comma. (See Annex C of the C Standard).

The loop is reading in values and will continue executing
while j is not 0.

As an example, see what this does:

/* comma.c */
#include <stdio.h>

int
main (void)
{
int a, b = 6;
int ret;

ret = (a = 5, b);

printf ("a: %d; ret: %d\n", a, ret);

return 0;
}

Regards,
Jonathan.
 
N

Nick Austin

I am reading the code of a program from the examples, but really don't
understand this one........

int i,j;

....................................................

while (scanf("%d",&i),j)
{
.......................................
}

It's the comma operator. The call to scanf() occurs as part of
the loop but occurs before evaluating the end condition.

It's the same as either:

scanf("%d",&i);
while (j)
{
/*....................................... */
scanf("%d",&i);
}

or:

while (1)
{
scanf("%d",&i);
if (!j)
break;
/*....................................... */
}

Nick.
 
M

Mars

icic, thx~


Jonathan Burd mentioned:
Break it down like this:

scanf("%d", &i), j

The value returned by scanf is discarded and the whole expression
evaluates to the value of j. A sequence point occurs
at the comma. (See Annex C of the C Standard).

The loop is reading in values and will continue executing
while j is not 0.

As an example, see what this does:

/* comma.c */
#include <stdio.h>

int
main (void)
{
int a, b = 6;
int ret;

ret = (a = 5, b);

printf ("a: %d; ret: %d\n", a, ret);

return 0;
}

Regards,
Jonathan.
 
M

Mars

Thx~~


Nick Austin mentioned:
It's the comma operator. The call to scanf() occurs as part of
the loop but occurs before evaluating the end condition.

It's the same as either:

scanf("%d",&i);
while (j)
{
/*....................................... */
scanf("%d",&i);
}

or:

while (1)
{
scanf("%d",&i);
if (!j)
break;
/*....................................... */
}

Nick.
 
J

Jonathan Burd

Mars said:
icic, thx~

<snip>

Please do not top-post in c.l.c.
Top-posting blurs the context and makes it
difficult to see what you are replying to.

Regards,
Jonathan.
 
R

Richard Bos

Break it down like this:

scanf("%d", &i), j

The value returned by scanf is discarded and the whole expression
evaluates to the value of j. A sequence point occurs
at the comma. (See Annex C of the C Standard).

The loop is reading in values and will continue executing
while j is not 0.

True, but I must say that this is an inadvisable way of using the comma
operator. The scanf() call has nothing to do with whether or not the
loop continues at all. This use of the comma is mere obfuscation.

Richard
 
S

Servé La

True, but I must say that this is an inadvisable way of using the comma
operator. The scanf() call has nothing to do with whether or not the
loop continues at all. This use of the comma is mere obfuscation.

That's your opinion of course. I like this style because the return value of
scanf is often ignored anyway and you dont need to put in 2 calls to scanf
as in:

scanf();
while(j)
{
scanf();
}
 
L

Lawrence Kirby

That's your opinion of course. I like this style because the return value of
scanf is often ignored anyway

Unfortunately true. Consider however there are very few situations where
ignoring the return value of scanf() isn't a bug.

Lawrence
 
E

Eric Sosman

Servé La said:
"Richard Bos" <[email protected]> schreef in bericht

True, but I must say that this is an inadvisable way of using the comma
operator. The scanf() call has nothing to do with whether or not the
loop continues at all. This use of the comma is mere obfuscation.


That's your opinion of course. I like this style because the return value of
scanf is often ignored anyway [...]

What else do you ignore? NULL values from malloc() and
fopen()? Warning messages from compilers? STOP signs? Fire
alarms?

No wonder software sucks.
 
C

CBFalconer

Lawrence said:
.... snip ...

Unfortunately true. Consider however there are very few situations
where ignoring the return value of scanf() isn't a bug.

I rarely use scanf so am not sure, but are there any situations in
which it isn't a bug?
 
P

parser

see the code below
x=(scanf("%d",&i),j,k,l,......,z); /*x will hav the value of z*/

u should that the comma separated expressions are evaluated from Left
to Right and thats why the whole expression i.e. x, woud have a value
of the rightmost expression i.e. z

now consider this example

printf("here is a value : %d", ( 1,2,3,4,5,6,7,8,9));

output:
here is a vlaue : 9
so now i think that the it is clear to u

thanx :)
 
L

Lawrence Kirby

I rarely use scanf so am not sure, but are there any situations in
which it isn't a bug?

Only situations where you didn't care if the read operation failed or
encountered end-of-file, and the scanf() just readss characters without
writing values. scanf(fp, " ") to skip white-space perhaps in some odd
circumstances. Not very likely but it is difficult to say that there
is NO situation where this isn't a bug. Well actually easy to say, maybe I
should and just wait for others to come up with the counterexamples. :)

Lawrence
 
R

Richard Bos

[ Please ditch Google Broken Beta or learn to use it to post properly,
_with_ normal quoting. Also, learn to write grammatical English.
You're nearly illegible. ]
see the code below
x=(scanf("%d",&i),j,k,l,......,z); /*x will hav the value of z*/

u should that the comma separated expressions are evaluated from Left
to Right and thats why the whole expression i.e. x, woud have a value
of the rightmost expression i.e. z

No, that's not why. For example, the expressions separated by a string
of && operators are _also_ evaluated from left to right, but the value
of the whole expression is either 0 or 1, depending on any number of the
expressions, no matter whether the relevant ones are 1, 3.14159265,
&main, or any other non-zero value.

Your conclusion is correct in so far that the Standard says of the ,
operator that
- its operands are evaluated in the order left first, then right;
- because of the way its syntax it specified, it is left-to-right
associative (which, in conjunction with the above, also means that a
string of expressions separated by commas is evaluated left to right);
- the value and type of the whole expression are those of the right
argument.
However, the third requirement does not follow by necessity from the
first two at all.

Richard
 
C

Chris Croughton

Only situations where you didn't care if the read operation failed or
encountered end-of-file, and the scanf() just readss characters without
writing values. scanf(fp, " ") to skip white-space perhaps in some odd
circumstances. Not very likely but it is difficult to say that there
is NO situation where this isn't a bug. Well actually easy to say, maybe I
should and just wait for others to come up with the counterexamples. :)

I'm not aware of any for scanf, but sscanf can certainly be legitimately
used without checking the return value, because it leaves its arguments
alone if it has no data so they default to the latest value. Silly
example:

#include <stdio.h>
int main(void)
{
char buff[256];
int x = 0;
int y = 0;
while (fgets(buff, 256, stdin))
{
sscanf(buff, "%d%d", &x, &y);
printf("%d + %d = %d\n", x, y, x+y);
}
return 0;
}

I suppose one could use something similar with scanf() and check feof()
after each call.

Chris C
 
S

Servé La

Eric Sosman said:
True, but I must say that this is an inadvisable way of using the comma
operator. The scanf() call has nothing to do with whether or not the
loop continues at all. This use of the comma is mere obfuscation.


That's your opinion of course. I like this style because the return value of
scanf is often ignored anyway [...]

What else do you ignore? NULL values from malloc() and
fopen()? Warning messages from compilers? STOP signs? Fire
alarms?


As as advised in this group, scanf is best to be avoided except in quick
test programs. I haven't seen a lot of error checking in that kind of
programs.
 
C

CBFalconer

Servé La said:
.... snip ...

As as advised in this group, scanf is best to be avoided except in
quick test programs. I haven't seen a lot of error checking in that
kind of programs.

It can be quite useful if restricted to single operands, i.e.:

if (1 == scanf("%?", &something)) usesomething;
else failure;

with %? standing for a suitable customization. Scanf will leave
the terminating char. in the input stream, so you know that an
input line has at least one unconsumed \n left.
 
K

Keith Thompson

CBFalconer said:
It can be quite useful if restricted to single operands, i.e.:

if (1 == scanf("%?", &something)) usesomething;
else failure;

with %? standing for a suitable customization. Scanf will leave
the terminating char. in the input stream, so you know that an
input line has at least one unconsumed \n left.

One possible pitfall is that, for example, scanf("%d", &an_integer)
skips leading whitespace, including newlines. If you use fgets()
followed by sscanf(), a user who hits <return> (assuming interactive
keyboard input) will get an error message. If you use scanf()
directly, the user can hit <return> arbitrarily many times without
getting any feedback.

Whether this is a problem depends, of course, on the application.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,416
Latest member
LionelQ387

Latest Threads

Top