2. Use of printf() and scanf()
printf() is abbreviation of Print formatted
Print out something on the monitor
printf(“format string..”, argument list);
[Ex]
#include <stdio.h>
main() {
int n ;
n = 25 * 10;
printf(“%d”, n); /* print 250. */
}
[Ex]
printf(“%c%c%c”, ’a’, ’b’, ’c’ ); /* print “abc”. */
printf(“%s”, “def” ); /* print “def”. */
2
3. Use of printf() and scanf()
Conversion characters (format string)
%c character printf(“%c”, ’a’);
%d decimal integer printf(“%d”, 100);
%x Hexadecimal integer printf(“%x”, 100);
%o Octal integer printf(“%o”, 100);
%u Unsigned decimal printf(“%u”, 100);
%e floating point number in printf(“%e”, 1.234);
scientific notation
%f floating point number printf(“%f”, 1.234);
%g e-format or f-format printf(“%g”, 1.234);
%s string printf(“%s”, ”C-book”);
3
4. Use of printf() and scanf()
The Use of printf() : integer %d
printf(“ %md”, a ); /*print a left-justified in m width */
printf(“ %-md”, a); /*print a right-justified in m width */
[Ex]
a = 12 ;
printf(“%5d%-5d%dn” ,a, a, a+11);
12^^^^^^1223
– %5d : 12^^^
– %-5d : ^^^12
– %d : 23
4
5. Use of printf() and scanf()
The Use of printf() : float %f
printf(“ %m.pf”, a ); /*print a left-justified in m width
with p numbers after decimal point*/
printf(“ %-m.pf”, a); /*print a right-justified in m width
with p numbers after decimal point*/
[Ex] 83.1260^^83.126083.126000^^^83.13
a = 83.126 ;
printf(“%8.4f%-8.4f%f%-7.2fn”, a, a, a, a );
– %8.4f : 83.1260^
– %-8.4f : ^83.1260
– %f : 83.126000
– %-7.2f : ^^^83.13
5
6. Use of printf() and scanf()
scanf() is abbreviation of Scanf formatted
Reads data from standard input
scanf(“format string..”, argument list);
%c character scanf(“%c”, &a);
%d decimal integer scanf(“%d”, &a);
%f floating point number scanf(“%f”, &a);
%lf floating point number scanf(“%lf”, &a);
%Lf floating point number scanf(“%Lf”, &a);
%s string scanf(“%s”, &a);
6
7. Use of printf() and scanf()
[Ex]
#include <stdio.h>
main() {
int n ;
printf(“Enter number : “);
scanf(“%d”, &n);
printf(“You entered : %d”, n);
return 0;
}
Enter number : 10
You entered : 10
7