C Formatted InputOutput - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

C Formatted InputOutput

Description:

In f or e style. h or l (int) Put before integer specifier for short or long integers ... b one back n beg. of next line r beg. of current line t next ... – PowerPoint PPT presentation

Number of Views:28
Avg rating:3.0/5.0
Slides: 13
Provided by: williama3
Category:

less

Transcript and Presenter's Notes

Title: C Formatted InputOutput


1
C Formatted Input/Output
  • Formatted output printf
  • Conversion Specifiers
  • Field Widths and Precision
  • Flags
  • Escape Sequences
  • Formatted input scanf
  • Conversion Specifiers

2
Formatted Output - Printf
  • int printf(char format, arg1, arg2, )
  • Translates internal values to characters
  • Returns the number of characters printed

3
Conversion Specifiers
  • d, i (int)
  • Signed decimal integer
  • u (int)
  • Unsigned decimal
  • o (int)
  • Unsigned octal integer
  • x, X (int)
  • Unsigned hexadecimal
  • f (double)
  • m.dddddd
  • e, E (double)
  • m.dddddxx
  • g, G (double)
  • In f or e style
  • h or l (int)
  • Put before integer specifier for short or long
    integers

4
Conversion Specifiers
  • c (int)
  • Single character
  • s (char )
  • Character strings
  • p (void )
  • pointer
  • Print a

5
Field Widths and Precision
  • Between and conversion character
  • Number for minimum field width
  • Period to separate field width from precision
  • Number for the precision
  • Strings maximum number of characters
  • Number of digits after the decimal point
  • Minimum number of digits for an integer

6
Flags
  • Between and conversion character
  • Minus sign (-) for left justification
  • Plus sign () to display plus or negative sign
    for numbers
  • Space ( ) put a space before positive number
  • Pound sign () to put 0 for octal, 0x for
    hexadecimal, force a decimal point for floats
  • Zero (0) to pad a field with leading zeros

7
Example Code
  • int main()printf("9s9d9c9f\n","aloha",5,'Z',
    5.67)
  • printf("-9s-9d-9c-9f\n",
  • "aloha",5,'Z',5.67)
  • printf("-9.3s-9.3d-9.3c-9.3f\n",
  • "aloha",5,'Z',5.67)
  • return 0
  • /
  • aloha 5 Z 5.670000
  • aloha 5 Z 5.670000
  • alo 005 Z 5.670
  • / (See example1.txt)

8
Example Code
  • include ltstdio.hgt
  • int main()
  • int x 64
  • double y 64.0
  • printf( "o\n", x ) /0100/
  • printf( "x\n", x ) /0x40/
  • printf( "X\n", x ) /0X40/
  • printf( "g\n", y ) /64.0000/
  • printf( "E\n", y ) /6.400000E01/
  • return 0
  • (See example2.txt)

9
Escape Sequences
  • Move the cursor
  • \b one back
  • \n beg. of next line
  • \r beg. of current line
  • \t next horizontal tab
  • Output respective character
  • \ \ \? \\
  • Annoying alert sound
  • \a

10
Formatted Input - Scanf
  • int scanf(char format, arg1, arg2, )
  • Read characters and store by format
  • Arguments must be pointers
  • Returns the number of successfully matched and
    assigned input items
  • Stops when format string ends, or if input does
    not match input specification
  • Skips blanks and tabs

11
Conversion Specifiers
  • Reading integers (put before character)
  • d signed decimal
  • i signed octal, decimal, or octal
  • o an octal value
  • u unsigned decimal
  • X, x hexadecimal
  • h or l place before integer specifier for short
    long integers

12
Example Code
  • include ltstdio.hgt
  • int main()
  • int m0 float d0 char y10 0
  • printf( "Enter a date (mm dd yyyy) " )
  • scanf( "d f s", m, d, y )
  • printf("monthd dayf years\n", m, d, y
    )
  • return 0
  • /
  • Enter a date (mm dd yyyy) 3 3 2003
  • month3 day3.000000 year2003
  • Enter a date (mm dd yyyy) 3-3-2003
  • month3 day-3.000000 year-2003
  • Enter a date (mm dd yyyy) 3/3/2003
  • month3 day0.000000 year
  • / (See
    example3.txt)
Write a Comment
User Comments (0)
About PowerShow.com