1 / 17

C Formatted Input/Output

C Formatted Input/Output. /* Using Integer Conversion Specifiers */ #include &lt;stdio.h&gt; int main ( ) { printf( &quot;%d<br>&quot;, 455 ); printf( &quot;%i<br>&quot;, 455 ); printf( &quot;%d<br>&quot;, +455 ); printf( &quot;%d<br>&quot;, -455 ); printf( &quot;%hd<br>&quot;, 32000); printf( &quot;%ld<br>&quot;, 2000000000 );

daniellemay
Télécharger la présentation

C Formatted Input/Output

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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)

  2. 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

  3. 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)

  4. 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.

  5. 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)

  6. You specify these elements in the following order:

  7. 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

  8. 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

  9. 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)

  10. 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

  11. 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

  12. 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.

  13. 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)

  14. 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.

  15. 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.

  16. 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)

  17. More Examples • Cs315f06/week04 on cslinux2.cs.semo.edu

More Related