| |

VerySource

 Forgot password?
 Register
Search
Author: yifei110

scanf weird

[Copy link]

0

Threads

55

Posts

44.00

Credits

Newbie

Rank: 1

Credits
44.00

 Invalid IP Address

Post time: 2020-3-16 22:30:01
| Show all posts
fflush and stdin are both specified by ansi c, how can it be called undefined.
C language can clear the state, only this function.
Reply

Use magic Report

0

Threads

11

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 Invalid IP Address

Post time: 2020-3-18 19:30:02
| Show all posts
Both fflush and stdin are ANSI C, but fflush (stdin) is undefined.

It's all flush pipes. Who has seen the flush taps?
Reply

Use magic Report

0

Threads

55

Posts

44.00

Credits

Newbie

Rank: 1

Credits
44.00

 Invalid IP Address

Post time: 2020-3-19 14:00:01
| Show all posts
Open stdio.h and see for yourself
extern FILE (* _imp ___ iob) [];
#define _iob (* _imp ___ iob)
#define stdin (&_iob [0])
#define stdout (&_iob [1])
#define stderr (&_iob [2])
Since stdin is FILE *, of course fflush must make sure it is correct.
All are sewer pipes, where's the faucet?
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-3-19 18:45:01
| Show all posts
Fun to learn
Reply

Use magic Report

0

Threads

73

Posts

46.00

Credits

Newbie

Rank: 1

Credits
46.00

 Invalid IP Address

Post time: 2020-3-20 05:30:01
| Show all posts
To:superandy
This may not be just the keyboard.

K&R's "The C Programming Language" said:
Appendix B1.1:
On an output stream, fflush causes any buffered but unwritten data to be written; on an input stream, the effect is undefined.

Of course, it is mentioned in the C language standard.
C99 7.19.5.2
(2) If stream points to an output stream or an update stream in which the most recent operation was not input, the fflush function causes any unwritten data for that stream to be delivered to the host environment to be written to the file; otherwise, the behavior is undefined.

I understand it this way: Although for a general keyboard and its driving mode, clearing the buffer area of ​​a keyboard, its meaning is completely understandable.
But C / C ++ 's "standard input stream" does not necessarily point to a normal keyboard, and can also be redirected.
For example, if stdin is redirected to a file, wouldn't it make sense to "flush" a file input? Do you want to discard all the data read into memory? Or discard it by line? What about binary files?
Reply

Use magic Report

0

Threads

55

Posts

44.00

Credits

Newbie

Rank: 1

Credits
44.00

 Invalid IP Address

Post time: 2020-3-24 09:30:01
| Show all posts
an update stream in which the most recent operation was not input
words on msdn
The fflush function flushes a stream. If the file associated with stream is open for output, fflush writes to that file the contents of the buffer associated with the stream. If the stream is open for input, fflush clears the contents of the buffer. Fflush negates the effect of any prior call to ungetc against stream. Also, fflush (NULL) flushes all streams opened for output. The stream remains open after the call. fflush has no effect on an unbuffered stream.
Reply

Use magic Report

0

Threads

73

Posts

46.00

Credits

Newbie

Rank: 1

Credits
46.00

 Invalid IP Address

Post time: 2020-3-24 22:45:01
| Show all posts
an update stream in which the most recent operation was not input
Of course it is a stream of reads and writes, and the most recent operation was not read.

As for what to say on MSDN, that only represents Microsoft's attitude, haha.
I have mentioned it before, and it turns out that fflush (stdin) is feasible in VC and TC in DOS, haha.
Reply

Use magic Report

0

Threads

73

Posts

46.00

Credits

Newbie

Rank: 1

Credits
46.00

 Invalid IP Address

Post time: 2020-3-25 02:45:02
| Show all posts
Since Microsoft said that, of course it also supports it so much, it is just inconsistent with the standard.
Reply

Use magic Report

0

Threads

55

Posts

44.00

Credits

Newbie

Rank: 1

Credits
44.00

 Invalid IP Address

Post time: 2020-3-25 12:00:01
| Show all posts
devcpp / bcb / intel compilers on windows platforms all support fflush (stdin);
Reply

Use magic Report

0

Threads

55

Posts

44.00

Credits

Newbie

Rank: 1

Credits
44.00

 Invalid IP Address

Post time: 2020-3-26 10:30:01
| Show all posts
Must use the most standard method, then only while (getchar ()! = '\n');
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list