200 likes | 298 Views
C Formatted Input/Output. /* Using Integer Conversion Specifiers */ #include <stdio.h> int main ( ) { printf( "%d<br>", 455 ); printf( "%i<br>", 455 ); printf( "%d<br>", +455 ); printf( "%d<br>", -455 ); printf( "%hd<br>", 32000); printf( "%ld<br>", 2000000000 );
E N D
C Formatted Input/Output /* Using Integer Conversion Specifiers */ #include <stdio.h> int main ( ) { printf( "%d\n", 455 ); printf( "%i\n", 455 ); printf( "%d\n", +455 ); printf( "%d\n", -455 ); printf( "%hd\n", 32000); printf( "%ld\n", 2000000000 ); printf( "%d\n", 455 ); printf( "%o\n", 455 ); printf( "%u\n", 455 ); printf( "%u\n", -455 ); printf( "%x\n", 455 ); printf( "%X\n", 455 ); return 0; } (week04 specifier.c)
What we observed d --- is same as i in printf o --- Display an unsigned octal number u --- Display an unsigned decimal integer X , x --- unsigned hexadecimal 0-9 a-f or A-F h or l --- short or long integer
Example 2 • /* Using Integer Conversion Specifies */ • #include <stdio.h> • int main ( ) • { • printf( "%e\n", 1234567.89 ); • printf( "%e\n", +1234567.89 ); • printf( "%e\n", -1234567.89 ); • printf( "%E\n", 1234567.89 ); • printf( "%f\n", 1234567.89); • printf( "%g\n", 1234567.89 ); • printf( "%G\n", 1234567.89 ); • return 0; • } (week04 spe2.c)
From this Example E or e --- Display a floating-point in exponential notation. f --- Display floating-point values G or g --- Display a floating-point value in either floating-point formf or e (or E) (week04 spe3.c) L --- Place before any floating-point to indicate a long double For g and G, default is 6 significant digits.
In general • Conversion specifications begin with the % character and contain these optional and required elements: • Flags (optional) • Width and precision fields (optional) • A subtype specifies (optional) • Conversion character (required)
Flags • Character Description Minus sign (-) -- left-justifies the converted argument in its fieldPlus sign (+)--Always prints a sign character (+ or -)Space characterInserts a space before the valueZero (0)Pads with zeros rather than spaces
Field Width and Precision Specifications • Character Description • Field width -- A digit string specifying the minimum number of digits to be printed %6fPrecision-- A digit string including a period (.) specifying the number of digits to be printed to the right of the decimal point%6.2f
Caraacters and strings • /* Using Integer Conversion Specifiers */ • #include <stdio.h> • int main ( ) • { • char character = 'A'; • char string[ ] = "This is also a string"; • const char *stringPtr = "This is also a string"; • printf( "%c\n", character ); • printf( "%s\n", "This is a string" ); • printf( "%s\n", string ); • printf( "%s\n", stringPtr ); • return 0; • } (week04 intspe.c)
Print strings and characters • c and s are used to print individual characters and strings • c- requires a char argument • s- requires a pointer to char as an argument
Common programming Error • 1. Using %c to print a string • 2. Using %s to print a character • 3. Using single quotes around character string is a syntax error. • 4. Using a double quotes around a character constant
More about c in/out format • %p --- Display a pointer value in an • implementation-defined manner. • %n --- store the number of characters already output in the current printf statement. (including all blanks, too) • %% Display the percent character.
Example • /* Using Integer Conversion Specifiers */ • #include <stdio.h> • int main ( ) • { • int *ptr; • int x = 12345, y; • ptr = &x; • printf( "The value of ptr is %p\n", ptr ); • printf( "The address of x is %p\n\n", &x ); • printf( "Total characters printed on this line is:%n", &y ); • printf( " %d\n\n", y ); • y = printf( "This line has 28 characters\n" ); • printf( “%d characters were printed\n\n", y); • printf( "Printing a %% in a format control string\n" ); • return 0; • } (week04 spe5.c)
More about numbers’ format • %d 37 -37 • %.4d 0037 –0037 • %11d 37 -37 • %11.4d 0037 -0037 • %-11 37 -37 • %-11.4 0037 -0037 • %011d 00000000037 -00000000037 • The default is right justified. By using ‘–’ right after % will make the field left justified.
Printing Literals and Escape Sequences • \’ • \” • \? • \\ • \a Cause an audible (bell) or visual alert. • \b Move the cursor back one position on the current line. • \f Move the cursor to the start of the next logical page. • \n • \r More the cursor to the beginning of the current line. • \t More the cursor to the next horizontal tab position. • \v Move the cursor to the next vertical tab position.
Program Example • #include <stdio.h> • int main ( ) • { • int month1, day1, year1, month2, day2, year2; • printf( "Enter a date in the form mm-dd-yyyy: "); • scanf( "%d%*c%d%*c%d”, &month1, &day1, &year1 ); • printf( "month = %d day = %d year = %d\n", month2, day2, year2 ); • printf( "Enter a date in the form mm/dd/yyyy: " ); • scanf( "%d%*c%d%*c%d", &month2, &day2, &year2 ); • printf( "month = %d day = %d year = %d\n", month2, day2, year2 ); • return 0; • } • Notice the *in front of c% as an • Assignment Suppression Character to discard characters from input. (week04 spe6.c)
More Examples • Cs315f06/week04 on cslinux2.cs.semo.edu