Format Specifiers
Format Specifier अक्षरो का समूह होता है जो किसी datatype को प्रदर्शित करता है । Format Specifier प्रतिशत चिन्ह के साथ एक या अधिक अक्षरो का मिश्रण होता है ।
A Format Specifier is a group of characters which represent a data type within a string. A format specifier is a combination of a percent sign with one or more characters. Format Specifiers are used with printf( ) and scanf( ) to specify the type and amount of input and output respectively. Here is a list of format specifiers with their meanings - corresponding to every data type.
string containing format specifiers is called a format string. For example, the string “%d %f %c” is a format string which represents an integer, a float and a character respectively.
A format string is used both in printf( ) and scanf( ). How it is used, is described next.
Format Specifiers with printf( )
printf(“format sring”, list of variables);
Here is an example.
int i = 10;
printf("Value of i = %d", i);
The first statement declares an integer variable i initialized with value 10. Next, the printf( ) contains a format string “Value of i = %d”. The printf( ), when display this string, will not output the %d specifier, instead it will replace the specifier with the value of variable i, provided in the printf( ), just after the format string. So the output of this code would be –
Value of i = 10
Also, the printf( ) will not display the i after the format string. It only outputs the contents of format string.
The process can be easily understood with the help of following diagram –
printf(“Value of i = %d”, i);
The same method can be applied for more than variables. Like this—
int i = 10,j = 20,k = 30;
printf(“i = %d j = %d k = %d”,i,j,k);
The output would be —
i = 10 j = 20 k = 30
This output can be understood with the help of following diagram—
printf(“i = %d j = %d k = %d”,i,j,k);
Variables of different type can also be mixed in printf( ) –
int i = 10;
char ch = ‘$’;
float x = 67.25f;
double y = 825.75;
printf("i = %d ch = %c x = %f y = %lf",i,ch,x,y);
The output would be –
i = 10 ch = $ x = 67.25 y = 825.75
A Complete Program for Illustration
Here is a complete program to demonstrate the use of all format specifiers.
int main(void)
Output :
ch = V x = 10 y = 56.250000 z = 72.750000
Address of a Variable
Format Specifiers with scanf( )
scanf(“format string”, list of addresses);
The format string describes the number, type and order of values which will be input through keyboard and the list of addresses describes the locations (i.e. the variables) where the values will be stored.
For Example, if i is an integer variable declared as int i; then to input the value into i, scanf( ) will be used like this-
scanf("%d",&i);
Here %d in format string specifies that an integer value is being input through the keyboard and it will be stored at the address of i, that is, in the i itself.
To input three integer values for variables a, b, c of type integer, scanf( ) will be used like this—
scanf("%d %d %d",&a,&b,&c);
the first value will be stored in a, second in b and third in c.
Variables of different type can also be mixed. For example consider three variables declared as –
char ch;
int i;
float x;
to input values for ch, i and x through keyboard, the scanf( ) will be used like this –
scanf("%c %d %f",&ch,&i,&x);
Here %c specifies that the first value is a character, %d specifies that second an integer, %f specifies that third a float and they will be stored in ch, i and x respectively.
Remember, variables are not used directly in scanf( ) rather they are used with a & operator. So i must be used as &i, ch as &ch and x as &x.
Program to Demonstrate the use of printf( ) and scanf( )
This program adds two numbers, input through the keyboard. First it declares three variables. Then a request is made to the user using printf( ) to input two numbers. The input operation is performed using scanf( ). The two numbers are placed in two variables and the third holds the sum of them. Finally, the sum is displayed using printf( ).
int main(void)
Output :
Please note that the two values 10 and 20, inputted through the keyboard here, are separated by space
You can use space, tab or Enter key to separate them as prescribed.
The statement
c = a + b;
First adds the values of a and b and then stores the result into c.
The Art of Writing a format string in scanf( ) and the cause of problems
Format Specifier अक्षरो का समूह होता है जो किसी datatype को प्रदर्शित करता है । Format Specifier प्रतिशत चिन्ह के साथ एक या अधिक अक्षरो का मिश्रण होता है ।
A Format Specifier is a group of characters which represent a data type within a string. A format specifier is a combination of a percent sign with one or more characters. Format Specifiers are used with printf( ) and scanf( ) to specify the type and amount of input and output respectively. Here is a list of format specifiers with their meanings - corresponding to every data type.
Data type
|
Format
Specifier
|
Meaning
|
Character
|
%c
|
c stands for character
|
Integer
|
%d or
%i
|
d stands for
decimal, i stands for integer
( %d is mostly used) |
Float
|
%f
|
f stands for
float
|
Double
|
%lf
|
lf stands for
long float (the second name for double)
|
string containing format specifiers is called a format string. For example, the string “%d %f %c” is a format string which represents an integer, a float and a character respectively.
A format string is used both in printf( ) and scanf( ). How it is used, is described next.
Format Specifiers with printf( )
When a format string is used in printf( ) it may also use plain text with specifiers. In printf( ), just after the format string, there is a comma separated list of variables whose type, number and order exactly matches with the format specifiers used within format string. printf( ) does not output the format specifiers, instead it outputs the values of the variables present in the list, corresponding to the specifiers. The general form of using printf( ) in this way is –
Here is an example.
int i = 10;
printf("Value of i = %d", i);
The first statement declares an integer variable i initialized with value 10. Next, the printf( ) contains a format string “Value of i = %d”. The printf( ), when display this string, will not output the %d specifier, instead it will replace the specifier with the value of variable i, provided in the printf( ), just after the format string. So the output of this code would be –
Value of i = 10
Also, the printf( ) will not display the i after the format string. It only outputs the contents of format string.
The process can be easily understood with the help of following diagram –
printf(“Value of i = %d”, i);
The same method can be applied for more than variables. Like this—
int i = 10,j = 20,k = 30;
printf(“i = %d j = %d k = %d”,i,j,k);
The output would be —
i = 10 j = 20 k = 30
This output can be understood with the help of following diagram—
Variables of different type can also be mixed in printf( ) –
int i = 10;
char ch = ‘$’;
float x = 67.25f;
double y = 825.75;
printf("i = %d ch = %c x = %f y = %lf",i,ch,x,y);
The output would be –
i = 10 ch = $ x = 67.25 y = 825.75
A Complete Program for Illustration
Here is a complete program to demonstrate the use of all format specifiers.
/* Format Specifiers */
#include<stdio.h>
#include<conio.h>
int main(void)
{
char ch='V';
int x=10;
float y=56.25f;
double z=72.25;
clrscr();
printf("ch = %c x = %d y = %f z = %lf",ch,x,y,z);
getch();
return 0;
}
ch = V x = 10 y = 56.250000 z = 72.750000
Please note that when a character constant is shown on the screen the single quotes are not displayed. That is, ’V’ is shown as V. In case of a float constant the suffix f (or F) is not displayed. Therefore, the value 56.25f is displayed as 56.250000. Here extra zeros are added by the program itself. All Real Values are shown up to six places of decimals.
Address of a Variable
Every variable in our program has an “address”. Actually, the address of the variable is the address of the memory occupied by it in runtime. Because variables come into existence during the execution of the program, therefore, while coding programs you don’t – and – can’t know in advance that what will be the address of a variable. In our programs, the address of a variable is denoted by prefixing a & (ampersand) with it. For example, if i is a variable then the address of i will be written as &i. This & is called address of operator. That is, it gives the address of the variable attached to it.
Format Specifiers with scanf( )
Suppose you want to make a program that asks you to input some values and you supply the values through keyboard. For this purpose we use scanf( ) function. The name scanf stands for scan format. The scanf( ) function enables a user to input the value of one or more variables through keyboard. It requires two things – a format string that specifies the type and number of values, and a comma separated list of variables’ addresses. Therefore the general form using scanf( ) is –
scanf(“format string”, list of addresses);
The format string describes the number, type and order of values which will be input through keyboard and the list of addresses describes the locations (i.e. the variables) where the values will be stored.
For Example, if i is an integer variable declared as int i; then to input the value into i, scanf( ) will be used like this-
scanf("%d",&i);
Here %d in format string specifies that an integer value is being input through the keyboard and it will be stored at the address of i, that is, in the i itself.
To input three integer values for variables a, b, c of type integer, scanf( ) will be used like this—
scanf("%d %d %d",&a,&b,&c);
the first value will be stored in a, second in b and third in c.
Variables of different type can also be mixed. For example consider three variables declared as –
char ch;
int i;
float x;
to input values for ch, i and x through keyboard, the scanf( ) will be used like this –
scanf("%c %d %f",&ch,&i,&x);
Here %c specifies that the first value is a character, %d specifies that second an integer, %f specifies that third a float and they will be stored in ch, i and x respectively.
Remember, variables are not used directly in scanf( ) rather they are used with a & operator. So i must be used as &i, ch as &ch and x as &x.
Program to Demonstrate the use of printf( ) and scanf( )
This program adds two numbers, input through the keyboard. First it declares three variables. Then a request is made to the user using printf( ) to input two numbers. The input operation is performed using scanf( ). The two numbers are placed in two variables and the third holds the sum of them. Finally, the sum is displayed using printf( ).
#include<stdio.h>
#include<conio.h>
int main(void)
{
int a,b,c;
clrscr();
printf("Enter two numbers:");
scanf("%d %d",&a,&b);
c = a + b;
printf("Sum of numbers = %d",c);
getch();
return 0;
}
Enter two numbers:10 20
Sum of numbers = 30
Please note that the two values 10 and 20, inputted through the keyboard here, are separated by space
You can use space, tab or Enter key to separate them as prescribed.
The statement
c = a + b;
First adds the values of a and b and then stores the result into c.
The Art of Writing a format string in scanf( ) and the cause of problems
NEXT : Operator & Expression
PREVIOUS : Rules for variable
सी भाषा शुरुआती प्रोग्रामर के लिए छोटे प्रोग्राम कोड
ReplyDeleteTermfo डेटाबेस सी प्रोग्राम का उपयोग करना