Lesson 11

ArtyArty
5 min read

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

SpecifierDescriptionExample Output
%dSigned decimal integer123
%iSigned decimal integer (same as %d)123
%uUnsigned decimal integer123
%oUnsigned octal173
%xUnsigned hexadecimal (lowercase)7b
%XUnsigned hexadecimal (uppercase)7B
%hhdSigned char127
%hhuUnsigned char255
%hdSigned short32767
%huUnsigned short65535
%ldSigned long2147483647
%luUnsigned long4294967295
%lldSigned long long9223372036854775807
%lluUnsigned long long18446744073709551615
%jdSigned intmax_tDepends on system
%juUnsigned uintmax_tDepends on system
%zdSigned size_tDepends on system
%zuUnsigned size_tDepends on system
%tdSigned ptrdiff_tDepends on system
%tuUnsigned ptrdiff_tDepends on system

Printing Floating-Point Numbers

SpecifierDescriptionExample Output
%fDecimal notation (fixed-point)123.456000
%FDecimal notation (uppercase, fixed-point)123.456000
%eScientific notation (lowercase 'e')1.234560e+02
%EScientific notation (uppercase 'E')1.234560E+02
%gShortest representation of %f or %e123.456 or 1.23e+02
%GShortest representation of %F or %E123.456 or 1.23E+02
%aHexadecimal floating-point (lowercase)0x1.edd2f2p+6
%AHexadecimal floating-point (uppercase)0X1.EDD2F2P+6
%Lflong double in decimal notationDepends on system
%Lelong double in scientific notationDepends on system
%Lglong double in shortest representationDepends on system

Other Conversion Specifiers

SpecifierDescriptionExample Output
%cCharacterA
%sString"Hello"
%pPointer address0x7ffee5b7a8c0
%nStores number of characters printed so far(Stores value in an integer variable)
%%Prints a literal %%

Printing With Control String Including Flags

FlagDescriptionExample (%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)
0Zero-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 SequenceDescriptionExample UsageOutput
\nNewlineprintf("Hello\n");"Hello" (new line after)
\tTab (horizontal)printf("A\tB\n");"A B"
\rCarriage returnprintf("Hello\rHi");"Hi" (overwrites "Hello")
\bBackspaceprintf("A\bB\n");"B" (A is erased)
\fForm feed (page break)printf("A\fB\n");"A" "(page break) B"
\vVertical tabprintf("A\vB\n");"A" (vertical spacing) "B"
\\Backslashprintf("\\n");"\n" (prints as text)
\'Single quoteprintf("\'");'
\"Double quoteprintf("\"");"
\?Question mark (avoid trigraphs)printf("\?");?
\0Null character (string terminator)printf("A\0B\n");"A" (stops at \0)
\xhhHexadecimal characterprintf("\x41");"A" (ASCII 65)
\oooOctal characterprintf("\101");"A" (ASCII 65)

Reading Formatted Input

SpecifierDescriptionExample InputExample Code
%dReads a signed integer42scanf("%d", &num);
%iReads an integer (auto base detect: decimal, octal, hex)0x2Ascanf("%i", &num);
%uReads an unsigned integer100scanf("%u", &unum);
%oReads an octal integer052scanf("%o", &octal);
%xReads a hexadecimal integer0x2Ascanf("%x", &hex);
%fReads a floating-point number3.14scanf("%f", &flt);
%lfReads a double3.14159scanf("%lf", &dbl);
%LfReads a long double3.1415926535scanf("%Lf", &ldbl);
%cReads a single characterAscanf(" %c", &ch);
%sReads a string (until whitespace)Helloscanf("%s", str);
%pReads a memory address (pointer)0x123abcscanf("%p", &ptr);
%nStores number of characters read(N/A)scanf("%d%n", &num, &count);
0
Subscribe to my newsletter

Read articles from Arty directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Arty
Arty