Skip to content
hengxin edited this page Oct 27, 2017 · 3 revisions

Header Files

Input and Output operations can also be performed in C++ using the C Standard Input and Output Library (cstdio, known as stdio.h in the C language).

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.

Basic Info.

  • After pressing enter, the scanf function begins to scan the input stream.

Return Value

Whitespace

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.

Specifiers

%d

%ld: long int

long n;
scanf( "%ld", &n );

%s

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 fgets stop reading, but it is considered a valid character by the function and included in the string copied to str.
  • A terminating null character is automatically appended after the characters copied to str.

Return Value

  • On success, the function returns str.
  • If the end-of-file is encountered while attempting to read a character, the eof indicator is set (feof). If this happens before any characters could be read, the pointer returned is a null pointer (and the contents of str remain unchanged).
  • If a read error occurs, the error indicator (ferror) is set and a null pointer is also returned (but the contents pointed by str may have changed).

Comparisons

  • What is the difference between printf() and puts() in C?

    • puts is simpler than printf but be aware that puts automatically appends a newline.
    • int fputs(const char *str, FILE *stream) for stdout does not automatically append a newline.
    • Never, ever do a printf(variable) to print a string.
    • puts returns a nonnegative integer if successful or EOF if unsuccessful; printf returns the number of characters printed (not including the trailing null)
    • In simple cases, the compiler converts calls to printf() to calls to puts().

Use Cases

Note: This kind of optimization is almost never worth worrying about. Let style drives the decision.

Security Issues

See this lecture note: Format String Vulnerability.

Special Topics

  • EOF indicates "end of file". A newline (which is what happens when you press enter) isn't the end of a file (stream), it's the end of a line,
  • getchar() != EOF: The value of EOF is (commonly) -1 because it has to be different from any return value from getchar that is an actual character. So getchar returns any character value as an unsigned char, converted to int, which will therefore be non-negative.
  • The call to getchar() returns EOF when 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 is control-Z. On Unix systems, this is control-D.
  • EOF is not a character. The EOF is a macro that getchar() returns when it reaches the end of input or encounters some kind of error. The ^D is not "an EOF character". What's happening under Linux when you hit ^D on a line by itself is that it closes the stream, and the getchar() call reaches the end of input and returns the EOF macro. If you type ^D somewhere in the middle of a line, the stream isn't closed, so getchar() 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 configured EOF key (^D by 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 the stdio layer will interpret it as EOF status.
Clone this wiki locally