From cdbbfabeecd87881e8f7fda99806e301c9c7ec5e Mon Sep 17 00:00:00 2001 From: Kevin Duignan Date: Wed, 20 Sep 2023 22:56:34 +1000 Subject: [PATCH] Update printing.md Add sample code snippet that follows "If you want to have a play with printf()..." that was previously omitted. --- src/chapter2/printing.md | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/chapter2/printing.md b/src/chapter2/printing.md index 196c36b..004b5ff 100644 --- a/src/chapter2/printing.md +++ b/src/chapter2/printing.md @@ -42,8 +42,21 @@ int main() > Question: Notice how we used `double` for the type of `sum`. What would happen if `sum` type was `int`? -If you want to have a play with `printf()`, copy the following code snippet run it on your own device. The command will be identically to 'Hello World!'. +If you want to have a play with `printf()`, copy the following code snippet run it on your own device. The command line will output different varieties of 'Hello World!'. +```c +#include + +int main() { + printf("%30s\n", "Hello World!"); // Padding added + printf("%40s%10s%20s%15s\n", "Hell", "o", "World", "!"); + printf("%10.7s\n", "Hello World!"); // Print only the first 7 characters with padding + printf("%100c%c%c%c%c %c%c%c%c%c%c%c\n", + 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33, '\n'); // Hex values + return 0; +} + +``` ### Formatting Specification You'll notice we used a different character after the `%` for each argument. This is because `printf()` needs to know the type of the incoming arguments so that it can format the string appropriately. For example floating point types have to use a decimal point when transformed into a text format while integers do not.