Menu Close

Articles

C: Input & Output

Most applications receive input, process the data and produce output. Input can be from keyboard, from touch screen or from file. Data processing is internal. Output can be to screen or to file. Input output functions are provided by <stdio.h> library that must be included into your code.

Console functions

For input and output from console we use following functions:

  • getchar()  – read a single character from console
  • putchar() – write a single character to console
  • gets() – read a string from console
  • puts() – write a string to console
  • scanf() – read something from console
  • printf() – write something to console

getchar() and putchar()

Read & write one character:

gets() and puts()

Read and write one string:

scanf() and printf()

Read and write integer and string:

Note: Observe placeholder symbol % has nothing special about it. It was a random choice to place this symbol along with one other to create a placeholder in string. For this placeholder we can send a value that matches the placeholder position.

Placeholders are called: format specifiers. These specifiers are specific to a data type but also there are several special specifiers not data type dependent. Using specifier you control output as well as input.

Format Specifiers:

%ccharacter
%ddecimal (integer) number (base 10)
%eexponential floating-point number
%ffloating-point number
%iinteger (base 10)
%ooctal number (base 8)
%sa string of characters
%uunsigned decimal (integer) number
%xnumber in hexadecimal (base 16)
%%print a percent sign
\%print a percent sign

Function signature:

This is the signature of the two functions:

  • int scanf(const char *format, …);
  • int printf(const char *format, …);

Note: 3 dots “…” is used in a function to denote a variable number of arguments. This must be always the last arguments in a function call. First argument is mandatory while variable arguments are optional.

Controlling alignment

You can control printf() numbers and strings by using a number format between symbol: %  and format specifier letter. This is important if you create table like reports where the numbers must fill in a specific number of available spaces.

Examples:

Working with files

Files are stored on external storage like: disk, usb-stick, dvd-rom, cd-rom, tape drive. The file can be a binary file or a text file. There is a special library in C that provide functions to create and open files, read content of a file and write content into a file.

Writing into a file:

Note: Function fopen() receive two parameters. First is the file name and second is access mode flag. This flag has predefined values. Using sign + will create the file if the file do not exist. Using “w” will open file for writing.

Access modifiers:

First access modifier is used for text files. Second access modifier is used for binary files.

r

rb

Opens an existing text file for reading purpose.

w

wb

Opens a text file for writing. If it does not exist, then a new file is created. Here your program will start writing content from the beginning of the file.

a

ab

Opens a text file for writing in appending mode. If it does not exist, then a new file is created. Here your program will start appending content in the existing file content.

r+

rb+

Opens a text file for both reading and writing.

w+

wb+

Opens a text file for both reading and writing. It first truncates the file to zero length if it exists, otherwise creates a file if it does not exist.

a+

ab+

Opens a text file for both reading and writing. It creates the file if it does not exist. The reading will start from the beginning but writing can only be appended.

Reading from a file:

To read from a file you must open the file using access modifier “r”. You can use functions fscanf() or fgets() to read word by word or row by row. Usually you read an entire file using a loop.

Reading a bunch of numbers:
Usually you read numbers into array. If the numbers are on multiple columns things are going to be more complex, but in next example we expect numbers to be one/row. 

Note:  For working with binary files  you must check external documentation for functions: fread() and fwrite(). Is not in our scope to teach you these two functions. 

Next article: Compiler directives