Format Specifier In C Programming Btech BCA Notes
Welcome, Hope you are doing well. Today we are talking about Foramat specifier in C language. We can also say Type Specifier In C.
So, Today we are read formate specifier as well as printf and scanf Formats in different situations and different conditions.
Before you read this article May you also like our previous posts:-
Beginner our view on C programming Language
Structure Of C Language Program
Variables & Datatypes In C Language . It may help you to boost Your Information.
In C Programming language, Types specifier is a special piece
of code which is used during the input and output. Type specifier inform the compiler
about the type of data which will be entered or displayed during an input
output operation. Input mans reading data from keyword while output means
displaying data either on screen or a printer.
In c – Language every datatype has its own type specifier:
Name | size | Type Specifier |
---|---|---|
Int | 2 | %d |
Char | 1 | %c |
Long | 4 | %ld |
Double | 8 | %lf |
Float | 4 | %f |
Long Double | 10 | %Lf |
* Type specifier are used while performing formatted input /
output
* Input can be through keyboard only and output monitor
printer.
* This type of specifier is not used to print simple English statement.
Eg : Hello / Etc
Handling Input / Output In C Language
Printf(print Formatted)
Printf() formats the data according to the formate specifier. Printf() is the the most versatile method of displaying output on screen.
Syntax :-
The first form of printf() is used to display a string. Provide the string in double codes.
For Eg :-
syntax:-
The Second form of printf() is used to display value of variable. Whenever value of variable is to be display, we first specifie the type specifier of the variable and then after a, We specifie the variable name.
For Eg :-
Int b=200;
Int c= a+b;
Printf(“%d”,c);
Various use of printf
· A single printf can be used to print multiple variables. This done by providing n-types specifiers followed by equal number of variables.
Syntax :
For Ex : suppose our output is 10 20 . We take a=10 & b=20, So we print them as below.
float b=20;
printf(“%d,%f”, a,b);
· Printing a string and a value of variable together.
Output:- The sum of 10 and 20 = 30 {Where:- a=10 b=20 c=30}
Code:-
We can print value of a variable and a string by specifying the string and type specifier both in the double codes.
After double codes apply a comma and then supply list of variable whose value is to be printed.
Output:- 10*5=50 { where a=10 b=5 & c=50 }
Code:-
Output:- The sq of 8=64.thanks
Code:-
Scanf (scan formatted)
Syntax :-
Scanf() accepts two arguments. The first argument is the type specifier of the variable to be printed. The second argument is the variable name.
For Eg :-
Printf(“enter a number”);
Scanf(“%d”,&x);
Various use of scanf
· Using scanf to fetch multiple value :-h6yn 04 n
Syntax :-
Ex :- fetch multiple value
Float y;
Scanf(“%d%f”, &x,&y);
A single scanf can be used to read multiple values. To do so, first provide the required number of type specifiers in double codes and then provide a comma. After the comma supply a list of variables in which value is to be stored.
Syntax :-
For Ex :-
Double a;
Scanf(“%f%lf”,&d,&a);
Sample Code:-
//Area of Rectangle
#include<conio.h>
#include<stdio.h>
void main()
{
int l,b,r:
clrscr();
printf(“enter l and b”);
scanf(“%d”,&l,&b);
r=l*b;
printf(“the area is %d”,r);
getch();
}
Comments
Post a Comment