We are going to take a little detour for a minute to help you understand what is going on in the code in the lessons. You might have noticed
printf() at the bottom of some exercises. This allows for text output to the terminal.
The basic format is
printf("string to display", [list of optional parameters]).
So if you had something like
printf("Hello World!"), the terminal would display
The optional parameters let you add values dynamically into the string, such as values stored in the variables we have been learning about. For example, if we wanted to output
Hello World, today is the 3rd!, you could do that like this:
int day = 3; printf("Hello World, today is the %drd!", day);
When the compiler runs the code it will replace %d with the value in the variable list, taking them in the order found in the string matching the order they are listed; first in the string is matched with first in the list of parameters).
There are lots of optional formatting and parameter types that can be used, but for our purposes here are a few of the basic ones.
To indicate a variable type you want to replace:
|%d or %i||int|
|%f||double or float|
You can also use some reserved symbols within the string to have invoked formatting, here are a few common ones.
Add newline command at the end of the first
printf() so that the output is cleaner.
printf() command in the code to add the variable to the output so that the output makes sense.