C Programming + Tutorials in C for Beginners

C Input/Output Statements

Home  »  C Programming  »  C Input/Output Statements


        In C Language input and output function are available as C compiler function or C library 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 prcessing functions. Console input/output function (deals with keyborad and monitor), disk input/output function (deals with floppy or hard disk) and port input/output function (deals with serial or parallet 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:

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.

Contents

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). There are mainly two of Input/Output functions are used for this purpose. These are discussed as:

Unformatted I/O functions

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

a) getchar()
b) putchar()
c) gets()
d) puts()
e) getch()
f) getche()

a) getchar()

    This function is an Input function. It is used for reading a single character from the keyborad. It is 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
main()
{
char n;
n = getchar();
}


b) 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
main()
{
char n;
n = getchar();
putchar(n);
}


c) gets()

    This function is an input function. It is used to read a string from the keyboar. It is also 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 nulll 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
main()
{
char n[20];
gets(n);
}


d) puts()

    This is an output function. It is used to display a string inputted by gets() function. It is also used to display an 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
main()
{
char name[20];
puts("Enter the Name");
gets(name);
puts("Name is :");
puts(name);
}

OUTPUT IS:
Enter the Name
Laura
Name is:
Laura


e) 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 assign to a variable rather it goes to the memory buffer, the character data directly assign 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
main()
{
char n;
puts("Enter the Char");
n = getch();
puts("Char is :");
putchar(n);
getch();
}

OUTPUT IS:
Enter the Char
Char is L
f) 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
main()
{
char n;
puts("Enter the Char");
n = getche();
puts("Char is :");
putchar(n);
getche();
}

OUTPUT IS:
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:

a) scanf()
b) printf()

a) 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 CodeMeaning
%cTo read a single character
%dTo read a signed decimal integer (short)
%ldTo read a signed long decimal integer
%eTo read a float value exponential
%fTo read a float (short0 or a single precision value
%lfTo read a double precision float value
%gTo read double float value
%hTo read short integer
%iTo read an integer (decimal, octal, hexadecimal)
%oTo read an octal integer only
%xTo read a hexadecimal integer only
%uTo read unsigned decimal integer (used in pointer)
%sTo 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


/*Program to illustrate the use of formatted code by using the formatted scanf() function */ #include
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();
}

b) 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 CodeMeaning
%cTo read a single character
%sTo read a string
%dTo read a signed decimal integer (short)
%ldTo read a signed long decimal integer
%fTo read a float (short0 or a single precision value
%lfTo read a double precision float value
%eTo read a float value exponential
%gTo read double float value
%oTo read an octal integer only
%xTo read a hexadecimal integer only
%uTo read unsigned decimal integer (used in pointer)


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




<< Previous Topic
Next Topic >>



Previous
Next Post »