Lesson 11

Streams
scanf, gets and getchar read from the standard input stream.
printf, puts and putchar write to the standard output stream.
The standard input stream normally is connected to the keyboard.
The standard output stream normally is connected to the screen.
There is a third standard stream also connected to the screen for errors, it’s called the standard error stream.
Inputs and outputs are processed with streams which are sequence of bytes.
In input operations, the bytes flow from a device: Keyboard, Disk Drive, network connection, etc.
In output operations, bytes flow from main memory to a device: display screen, a printer, a network connection, etc.
Printing Integers
Specifier | Description | Example Output |
%d | Signed decimal integer | 123 |
%i | Signed decimal integer (same as %d ) | 123 |
%u | Unsigned decimal integer | 123 |
%o | Unsigned octal | 173 |
%x | Unsigned hexadecimal (lowercase) | 7b |
%X | Unsigned hexadecimal (uppercase) | 7B |
%hhd | Signed char | 127 |
%hhu | Unsigned char | 255 |
%hd | Signed short | 32767 |
%hu | Unsigned short | 65535 |
%ld | Signed long | 2147483647 |
%lu | Unsigned long | 4294967295 |
%lld | Signed long long | 9223372036854775807 |
%llu | Unsigned long long | 18446744073709551615 |
%jd | Signed intmax_t | Depends on system |
%ju | Unsigned uintmax_t | Depends on system |
%zd | Signed size_t | Depends on system |
%zu | Unsigned size_t | Depends on system |
%td | Signed ptrdiff_t | Depends on system |
%tu | Unsigned ptrdiff_t | Depends on system |
Printing Floating-Point Numbers
Specifier | Description | Example Output |
%f | Decimal notation (fixed-point) | 123.456000 |
%F | Decimal notation (uppercase, fixed-point) | 123.456000 |
%e | Scientific notation (lowercase 'e') | 1.234560e+02 |
%E | Scientific notation (uppercase 'E') | 1.234560E+02 |
%g | Shortest representation of %f or %e | 123.456 or 1.23e+02 |
%G | Shortest representation of %F or %E | 123.456 or 1.23E+02 |
%a | Hexadecimal floating-point (lowercase) | 0x1.edd2f2p+6 |
%A | Hexadecimal floating-point (uppercase) | 0X1.EDD2F2P+6 |
%Lf | long double in decimal notation | Depends on system |
%Le | long double in scientific notation | Depends on system |
%Lg | long double in shortest representation | Depends on system |
Other Conversion Specifiers
Specifier | Description | Example Output |
%c | Character | A |
%s | String | "Hello" |
%p | Pointer address | 0x7ffee5b7a8c0 |
%n | Stores number of characters printed so far | (Stores value in an integer variable) |
%% | Prints a literal % | % |
Printing With Control String Including Flags
Flag | Description | Example (%8d , %.2f , etc.) | Output (if value = 42 or value = 3.14159 ) |
- | Left-align the output within the given width | %-8d | "42 " (padded with spaces) |
+ | Always show sign (+ or - ) for numbers | %+d | +42 |
Add a space before positive numbers | % d | " 42" (instead of just 42 ) | |
0 | Zero-padding instead of spaces | %08d | "00000042" |
# | Alternate form (prefix for hex, octal, etc.) | %#x , %#o , %#f | "0x2a" , "052" , "3.141590" |
. | Precision specifier (decimal places for floats) | %.2f | "3.14" |
* | Width or precision from an argument | %*d (printf("%*d", 8, 42); ) | " 42" |
Printing Literals and Escape Sequences
Escape Sequence | Description | Example Usage | Output |
\n | Newline | printf("Hello\n"); | "Hello" (new line after) |
\t | Tab (horizontal) | printf("A\tB\n"); | "A B" |
\r | Carriage return | printf("Hello\rHi"); | "Hi" (overwrites "Hello") |
\b | Backspace | printf("A\bB\n"); | "B" (A is erased) |
\f | Form feed (page break) | printf("A\fB\n"); | "A" "(page break) B" |
\v | Vertical tab | printf("A\vB\n"); | "A" (vertical spacing) "B" |
\\ | Backslash | printf("\\n"); | "\n" (prints as text) |
\' | Single quote | printf("\'"); | ' |
\" | Double quote | printf("\""); | " |
\? | Question mark (avoid trigraphs) | printf("\?"); | ? |
\0 | Null character (string terminator) | printf("A\0B\n"); | "A" (stops at \0 ) |
\xhh | Hexadecimal character | printf("\x41"); | "A" (ASCII 65) |
\ooo | Octal character | printf("\101"); | "A" (ASCII 65) |
Reading Formatted Input
Specifier | Description | Example Input | Example Code |
%d | Reads a signed integer | 42 | scanf("%d", &num); |
%i | Reads an integer (auto base detect: decimal, octal, hex) | 0x2A | scanf("%i", &num); |
%u | Reads an unsigned integer | 100 | scanf("%u", &unum); |
%o | Reads an octal integer | 052 | scanf("%o", &octal); |
%x | Reads a hexadecimal integer | 0x2A | scanf("%x", &hex); |
%f | Reads a floating-point number | 3.14 | scanf("%f", &flt); |
%lf | Reads a double | 3.14159 | scanf("%lf", &dbl); |
%Lf | Reads a long double | 3.1415926535 | scanf("%Lf", &ldbl); |
%c | Reads a single character | A | scanf(" %c", &ch); |
%s | Reads a string (until whitespace) | Hello | scanf("%s", str); |
%p | Reads a memory address (pointer) | 0x123abc | scanf("%p", &ptr); |
%n | Stores number of characters read | (N/A) | scanf("%d%n", &num, &count); |
Subscribe to my newsletter
Read articles from Arty directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
