简体   繁体   中英

Command Line Interface with C - HOW DOES SCANF EVEN WORK?

Important edit: From the comments below, I understood that you guys didn't notice any problem with my code below. I've used a scanf(%c, &[a char variable]) to feed in more than one character before hitting enter on the commandline, and the scanf didn't work as it normally would when I tried using it the SECOND TIME. The SECOND TIME I used scanf and entered something on the commandline, IT DID NOT TAKE IN THE INPUT I had just entered. INSTEAD IT STORED WHATEVER WAS REMAINING FROM THE FIRST LINE OF INPUT.

Okay, so I was messing around with my C code yesterday. I ran the following code to see what would happen if I were to feed in a word in instead of a letter like I'm supposed to (Since I declared a char and not a char array or a string.)

Apperently if you feed in more than one letter, It will remember the remaining letters that it failed to take in as an input and then assign it to the variable you'll to use for assignment in your next scanf statement

PS I didn't use a for loop on purpose.

Can someone explain to me why this is happening? How does scanf work? And if the remaining input is not discarded, how exactly is it saved in the memory?

char word;

printf("start %d\n", 1);
scanf("%c", &word);
printf("%c\n\n", word);
printf("start %d\n", 2);
scanf("%c", &word);
printf("%c\n\n", word);
printf("start %d\n", 3);
scanf("%c", &word);
printf("%c\n\n", word);
printf("start %d\n", 4);
scanf("%c", &word);

scanf does not manage any of the work of reading from your terminal or console or from other devices.

In C, the standard input, standard output, and standard error are streams , and a program can open other streams manually. Streams are constructions (software, data, and algorithms) for managing connections to files and devices. They are implemented by other parts of the C standard I/O routines, such as fopen and fgetc .

When you call scanf , it gets one character at a time from the stream and attempts to match it to the conversions you have requested. As it finds matches, it stores data in the objects you have passed it. When it gets a character that is not acceptable for the conversion it is working on, it “puts it back” into the stream.

Streams may be buffered or unbuffered. If a stream is buffered, the stream software reads some amount of data from the file or device and keeps it in an internal array of characters. When scanf or other routines request one or more characters from the stream, the stream software provides the requested data from its array. The stream keeps track of how much data it has buffered. When the buffer is empty and more data is requested, the stream software reads more data from the file or device. Buffered streams can be fully buffered or line buffered . With a fully buffered stream, the software attempts to completely fill the buffer before performing another I/O operation with the file or device. With a line buffered stream, the software receives or sends one line at a time.

Even unbuffered streams maintain a buffer of at least one byte. This enables the “put back” capability. When scanf or another routine “puts back” a character, the stream software remembers it and, when a character is requested in the future, it returns that character first.

In spite of its name, the FILE object in C manages a stream. When fopen , for “file open,” is called, it opens a stream. The “file” part is legacy from the history of computing; if we were starting it over, we might call it “sopen” for “stream open.” stdin , stdout , and stderr point to FILE objects for streams that are automatically opened when the program starts. fclose closes a stream. fgetc and fputc read and write individual characters from a stream. There are variations of these routines, like getchar , that read from stdin . fread and fwrite read and write multiple characters at a time. These are the direct input/output routines.

scanf is one of the formatted input/output routines. It uses getchar , fread , or equivalents to get the characters, and then it performs additional operations to do the matching and conversions. When it finishes performing the conversions you request, it stops reading. The unread data remains in the stream. scanf does not take any action to read and discard the rest of the line, unless you explicitly request it with something in the format string that will match the rest of the line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM