6 | I/O Statements
6.0 | Contents :
1. Formatted Input : scanf()
2. Formatted Output : printf()
3. Examlpes of C Programs
6.1 | Formatted Input : scanf()
The function scanf() is used to take values from user. This funtion takes codeious types of values like characters, strings and digits etc.
The general format is :
scanf("string's format / format specifier", argument lists);
The following table shows which format specifier is used with which data-type :
Format Specifier | Data-type | Meaning |
---|---|---|
%c | char | Read a single character |
%d or %i | int | Read a decimal integer |
%x | short, unsigned short, int, unsigned int, long |
Read a hexadecimal integer |
%o | Read an octal integer | |
%u | unsigned int and unsigned long |
Read an unsigned integer |
%f OR %g | float | Read a floating point value |
%e | double | Read a floating point value (even in exponential format) |
Example :
scanf(" %c%d",&m,&n);
Here, the format specifiers %c and %d says that m is a chracter value and n is an integer value respectively.
6.2 | Formatted Output : printf()
The function printf() is used to display statements or values or results etc. in program's output.
The general format is :
printf("control string", argument lists);
Here, control string is consist of any texts to be displayed, format specifiers and special characters.
printf("Hello World !");
Here, Hello World ! to be displayed on the screen i.e. Hello World ! is the output.
Example 2 :printf("The value is %d",m);
(let, variable m is of integer data-type and m=5)
Here, format specifier %d is used, because variable m is an integer. And, The value is 5 to be displayed on the screen i.e. The value is 5 is the output.
6.3 | Examlpes of C Programs :
Example 1 : Display Hello World ! on screen
#include<stdio.h>
void main()
{
clrscr();
printf("Hello World !");
getch();
}
Example 2 : Display Hello World ! on screen
#include<stdio.h>
int main()
{
clrscr();
printf("Hello World !");
getch();
return 0;
}
Example 3 : Addition of two integer numbers
#include<stdio.h>
int main()
{
int a,b,c;
clrscr();
printf("Enter first number :\n");
scanf("%d",&a);
printf("Enter second number :\n");
scanf("%d",&b);
c=a+b;
printf("\nAddition is %d",c);
getch();
return 0;
}
Example 4 : Multiplication of two integer numbers
#include<stdio.h>
int main()
{
int d,e,f;
clrscr();
printf("Enter first and second numbers :\n");
scanf("%d%d",&d,&e);
f=d*e;
printf("\nMultiplication of %d and %d is %d",d,e,f);
getch();
return 0;
}
Example 5 : Display addition, subtraction, multiplication, division and remainder of two integer numbers simultaneously
#include<stdio.h>
int main()
{
int x,y,add,sub,mul,mod;
float div;
clrscr();
printf("Enter first number\n");
scanf("%d",&x);
printf("Enter second number\n");
scanf("%d",&y);
add=x+y;
sub=x-y;
mul=x*y;
div=x/y;
mod=x%y;
printf("\nAddition is %d\n",add);
printf("Subtraction is %d\n",sub);
printf("Multiplication is %d\n",mul);
printf("Division is %f\n",div);
printf("Remainder is %d\n",mod);
getch();
return 0;
}
Comments
Post a Comment