C Input Output Statements

In C Language input and output function are available as C compiler functions or C libraries provided with each C compiler implementation. These all functions are collectively known as Standard I/O Library function. Here I/O stands for Input and Output used for different inputting and outputting statements. These I/O functions are categorized into three processing functions. Console input/output function (deals with keyboard and monitor), disk input/output function (deals with floppy or hard disk), and port input/output function (deals with a serial or parallel port). As all the input/output statements deals with the console, so these are also Console Input/Output functions. Console Input/Output function access the three major files before the execution of a C Program. These are as follows:

  • stdin: This file is used to receive the input (usually is keyborad file, but can also take input from the disk file).
  • stdout: This file is used to send or direct the output (usually is a monitor file, but can also send the output to a disk file or any other device).
  • stderr: This file is used to display or store error messages.

Input Ouput Statement

Input and Output statement are used to read and write the data in C programming. These are embedded in stdio.h (standard Input/Output header file).

Input means to provide the program with some data to be used in the program and Output means to display data on screen or write the data to a printer or a file.C programming language provides many built-in functions to read any given input and to display data on screen when there is a need to output the result.

There are mainly two of Input/Output functions are used for this purpose. These are discussed as:

  • Unformatted I/O functions
  • Formatted I/O functions

Unformatted I/O functions

There are mainly six unformatted I/O functions discussed as follows:

  • getchar()
  • putchar()
  • gets()
  • puts()
  • getch()
  • getche()

getchar()

This function is an Input function. It is used for reading a single character from the keyboard. It is a buffered function. Buffered functions get the input from the keyboard and store it in the memory buffer temporally until you press the Enter key.

The general syntax is as:

v = getchar();

where v is the variable of character type. For example:

char n;
n = getchar();

A simple C-program to read a single character from the keyboard is as:

/*To read a single character from the keyboard using the getchar() function*/#include <stdio.h>
main()
{
char n;
n = getchar();
}

putchar()

This function is an output function. It is used to display a single character on the screen. The general syntax is as:

putchar(v);

where v is the variable of character type. For example:

char n;
putchar(n);

A simple program is written as below, which will read a single character using getchar() function and display inputted data using putchar() function:

/*Program illustrate the use of getchar() and putchar() functions*/#include <stdio.h>
main()
{
char n;
n = getchar();
putchar(n);
}

gets()

This function is an input function. It is used to read a string from the keyboard. It is also a buffered function. It will read a string when you type the string from the keyboard and press the Enter key from the keyboard. It will mark null character (‘\0’) in the memory at the end of the string when you press the enter key. The general syntax is as:

gets(v);

where v is the variable of character type. For example:

char n[20];
gets(n);

A simple C program to illustrate the use of gets() function:

/*Program to explain the use of gets() function*/#include <stdio.h>
main()
{
char n[20];
gets(n);
}

puts()

This is an output function. It is used to display a string inputted by gets() function. It is also used to display a text (message) on the screen for program simplicity. This function appends a newline (“\n”) character to the output.

The general syntax is as:

puts(v);

or

puts("text line");

where v is the variable of character type.

A simple C program to illustrate the use of puts() function:

/*Program to illustrate the concept of puts() with gets() functions*/#include <stdio.h>
main()
{
char name[20];
puts("Enter the Name");
gets(name);
puts("Name is :");
puts(name);
}

The Output is as follows:

Enter the Name
Geek
Name is:
Geek

getch()

This is also an input function. This is used to read a single character from the keyboard like getchar() function. But getchar() function is a buffered is function, getchar() function is a non-buffered function. The character data read by this function is directly assigned to a variable rather it goes to the memory buffer, the character data is directly assigned to a variable without the need to press the Enter key.

Another use of this function is to maintain the output on the screen till you have not press the Enter Key. The general syntax is as:

v = getch();

where v is the variable of character type.

A simple C program to illustrate the use of getch() function:

/*Program to explain the use of getch() function*/#include <stdio.h>
main()
{
char n;
puts("Enter the Char");
n = getch();
puts("Char is :");
putchar(n);
getch();
}

The output is as follows:

Enter the Char
Char is L

getche()

All are same as getch(0 function execpt it is an echoed function. It means when you type the character data from the keyboard it will visible on the screen. The general syntax is as:

v = getche();

where v is the variable of character type.

A simple C program to illustrate the use of getch() function:

/*Program to explain the use of getch() function*/#include <stdio.h>
main()
{
char n;
puts("Enter the Char");
n = getche();
puts("Char is :");
putchar(n);
getche();
}

The output is as follows:

Enter the Char L
Char is L

Formatted I/O functions

Formatted I/O functions which refers to an Input or Ouput data that has been arranged in a particular format. There are mainly two formatted I/O functions discussed as follows:

  • scanf()
  • printf()

scanf()

The scanf() function is an input function. It used to read the mixed type of data from keyboard. You can read integer, float and character data by using its control codes or format codes. The general syntax is as:

scanf("control strings",arg1,arg2,..............argn);

or

scanf("control strings",&v1,&v2,&v3,................&vn);

Where arg1,arg2,……….argn are the arguments for reading and v1,v2,v3,……..vn all are the variables.

The scanf() format code (spedifier) is as shown in the below table:

Format Code Meaning
%c To read a single character
%d To read a signed decimal integer (short)
%ld To read a signed long decimal integer
%e To read a float value exponential
%f To read a float (short0 or a single precision value
%lf To read a double precision float value
%g To read double float value
%h To read short integer
%i To read an integer (decimal, octal, hexadecimal)
%o To read an octal integer only
%x To read a hexadecimal integer only
%u To read unsigned decimal integer (used in pointer)
%s To read a string
%[..] To read a string of words from the defined range
%[^] To read string of words which are not from the defined range

Example Program:

/*Program to illustrate the use of formatted code by using the formatted scanf() function */ 
#include <stdio.h>
main()
{
char n,name[20];
int abc;
float xyz;
printf("Enter the single character, name, integer data and real value");
scanf("\n%c%s%d%f", &n,name,&abc,&xyz);
getch();
}

printf()

This ia an output function. It is used to display a text message and to display the mixed type (int, float, char) of data on screen. The general syntax is as:

printf("control strings",&v1,&v2,&v3,................&vn);

or

printf("Message line or text line");

Where v1,v2,v3,……..vn all are the variables.

The control strings use some printf() format codes or format specifiers or conversion characters. These all are discussed in the below table as:

Format Code Meaning
%c To read a single character
%s To read a string
%d To read a signed decimal integer (short)
%ld To read a signed long decimal integer
%f To read a float (short0 or a single precision value
%lf To read a double precision float value
%e To read a float value exponential
%g To read double float value
%o To read an octal integer only
%x To read a hexadecimal integer only
%u To read unsigned decimal integer (used in pointer)

Example Program:

/*Below the program which show the use of printf() function*/ 
#include <stdio.h>
main()
{
int a;
float b;
char c;
printf("Enter the mixed type of data");
scanf("%d",%f,%c",&a,&b,&c);
getch();
}
Related Post