Console output

The function printf

The header of the C Standard Library declaring the functions to input/output to the console/files is <stdio.h> (STandarD Input Output). To output formatted text in the console, the C Standard Library provides the function printf. Its header is int printf(const char* format). The argument is a constant pointer to a null-terminated string. If the operation is successful, the number of character printed is returned. To print a newline character, we must use the backslash character followed by n (\n).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdio.h> int main() { printf("Hello.\n"); // "Hello.\n" is written in the console. const char* str = "I am a null-terminated string.\n"; printf(str); /* The string "I am a null-terminated string.\n" is outputted into the console. */ return 0; }

Outputting the value of variables

To output the content of a variable using printf, we must put, inside the string given as first argument, the percentage symbol %, followed by a letter (Called specifier) representing the type of the variable to output, where we want it to be displayed. We then give the variable or value to output, in argument to printf. If there are many variables to output, we give them in argument to printf, in the order they must be displayed. The qualifiers used to represent the type int are d and i.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h> int main() { int v1 = 767, v2 = 26; printf("The number is %d.\n", v1); /* The string "The number is 767.\n" is printed into the console. */ printf("The numbers are %i and %d.\n", v1, v2); /* The string "The numbers are 767 and 26.\n" is written in the console. */ return 0; }

Note that consoles can only input/output text, so the value of the variables are converted to strings before they are written to them. We could, without problems, manually convert our numbers to strings (By using functions coded by us or functions from libraries) and print those strings instead of using specifiers to do it.

It is also possible to output a string inside the string given as first argument to printf. The letter (specifier) used to output a string is s.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h> int main() { const char* name = "Laura"; printf("My name is %s.\n", name); /* The string "My name is Laura.\n" is written into the console. */ return 0; }

If we want to output the percentage character % in the string as first argument of printf, similarly to the backslash character, we must write it 2 times (%%), so the function knows it is not to include the value of a variable.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h> int main() { printf("I had %d%% in my last test.\n", 100); /* The string "I had 100% in my last test.\n" is printed into the console. */ return 0; }

Here is a table with the specifiers:

CharacterTypeExample
d or iDecimal integer50, -50
uUnsigned decimal integer300
xUnsigned integer (lowercase) (Displayed in hexadecimal)5a4c
XUnsigned integer (Displayed in hexadecimal, with uppercase letters)5A4C
oUnsigned integer (Displayed in octal)742
cChard
sStringHello
pPointer address (In hexadecimal)00f61598
fDecimal floating point number12.34
eScientific notation (mantissa(e)exponent). (lowercase e)1.2345e+4
EScientific notation (mantissa(E)exponent). (uppercase E)1.2345E+4
gShortest representation between %e or %f12.34
GShortest representation between %E or %f12.34
aScientific notation (mantissa(e)exponent). (lowercase e)(Displayed in hexadecimal)a.bc1e+3
AScientific notation (mantissa(E)exponent). (uppercase E)(Displayed in hexadecimal)A.BC1E+3
nPrints nothing. Assign the int variable pointed in argument with the number of characters printed so far.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <stdio.h> int main() { double d = 123.4567; printf("Doubles: %f, %f.\n", d, 98.32); /* "Doubles: 123.4567, 98.32.\n" is printed. */ int *ptr = 0; printf("Address: %p.\n", ptr); /* "Address: 00000000.\n" is printed. */ printf("Character: %c.\n", 'a'); /* "Character: a.\n" is printed. */ printf("Hexadecimal of 5000: %x.\n", 5000); /* "Hexadecimal of 5000: 1388.\n" is printed. */ printf("Scientific notation: %e.\n", 12345.6789); /* "Scientific notation: 1.234568e+004.\n" is printed. */ return 0; }

Output formatting

Additional, optional, specifiers can be added to specify the formatting in which the values will be displayed. They are called sub-specifiers. They are added following that template:

%[flags][width][.precision][size]specifier

The flags are:

FlagDescription
-The characters representing the value are written to the left if they are smaller then the minimum number of characters to print (See the sub-specifier width).
+The number sign is displayed even if the number is positive.
(space)If no sign is displayed, a space is written instead.
#With the specifiers 0, x or X, numbers displayed, except 0, are prefixed with, in order, 0, 0x or 0X.
0Zeroes are added at the beginning if the number of character representing the value is smaller then the minimum amount of character to print (See the sub-specifier width).

We can put a number (The width sub-specifier) after the optional flag sub-specifier to specify the minimum amount of character printed to represent the value of the variable. If we put an asterisk * instead, the value of the width sub-specifier must be given in argument to printf, right before the variable which value must be printed.

We can define the precision sub-specifier by adding a point followed by a number representing the precision, after the optional width sub-specifier. With (signed and unsigned) integers, it defines the minimum amount of digits to print. Zeros are added at the left if the string representing the number is smaller. Note that if the precision is 0, the result is that nothing will be displayed for the value zero. With floating-point number specifiers (Except for g and G), it defines the number of digits to display after the decimal point (The default is 6). With the specifiers g and G, it specifies the maximum amount of significant digits to write. With the specifier s, it sets the maximum amount of characters to print (The default is the whole string). If we put an asterisk * after the point, we must specify the precision as an argument, of type int, to printf, right before the variable to print.

The optional size sub-specifier sets the variable type expected as argument to printf.

Lenghtd, iu, x, X, of, e, E, g, G, a, Acspn
(Default)intunsigneddoubleintchar*void*int*
hhcharunsigned charchar*
hshortunsigned shortshort*
llongunsigned longwint_twint_twchar_t*long*
lllong longunsigned long longlong long*
jintmax_tuintmax_tintmax_t*
zsize_tsize_tsize_t*
tptrdiff_tptrdiff_tptrdiff_t*
Ldouble long

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <stdio.h> int main() { printf("%+d\n", 100); /* The string "+100\n" is printed in the console. */ printf("%#x\n", 560); /* The string "0x230\n" is printed in the console. */ printf("%6d\n", 333); /* The string " 333\n" is printed in the console. */ printf("%-6d\n", 333); /* The string "333 \n" is printed in the console. */ printf("%07d\n", 333); /* The string "0000333\n" is printed in the console. */ printf("%1d\n", 555); /* The string "555\n" is printed in the console. */ printf("%.5d\n", 333); /* The string "00333\n" is printed in the console. */ printf("%.2f\n", 12.34567); /* The string "12.35\n" is printed in the console. */ printf("%.4f\n", 12.34567); /* The string "12.3457\n" is printed in the console. */ // It takes a char as an argument, so the range of value it can print is [-128, 127]. printf("%hhd\n", 500); /* The string "-12\n" is printed in the console. */ printf("%ld\n", 500L); /* The string "500\n" is printed in the console. */ return 0; }

The function puts

The function which header is int puts(const char* str) is used to print a string, followed by the newline character, to the console. It takes the null-terminated string to print in argument. It returns a positive value if the operation is successful.

1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h> int main() { puts("I am a sentence."); // The line above produces the same result as: printf("I am a sentence.\n"); return 0; }

Outputting a single character

The function putchar, which header is int putchar(int char), is used to print a single character to the console. It takes the ASCII value of the character (as an int) to print, as argument and returns the character (as an int) if the operation is successful.

1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h> int main() { putchar('A'); /* The character 'A' is printed into the console. */ return 0; }