Table of Contents
show
A format specifier specifies the format according to which the printing will be done. There is different format specifier for each data type.
Syntax
%x
Where x is a character code
Some elements that affect format specifier are:
- A minus symbol (-) sign tells left alignment
- A number after % specifies the minimum field width. If the string is less than the width, it will be filled with spaces
- A period (.) is used to separate field width and precision.
- A precision explains how many digits come after the decimal part in floating number, number of digits in integer, and number of characters in the string.
- Width is an optional argument which specifies minimum number of position in the output.
- If data needs more space than specified, then printf overrides the width specified by the user
- If number of output characters is smaller than the specified width, then the output would be right justified with blank spaces to the left.
#include<stdio.h>
int main()
{
int a = 32415;
printf("%9d\n",a);
printf("%2d\n",a);
printf("%-9d\n",a);
printf("%09d\n",a);
return 0;
}
Output
C:\TDM-GCC-64>gcc FS12.c -o FS12
C:\TDM-GCC-64>FS12
32415
32415
32415
000032415
- scanf( ) function stands for scan formatting
- scanf( ) function is used to read data from the keyboard.
Syntax
scanf(“control string”,arg1,rg2,arg3,…,argn);
The control string specifies the type and format of the data that has to be obtained from the keyboard and stored in the memory locations pointed by arguments arg1, arg2,…, argn, i.e., the arguments are actually the variable addresses where each piece of data are to be stored
The prototype of control string is,
%[*][width][modifier]type
* is an optional argument (data will be read but not stored in memory location)
Rules for using scanf( ) function:
- The name of scanf() function should be in lowercase
- The inputs (or arguments) to scanf function are given within parentheses
- The first input to the scanf function is always a format string
- The inputs are separated by commas
- The inputs following the first input should denote l-value, represented using & operator
#include<stdio.h>
int main()
{
int a,b;
printf("\n Enter a five digit number");
scanf("%2d%4d",&a,&b);
printf("a=%d, b= %d",a,b);
return 0;
}
Output
C:\TDM-GCC-64>gcc scan_1.c -o s1
C:\TDM-GCC-64>s1
Enter a five digit number 34567 45678
a=34, b= 567
C:\TDM-GCC-64>
Views: 0