c
C Programming Tutorial

Input-Output in C

There are three main functions of any program- it takes data as input, processes is data, and gives The output. The input operation involves movement of data from an input device (gen rally keyboard) to computer memory, while in output operation the data moves from computer memory to the output device (generally screen).

C language does not provide any facility for input-output operations. The input-output is performed through a set of library functions that are supplied with every ‘C’ compiler. These functions are formally not a part of the language, but they are considered standard for all input-output operations in C. The set of library functions that perform input-output operations is known as standard I/O library.

There are several header files that provide the necessary information in support of the various library functions. These header files are entered into the program using the #include directive at the beginning of the program. For example, if a program uses any function from the standard I/O library, then it should include the header file stdio.h as-

#include<stdio .h>

Similarly, there are other header files like math.h, string.h, alloc.h that should be included when certain library functions are used.

In this tutorial we will discuss about the input functions scanf( ) and getchar( ) and the output functions Printf() and putchar( ). There are several other input-output functions that will be discussed in further tutorials.

Conversion Specifications

The functions scanf( ) and printf( ) make use of conversion specifications to specify the type and size of data Each conversion specification must begin with a percent sign ( % ). Some conversion specifications are as given below-

%c           a single character

%d          a decimal integer

%f           a floating-point number

%e           a floating-point number

%g           a floating-point number

%If           long range of floating-point number (for double data type)

%h           a short integer

%0           an octal integer

%x           a hexadecimal integer

%i            a decimal, octal or hexadecimal integer

%s           a string

%u          an unsigned decimal integer

The modifier h can be used before conversion specifications d, i, 0, u, x to specify short integer, and the modifier I can be used before them to specify a long integer. The modifier I can be used before conversion specifications f, e, g to specify double while modifier L can be used before them to specify a long double. For example, %Id, %hd, %Lf, %hx are valid conversion specifications.

Reading Input Data

Input data can be entered into the memory from a standard input device (keyboard). C provides the scanf( ) library function for entering input data. This function can take all types of values (numeric, character, string) as input. The scanf( ) function can be written as-

scanf( “control string” , address I, address2, ….);

This function should have at least two parameters. First parameter is a control string, which contains conversion specification characters. It should be within double quotes. The conversion specification characters may be one or more; it depends on the number of variables we want to input. The other parameters are addresses of variables. In the scanf( ) function at least one address should be present The address of a variable is found by preceding the variable name by an ampersand (&) sign. This sign is called the address operator and it gives the starting address of the variable name in memory. A string variable is not preceded by & sign to get the address.

Some examples of scanf( ) function are as-

#include<stdio.h>
main( )
{
int marks;
………….
scanf("%d",&marks) ;
………….
………….

}
 

In this example, the control string contains only one conversion specification character %d, which implies that one integer value should be entered as input. This entered value will be stored in the variable marks

#include<stdio.h>
main( )
{
fnt basic;
float hra;
char grade;
………….
scanf("%d %f %c", &basic, &hra, &grade) ;
………….
………….

} 

Here the control string has three conversion specifications characters %d, %f and %c means that one integer value, one floating-point value, and one single character can be entered as input. These values are stored in the variables basic, hra, and grade. The input data can be entered as-

1500 200.50 A

Writing Output Data

Output data can be written from computer memory to the standard output device (monitor) Using printf() library function. With this function, all types of values (numeric, character, or string) can be written as output. The printf( ) function can be written as-

printf(“control string”, variable 1, variable 2, );

In this function the control string contains conversion specification characters and text. It should be enclosed within double-quotes. The name of variables should not be preceded by an ampersand (&) sign. If the control string does not contain any conversion specification, then the variable names are not specified. Some example of printf( ) function areas-

#include<stdio.h>
main()
{
printf("C is excellent\n");
}

Output:
C is excellent
 

Here control string has only text and no conversion specification character, hence the output is only text.

#include<stdio.h>
main()
{
int age;
printf("Enter your age ");
scanf("%d",&age) ;
}
 

Here also printf() does not contain any conversion specification character and.is used to display a message that tells the user to enter his age.

#include<stdio.h>
main()
{
int age;
printf("Enter your age ");
scanf("%d",&age) ;
printf(" your age is : %d ", age);
}
 

Here in first printf() statement it does not contain any conversion specification character. In the last printf() statement we have used a conversion specification to display a value that is entered by user.