[C] printf & scanf

· 3 min read · (562 words)
  • printf (format)

alt text

  • scanf


printf

specifier

%[flag][width][.precision][length]specifier

Integer

  • %d, %i: signed int
    • %i accept oct(8) or hex(16) in scanf; while %d read as dec(10)
    • there’s no %D
  • %u: unsigned int
  • %o: unsigned int, in oct
    • oct: 0___
    • there’s no %O (no letter as hex, eg. ‘F’)
  • %x, %X: unsigned int, in hex
    • hex: 0X___

Floating number

  • %f, %F: double & float @?
    • %f: inf, infinity, nan
    • %F: INF, INFINITY, NAN
    • @ %lf: double
    • @Note: 123.0f–>float; 123.0–>double
  • %e, %E: double & float in exponential notation
  • %g, %G: double & float in either normal(%f) or exponential(%e) format
  • @%a, %A: hexadecimal floating-point

@Experiment

  double dp = 123.456;
  printf("%%f: %f\t\t\t%%F: %F\n", dp, dp);
  printf("%%e: %e\t\t%%E: %E\n", dp, dp);
  printf("%%g: %g\t\t\t%%G: %G\n", dp, dp);
  printf("%%a: %a\t%%A: %A\n\n", dp, dp);
  • Result
  %f: 123.456000                  %F: 123.456000
  %e: 1.234560e+02                %E: 1.234560E+02
  %g: 123.456                     %G: 123.456
  %a: 0x1.edd2f1a9fbe77p+6        %A: 0X1.EDD2F1A9FBE77P+6

Others

[flags]

  • +: print + for positive number.

    • Eg. printf("%+d\n", 10); //output: +10
  • -: Left-align (default: right-align)

  • 0: left-pads number with zeros.

    • Eg. printf("%06d \n", 1234); //output: 001234
  • #: o, x, X (oct, hex) lead with (0, 0x)

[width]

  • Minimum char to be printed.

    • Eg. printf("%06d \n", 1234); //output: 001234 --> Width=6
  • *: width value pass as an an argument.

    • Eg 1.

      printf("Width trick: %0*d \n", 5, 10); // pad with '0'
      printf("Width trick: %+*d \n", 5, 10); //'+' for positive
      printf("Width trick: %-*d \n", 5, 10); // left-align
      printf("Width trick: %*d \n", 5, 10);  // '*' pass as argument
      
    • Output. alt text

    • Eg 2. + can be used with (0 <-> -)

      printf("%0+8.2lf\n", 3.14567);
      printf("%-+8.2lf\n", 3.14567);
      printf("%+8.2lf\n", 3.14567);
      
    • Output. alt text

[.precision]

  • Digits printed behind the dot(.)
    • Eg. printf("%05.2f %+.0e %E\n", 3.1416, 3.1416, 3.1416); //output: 03.14 +3e+00 3.141600E+00
  • Precision can be passed as arguments as well.
    • Eg. printf("%0*.*f\n\n",10,1, 12.455); //output: 00000012.5

[length] @?

  • h: int –> char
  • hh: int –> short
  • l: int –> long
  • ll: int –> long long

Special Character

  • \t: tab
  • \n: newline
  • \0: null char
    • Eg. printf("before null \0 after null\n"); //output: before null [not newline, same line]
  • \b: backspace
    • ASCII = 8
    • Eg. printf("code\b academy\n"); //output: cod academy
  • \r: carriage return
    • ASCII = 13
    • Eg. printf("dog lover\rkitty\n"); //output: kittyover
  • \f: formfeed
    • ASCII = 12
    • Eg. printf("abcde\fghi\ffff\n");
    • Output. alt text
  • \a: alert beeping sound.

Usage

sprintf

dd

snprintf

d

Ref

  1. printf format string - Wikipedia
  2. printf 引數說明 - Edison.X.
  3. printf - C++ Reference
  4. Tips on printf - MIT

@Examples & Questions

  • Rounding (四捨五入) for floating point precision.

    • Eg.

      printf("%.1f\n\n", 12.44);  //output: 12.4
      printf("%.1f\n\n", 12.45);  //*** output: 12.4 ***
      printf("%.1f\n\n", 12.4500001);  //output: 12.5
      
    • Interesting … Hmmm…

  • printf different types (unsigned, dec, oct, hex)

    • Eg.

      int n = -15;
      int p = 15;
      printf("%d %u %#o %X\n", p, p, p, p);  //output: 15 15 017 F
      printf("%d %u %#o %#X\n", n, n, n, n);  //output: -15 4294967281 037777777761 0XFFFFFFF1
      
    • Result.

    dec unsigned dec %u unsigned oct %o unsigned hex %X
    15 15 017 F
    -15 4294967281 037777777761 0XFFFFFFF1
    (+) 4294967296 040000000000 0X100000000


  • Usage: sprintf

AAA



  • printf, puts, scanf, gets –> gets_s()

BBB



scanf

Ref

  1. [C] scanf 引數說明 - Edison.X.
  2. [C&++]scanf進階用法 - Edison.X

@TODO