scanf return values

This is a discussion on scanf return values within the C Programming forums, part of the General Programming Boards category; hi guys,
ok, i just realised something about scanf and its return values. when a user tries to assign (for ...

scanf return values

hi guys,

ok, i just realised something about scanf and its return values. when a user tries to assign (for example) three values to three variables, the return value of scanf is 3. nice.
THEN, if the user decides to input 4 values, return value of scanf is still three (since it reads only first three of them). where has the fourth value gone?
I'd like the user to be forced to input just three numbers by using:

I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.

i think it is an unnecessary use of input buffer. do extra values keep on filling up buffer and can this buffer overflow (if the number given to it is too large, for example)?

hmmm, reading the line and then parsing through sscanf()? i thought scanf() would do a similar job without the need of scanning the whole input line. well, I am not even a moderate (yet) programmer myself, but I'll try your solution.

yet, what bugs me, is that scanf can only give me first three values and not the actual number of values i provided. Makes IF statement redundant! heh.

I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.

actually, the programme waits until i enter at least 3 values. even if i separate them by X number of CARRIAGE RETURNS. Of course the programme (however small) terminates on the account of any letters. i guess that's typical of SCANF().

I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.

while running this programme in the terminal and pressing C-Z or C-D gives me russian letters (my keyboard is set to Eng/Rus). Anyway, the following ensures such termination and gives my own error message:

where n is my SCANF() return value
but i am not worried about the user inputting only two values (the above takes care of that). it is him putting 4 values, that still results in return value of SCANF() to be 3. I think it is a shame for scanf to take on predetermined number of arguments.

i am still to implement your solution involving SSCANF() - doing somehting else at the moment.

Tried to implement the SSCANF suggestion, but unsuccessfully.
From what errors I could unscramble, the issue was in an incorrect formating. I am going to stick to SCANF routine, although I still think it is not so beautiful.

You can't just use sscanf() as a drop-in replacement for scanf(). sscanf() parses its tokens from a char[] array rather than from stdin. So the general idea is that you use fgets() or a similar function to read data into a buffer, and then use sscanf() to parse that buffer. Quick example:

There are other ways to see if the user entered data beyond what you expected. You could use %p to see how far sscanf() read and then see if there were any non-space characters between there and the end of the string (or just use %c to make your life a lot easier).

"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell

I shall certainly try it your way, but as far as i can see that pretty much answers my question.
What i was doing, as you probably understood from previous posts, was using SSCANF just as I would use SCANF. Of course, I couldn't make getc to work either and was left to use scanf's buffer and that basically defeats the purpose of SSCANF.

the example you've given worked like a charm. Although i did not get the idea behind bogus variable, but then it came to me that it is a dummy variable and is necessary for the limitation of user input.
Couldn't define BUFFSIZE in pre-processor and then use it in CHAR BUFF[BUFFSIZE], so i just set it in declaration CHAR BUFF[120]. Other than that, there were no probs with the example.

Although i did not get the idea behind bogus variable, but then it came to me that it is a dummy variable and is necessary for the limitation of user input.

Yes, "dummy" would perhaps have been a better name. Oh well.

Couldn't define BUFFSIZE in pre-processor and then use it in CHAR BUFF[BUFFSIZE], so i just set it in declaration CHAR BUFF[120]. Other than that, there were no probs with the example.

BUFSIZ is pre-defined in stdio.h to be something at least 512. It's a good size to use for buffers. If you want to use your own size, of course, you could say #define BUFFER_SIZE 120 or something -- but don't try to re-define BUFSIZ, since the compiler probably won't like that too much.

"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell

i defined BUFFSIZE in a pre-processor statement as you have it in your last post, but my compiler did not like it. not quite sure why (i might have a look at it later), but that's the reason i defined it in declaration of CHAR BUFF[120]

i took a liberty of looking at your webpage, it looks good - i might visit it for reference purposes

good luck

p.s. still, i am left to implement your solution in my programme (when i get a chance)

i defined BUFFSIZE in a pre-processor statement as you have it in your last post, but my compiler did not like it. not quite sure why (i might have a look at it later), but that's the reason i defined it in declaration of CHAR BUFF[120]

i took a liberty of looking at your webpage, it looks good - i might visit it for reference purposes

good luck

p.s. still, i am left to implement your solution in my programme (when i get a chance)

I like guessing: don't put a semicolon at the end of your define (and don't put an equals sign in it either).