Formatted Output with printf()
printf() can be used to output data of different types in different formats such as perform rounding, aligning columns, right/left justification, inserting literal characters, exponential format, hexadecimal format, and fixed width and precision.
printf(format-string, arg1, arg2, arg3,...........,argn)
The precision specifier
It determines the number of decimal places displayed.
#include<stdio.h>
int main()
{
float a=5.231457865;
printf("The value of a is %.2f",a);
return 0;
}
Formatted Input with scanf()
scanf() reads characters from the standard input, interprets them according to the specification in format-string and stores the results in the remaining arguments arg1, arg2, ....., argn each of which must be a pointer that indicates where the corresponding converted input should be stored.
Reading Itegers
%d or %i %o %0x
Reading Floating-point values
Format specifier= %f %e %g
Reading string
Format specifier= %s
Reading single character
Format specifier = %c
getchar() and putchar()
getc() and putc()
getch() and putch()
getche()
Formatted Input with scanset
The set of characters enclosed in square brackets [ ] is preceded by a % sign. It scans input stream, looking only for characters in scan set.
Use of Caret ^: [^aeiou]
It does not scan the character inside the square bracket.
for eg:
#include<stdio.h>
int main()
{
char n[50];
printf("Enter a string: ");
scanf("%[aeiou]",n);
printf("%s",n);
return 0;
}
Output:
Enter a string: afhehihdolu
aeiou
#include<stdio.h>
int main()
{
char n[50];
printf("Enter a string: ");
scanf("%[^aeiou]",n);
printf("%s",n);
return 0;
}
Output:
Enter a string: afhehihdolu
fhhhdl
Download the PDF file
-----------------------------------------------------------------------------------------------------------------------------
The End
-----------------------------------------------------------------------------------------------------------------------------
No comments
If you have any doubts, Please let me know,