- printf (format)
- scanf
printf
specifier
%[flag][width][.precision][length]specifier
Integer
%d,%i: signed int%iaccept oct(8) or hex(16) inscanf; while%dread as dec(10)- there’s no
%D
%u: unsigned int%o: unsigned int, in oct- oct:
0___ - there’s no
(no letter as hex, eg. ‘F’)%O
- oct:
%x,%X: unsigned int, in hex- hex:
0X___
- hex:
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
%c: char%s: (null-terminated) string%p: pointer address- Ref.2:
int a=0, printf("%p", &a);==printf("%08x", &a); - @ %p v.s. %x
- Ref.2:
- @
%n: ?? 1. nothing? 2. buffer?
[flags]
+: print + for positive number.- Eg.
printf("%+d\n", 10); //output: +10
- Eg.
-: Left-align (default: right-align)0: left-pads number with zeros.- Eg.
printf("%06d \n", 1234); //output: 001234
- Eg.
#: o, x, X (oct, hex) lead with (0, 0x)
[width]
Minimum char to be printed.
- Eg.
printf("%06d \n", 1234); //output: 001234 --> Width=6
- Eg.
*: 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 argumentOutput.
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.
[.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
- Eg.
- Precision can be passed as arguments as well.
- Eg.
printf("%0*.*f\n\n",10,1, 12.455); //output: 00000012.5
- Eg.
[length] @?
h: int –> charhh: int –> shortl: int –> longll: 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]
- Eg.
\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.
\a: alert beeping sound.
Usage
sprintf
dd
snprintf
d
Ref
- printf format string - Wikipedia
- printf 引數說明 - Edison.X.
- printf - C++ Reference
- 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.5Interesting … 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 0XFFFFFFF1Result.
dec unsigned dec %uunsigned oct %ounsigned hex %X15 15 017 F -15 4294967281 037777777761 0XFFFFFFF1 (+) 4294967296 040000000000 0X100000000
- Usage: sprintf
AAA
- printf, puts, scanf, gets –> gets_s()
BBB