Input/output Statements and Operators
Reading data from input device and displaying the result on the screen are the two main tasks of any programming. In ‘C’ language the input output operations are carried out by using standard functions. The programs for these functions are kept in the file. This file must be included in the program to use these functions using the preprocessor directive as:
#include<stdio.h>
The i/o functions are classified into 2 types:
1) Formatted functions
2) Unformatted functions.
Formatted input output function The formatted I/O functions read and write all data values.
Formatted Input:
Scanf():
This formatted input function which read the data from the standard input into variable in different formats by the user.
Syntax:
Input single value from user scanf(“ control string”, &args);
Input multiple values scanf(“ control string”, &arg1, &arg2, …. ,&argn);
Example:
Input single value from user int no; scanf(“%d” ,&no);
Input multiple value float no 1; int no 2; scanf(“%f%d”, &no1, &no2);
Note:
It reads all types of data values.
It is used for run time assignment of variable.
The & (ampersand) symbol is used to indicate the memory location of the variable. So that the value read would be placed at that location.
The control string may contain format code, escape sequence & width specifiers.
The format code specifies the type of input data.
For each variable there must be a format code.
Code Meaning
%c Read a single character
%d Read decimal integer
%f Read a floating point value
%s Read a string
%o Read octal integer
%x Read hexadecimal integer
%u Read an unsigned decimal integer
Example:
Write a program to add two numbers and take input from user
#include<stdio.h>
#include<conio.h>
void main()
{
int no1,no2,no3;
clrscr();
printf("Enter two numbers\n");
scanf("%d%d",&no1,&no2);
no3=no1+no2;
printf("sum is %d",no3);
getch();
}
Example:
Write a program to find out Total marks.(prog2.c)
#include<stdio.h>
#include<conio.h>
void main()
{
int m1,m2,m3,m4,m5;
int total;
clrscr();
printf("Enter marks1 ");
scanf("%d",&m1);
printf("Enter marks2 ");
scanf("%d",&m2);
printf("Enter marks3 ");
scanf("%d",&m3);
printf("Enter marks4 ");
scanf("%d",&m4);
printf("Enter marks5 ");
scanf("%d",&m5);
total =m1+m2+m3+m4+m5;
printf("Total marks is %d\n",total);
getch();
}
Note:
Field width w can be used with scanf() function.
Inputting Integer Number
The field specifier %wd is used to read fixed width integers. Here w is the width of data needs to be read and store into a particular variable. Any unread data in a line is considered as input to the next scanf() call.
Example:
Scanf(“%2d %5d”, &no1, &no2);
Input data is 30 23499 Then the value 30 is assign to no1 And 23499 is assigning to no2.
input data is 23499 30 Then variable no1 will assigned 23(because %2d) And no2 will assign 499. The value 30 that is unread will be assigned to the first variable in next scanf().
Example: Use of field Width with scanf(). (prog3.c)
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y;
clrscr();
printf("Enter the value of x and y");
scanf("%3d%2d",&x,&y);
printf("------------------");
printf("%d\n%d",x,y);
getch();
}
Output:
Execution 1:
234 // reads 3digit integer into x with field width of 3 (%3d)
56 // reads 2digit integer into y with field width of 2 (%2d)
234
56
Execution 2:
345 78 // reads 3digit integer into x and 78 is input to next scanf() call
345
78
Execution 3:
2654 87 // reads 3digit integer 265 into x and 4 is input to next scanf()call
265
4
Execution 4:
6534 //reads 3digit integer 653 into x and 4 is input to next scanf() call
653
4
Comments
Post a Comment