4.input Output Operations
4.input Output Operations
INTRODUCTION
• In C, all I/O operations are carried out through function calls, such as scanf(), printf()
• All the standard functions used for I/O are collectively called as standard I/O library
• Each program that uses a standard I/O function must contain the following statement at the
beginning:
#include <stdio.h>
• This instruction tells the compiler ‘to search for a file named as stdio.h and place the contents at
this point in the program’
• If the program is using only the functions printf() and scanf() then the above statement is not
necessary to write
FORMATTED INPUT
• scanf() is used to input data through keyboard
• The general form of scanf() is:
scanf(“control string”, &arg1, &arg2, …,&argn);
• The control string specifies the field/ format specifications in which the data is to be entered
• The ampersand (&) symbol before each variable specifies the variable’s address
• The field specification for reading an integer number is as follows:
%wd
• % indicates that a conversion specification follows
• w, an integer, specifies the field width of the number to be read
• d, data type character, indicates that the number to be read is an integer
• On success, the function returns the number of items of the argument list successfully read
EXAMPLE: INPUTTING INTEGER NUMBERS
• Consider the following data line:
50 31426
• Corresponding scanf() statement will be: scanf(“%2d %5d”, &num1, &num2);
• num1 = 50, num2 = 31426
• If the input data is as follows:
31426 50
• With the same scanf() statement, num1 = 31, num2 = 426
• The unread value 50 will be assigned to the first variable in the next scanf() call
• An input field may be skipped by specifying * in the place of field width
• If the data line is: 123 456 789
• Then the statement: scanf(“%d %*d %d”, &a, &b) will assign 123 to a, 456 will be skipped,
789 will be assigned to b
SAMPLE PROGRAM
//program showing the use of scanf()
#include<stdio.h>
int main()
{
int num1,num2,count;
printf("\n enter the values:");
count=scanf("%d %d",&num1,&num2);
printf("\n num1=%d \t num2=%d \t
count=%d”,num1,num2,count);
return 0;
}
SAMPLE PROGRAM
//program showing the use of scanf() with %*d
#include<stdio.h>
int main()
{
int num1,num2,num3,count;
printf("\n enter the values:");
count=scanf("%d%*d%d",&num1,&num2);
printf("\n num1=%d \t num2=%d \t
count=%d",num1,num2,count);
return 0;
}
INPUTTING REAL NUMBERS
//program showing the use of scanf()
for real numbers
#include<stdio.h>
int main()
{
float x,y,z;
scanf("%f%f%f",&x,&y,&z);
printf("\n%f\t%f\t%f",x,y,z);
return 0;
}
READING A SINGLE CHARACTER
• A single character can be read using scanf() with %c
• Program waits at the getchar() line until a character is typed, and then reads it and stores it in the
respective variable
SAMPLE PROGRAM
//program showing the use of scanf() for
characters
#include<stdio.h>
int main()
{
char c, ch;
printf("\n enter the first character:");
c=getchar();
getchar();
printf("\n enter the second character:");
scanf("%c",&ch);
printf("\n c=%c\t ch=%c",c,ch);
return 0;
}
READING MIXED DATA TYPES
• Consider the following data line:
15 p 1.575
• Corresponding scanf() statement will be: scanf(“%d%c%f”, &num1, &c, &num2);
• In such cases, care should be taken to ensure that the input data items match the control
specifications in order and in type
• If it is not matched then scanf() doesn’t read that value and returns the correct values read
SAMPLE PROGRAM
//PROGRAM SHOWING THE USE OF SCANF() FOR MIXED DATA
#include<stdio.h>
int main()
{
int num1,count;
float num2;
char c;
printf("\n enter the values:");
count=scanf("%c%d%f",&c,&num1,&num2);
printf("\n c=%c \t num1=%d \t num2=%f \t count=%d",c, num1,num2,count);
count=scanf("%d%f",&num1,&num2);
printf("\n num1=%d \t num2=%f \t count=%d",num1,num2,count);
return 0;
}
FORMATTED OUTPUT
• printf() is used to produce output to the standard output device (typically monitor screen)
• The general form is: int printf(“control string”, arg1, arg2, …, argn);
• control string consists of following items:
• Characters that will be printed on the screen as they appear
• Format specifications that define the output format for display
• Escape Sequence characters, like \n, \t, \b, etc..
• The arguments arg1, arg2, .., argn are the variables whose values are formatted and printed as per
the specifications given in the control string
• If successful, the total number of characters written is returned. On failure, a negative number is
returned.
FORMATTED OUTPUT – INTEGER NUMBER
• Format specification for printing an integer number is: %wd
• Here, w specifies the minimum field width
• d specifies that the value to be printed is an integer
• By default the number is written right justified
• Printing to be left justified forcefully, a minus sign can be placed just after % character
• printf(“%d”, 9876);
• printf(“%6d”,9876);
• printf(“%2d”, 9876);
• printf(“%-6d”,9876);
• printf(“%06d”, 9876);
FORMATTED OUTPUT – INTEGER NUMBER
• Format specification for printing an integer number is: %wd
• Here, w specifies the minimum field width
• d specifies that the value to be printed is an integer
• By default the number is written right justified
• Printing to be left justified forcefully, a minus sign can be placed just after % character
• printf(“%d”, 9876); 9 8 7 6
• printf(“%6d”,9876); 9 8 7 6
• printf(“%2d”, 9876); 9 8 7 6
• printf(“%-6d”,9876); 9 8 7 6
• printf(“%06d”, 9876);
0 0 9 8 7 6
FORMATTED OUTPUT – REAL NUMBER
• Format specification is: %w.p f
• The integer w indicates the minimum number of positions that are to be used for the display of
the value and p indicates the number of digits to be displayed after the decimal point (precision)
• Default precision is 6 decimal places
• Real number can also be displayed in exponential notation as per the specification: %w.p e
• The display takes the form: [ - ] m.nnnne[ ± ]xxx
• Here, the length of the string of n’s is specified the precision p, default is 6
• The field width w>=p+8
SAMPLE PROGRAM
//use of printf() for real numbers
#include<stdio.h>
int main()
{
float y=98.7654;
printf("y=%f\n",y);
printf("y=%7.4f\n",y);
printf("y=%7.2f\n",y);
printf("y=%-7.2f\n",y);
printf("y=%e\n",y);
printf("y=%10.2e\n",y);
printf("y=%11.4e\n",y);
return 0;
}
FORMATTED OUTPUT – SINGLE CHARACTER AND
STRINGS
return 0;
}