-
Notifications
You must be signed in to change notification settings - Fork 2
IO c
Input and Output operations can also be performed in
C++using the C Standard Input and Output Library (cstdio, known asstdio.hin theClanguage).
That is, use #include <stdio.h> in C and #include <cstdio> in C++.
int scanf ( const char * format, ... );: Reads data from stdin
and stores them according to the parameter format into the locations
pointed by the additional arguments.
- After pressing
enter, thescanffunction begins to scan the input stream.
- What is the effect of trailing white space in a scanf() format string? A whitespace character in a scanf format causes it to explicitly read and ignore as many whitespace characters as it can.
With scanf("%d ", ...), after reading a number,
it will continue to read characters, discarding all whitespace
until it sees a non-whitespace character on the input.
That non-whitespace character will be left as the next character to be read by an input function.
Since most scanf %-conversions also skip leading whitespace
(all except for %c, %[ and %n),
spaces before %-conversions are irrelevant ("%d" and " %d" will act identically).
For the most part, you should avoid spaces in scanf conversions
unless you know you specifically need them for their peculiar effect.
long n;
scanf( "%ld", &n );
Reading any number of non-whitespace characters, stopping at the first whitespace character found. A terminating null character is automatically added at the end of the stored sequence.
char * gets ( char * str );
The most recent revision of the C standard (2011) has definitively removed this function from its specification. The function is deprecated in C++ (as of 2011 standard, which follows C99+TC3).
Using fgets instead.
char * fgets ( char * str, int num, FILE * stream );
Reads characters from stream and stores them as a C string into str
until (num-1) characters have been read or either a newline or the end-of-file is reached, whichever happens first.
Note:
- A newline character makes
fgetsstop reading, but it is considered a valid character by the function and included in the string copied tostr. - A terminating
nullcharacter is automatically appended after the characters copied tostr.
- On success, the function returns
str. - If the end-of-file is encountered while attempting to read a character,
the
eofindicator is set (feof). If this happens before any characters could be read, the pointer returned is anullpointer (and the contents ofstrremain unchanged). - If a read error occurs, the error indicator (
ferror) is set and anullpointer is also returned (but the contents pointed bystrmay have changed).
-
What is the difference between printf() and puts() in C?
-
putsis simpler thanprintfbut be aware thatputsautomatically appends anewline. -
int fputs(const char *str, FILE *stream)forstdoutdoes not automatically append anewline. - Never, ever do a
printf(variable)to print a string. -
putsreturns a nonnegative integer if successful orEOFif unsuccessful;printfreturns the number of characters printed (not including the trailingnull) - In simple cases, the compiler converts calls to
printf()to calls toputs().
-
-
putchar('\n')printf('\n')puts("")
Note: This kind of optimization is almost never worth worrying about. Let style drives the decision.
See this lecture note: Format String Vulnerability.
-
EOFindicates "end of file". Anewline(which is what happens when you pressenter) isn't the end of a file (stream), it's the end of a line, -
getchar() != EOF: The value ofEOFis (commonly)-1because it has to be different from any return value fromgetcharthat is an actual character. Sogetcharreturns any character value as an unsigned char, converted to int, which will therefore be non-negative. - The call to
getchar()returnsEOFwhen you reach the "end of file". As far as C is concerned, the 'standard input' (the data you are giving to your program by typing in the command window) is just like a file. Of course, you can always type more, so you need an explicit way to say "I'm done". On Windows systems, this iscontrol-Z. On Unix systems, this iscontrol-D. -
EOFis not a character. TheEOFis a macro thatgetchar()returns when it reaches the end of input or encounters some kind of error. The^Dis not "anEOFcharacter". What's happening under Linux when you hit^Don a line by itself is that it closes the stream, and thegetchar()call reaches the end of input and returns theEOFmacro. If you type^Dsomewhere in the middle of a line, the stream isn't closed, sogetchar()returns values that it reads. - When the terminal is in canonical mode, lines aren't transmitted over the tty device until you press
enter. Pressing the configuredEOFkey (^Dby default) causes the data to be immediately transmitted and any read waiting on it to return with the number of characters available. If the line already has data on it, this will be a normal, non-zero-length read. If the line is empty, this will result in a zero-length read, which is the definition of end-of-file status on a file descriptor. Thus thestdiolayer will interpret it asEOFstatus.