UC3M

Telematic/Audiovisual Syst./Communication Syst. Engineering

Systems Architecture

September 2017 - January 2018

8.3.  I/O functions for data types

Instead of reading in character by character from the user input, you can read a single data type at a time, such as integers, strings, etc. To do so, C provides you with the gets function, which reads strings, and the scanf function, which reads another basic data types. In the same way, we can write out strings with puts and other data types at once with the printf function. We have already seen this function, but here you are going to see some further details about it.

8.3.1.  The gets function

To read character by character from the input we have getc and getchar functions. We can also read line by line with several functions; one of them is gets:

#include <stdio.h>
char *gets(char *s);

Here the characters read from the standard input stream are stored in the character array identified by s. The gets function stops reading, and appends a null character '\0' to the array, when a newline or end-of-file (EOF) character is encountered. The function returns s if it concludes successfully. Otherwise, a null pointer is returned.