L-29 MCS 275 Fri 23 Mar 2001
Below is the program we discussed in class:
/* L-29 MCS 275 Friday 23 March 2001 exploring options of printf() */
#include <stdio.h>
int main()
{
int i = 75;
float f = 3.1415;
char c = 'A';
int row,col;
char *s = "Friday";
printf("\nSome options of printf() to display a character:\n");
printf(" the character %c, field width 5, left adjusted : \"%-5c\"\n", c, c);
printf(" the character %c, field width 5, right adjusted : \"%5c\"\n", c, c);
printf(" the character %c has ASCII code : %d \n", c, c);
printf("\nSome options of printf() to display an integer :\n");
printf(" the integer %d, field width 5, left adjusted : \"%-5d\"\n", i, i);
printf(" the integer %d, field width 5, right adjusted : \"%5d\"\n", i, i);
printf(" the integer %d, padded with zeros : \"%05d\"\n", i, i);
printf(" the integer %d, as octal number : %o\n", i, i);
printf(" the integer %d, as hexadecimal number : %x\n", i, i);
printf(" the integer %d is ASCII code of : %c\n", i , i);
printf("\nThe ASCII table : \n");
for (row = 0; row < 256/16; row++)
{
for (col = 0; col < 16; col++)
printf("%4c", row*16+col );
printf("\n");
}
printf("\nSome options of printf() to display a float :\n");
printf(" the float %f in scientific notation : %e\n", f, f);
printf(" the float %f with precision 3 : %.3f\n", f, f);
printf(" precision 3, field width 10, right adjusted : \"%10.3f\"\n", f, f);
printf(" precision 3, field width 10, left adjusted : \"%-10.3f\"\n", f, f);
printf("\nSome options of printf() to display a string:\n");
printf(" the string %s, field width 10, right adjusted : \"%10s\"\n", s, s);
printf(" the string %s, field width 10, left adjusted : \"%-10s\"\n", s, s);
printf(" precision 3, field width 10, right adjusted : \"%10.3s\"\n", s, s);
printf(" precision 3, field width 10, left adjusted : \"%-10.3s\"\n", s, s);
return 0;
}