What do you mean by scanf in this while statement is 3?

Asked 1 years ago, Updated 1 years ago, 82 views

while(scanf("%d %s %s", &s[n].id,s[n].name,s[n].major)==3) n++;

What do you mean 3?

c scanf while-loop

2022-09-22 15:37

1 Answers

http://www.cplusplus.com/reference/cstdio/scanf/

First, let's look at the documentation of the scanf function.

The instructions for use are well described.

Let's look at the return value of scanf that we're curious about.

On success, the function returns the number of items of the argument list successfully filled. This count can match the expected number of items or be less (even zero) due to a matching failure, a reading error, or the reach of the end-of-file.

If a reading error happens or the end-of-file is reached while reading, the proper indicator is set (feof or ferror). And, if either happens before any data could be successfully read, EOF is returned.

If an encoding error happens interpreting wide characters, the function sets errno to EILSEQ.

Returns the number of variables substituted upon successful function call.

So let's look at the code on the question

while(scanf("%d %s %s", &s[n].id,s[n].name,s[n].major)==3) n++;

As you can see, &s[n].id, s[n] by the scanf function.name, s[n].You will receive three major/code> variables, so the result returns 3.

This means that if you enter three values as expected, add one n.


2022-09-22 15:37

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.