Video Coming Soon...
03: Formatted Printing
Many programming languages use the C way of formatting output, so let's try it. Create a file named ex3.c with this code in it:
#include <stdio.h>
int main()
{
int age = 10;
int height = 74;
printf("I am %d years old.\n", age);
printf("I am %d inches tall.\n", height);
return 0;
}
Once you've finished that, do the usual make ex3 to build and run it. If you don't remember how to do this go back to Exercise 2 where you did your first little C program and created a Makefile.
Make sure you fix all warnings and errors that come up. If you have way too many to fix, then code a little bit at a time and run the compiler frequently to catch the errors early. If you don't know what an error is then search for it online, or feel free to ask your favorite AI chat bot to explain it.
WARNING: If you're using AI chatbots to replace Google search then that's totally valid. Also asking it to explain code is perfectly fine as well. You should never copy-paste AI code output or have an AI chatbot solve something for you. You're only cheating yourself out of learning when you do this. You can of course get help from a chatbot you like just be sure to manually type in everything it gives you and research what it tells you.
Breaking It Down
This exercise has a whole lot going on in a small amount of code, so let's break it down:
- First we're including another header file called
stdio.h. This tells the compiler that you're going to use the standard Input/Output functions. One of those isprintf. - Then we're using a variable named
ageand setting it to 10. - Next we're using a variable
heightand setting it to 72. - Then we're adding the
printffunction to print the age and height of the tallest 10-year-old on the planet. - In
printf, you'll notice we're including a format string, as seen in many other languages. - After this format string, we're putting in the variables that should be "replaced" into the format string by
printf.
The result is giving printf some variables and it's constructing a new string and then printing it to the terminal.
What You Should See
When you do the whole build, you should see something like this:
View Source file ex3.sh-session Only
$ make ex3
cc -Wall -g ex3.c -o ex3
$ ./ex3
I am 10 years old.
I am 72 inches tall.
$
Pretty soon I'm going to stop telling you to run make and what the build looks like, so please make sure you're getting this right and that it's working.
External Research
In the Imposture Check section of each exercise, I might have you find information on and figure things out on your own. This is an important part of being a self-sufficient programmer. If you're constantly running to ask someone a question before trying to figure things out yourself, then you'll never learn how to solve problems independently. You'll never build confidence in your skills and will always need someone else around to do your work.
The way to break this habit is to force yourself to try to answer your own question first, and then confirm that your answer is right. You do this by trying to break things, experimenting with your answer, and doing your own research.
For this exercise, I want you to go online and find all of the printf escape codes and format sequences. Escape codes are \n or \t that let you print a newline or tab, respectively. Format sequences are the %s or %d that let you print a string or integer. Find them all, learn how to modify them, and see what kind of "precisions" and widths you can do.
From now on, these kinds of tasks will be in the Imposture Check.
How to Break It
Try a few of these ways to break this program, which may or may not cause it to crash on your computer:
- Take the
agevariable out of the firstprintfcall, then recompile. You should get a couple of warnings. - Run this new program and it will either crash or print out a really crazy age.
- Put the
printfback the way it was, and then don't setageto an initial value by changing that line toint age;, and then rebuild it and run it again.
View Source file ex3.bad.sh-session Only
# edit ex3.c to break printf
$ make ex3
cc -Wall -g ex3.c -o ex3
ex3.c: In function 'main':
ex3.c:8: warning: too few arguments for format
ex3.c:5: warning: unused variable 'age'
$ ./ex3
I am -919092456 years old.
I am 72 inches tall.
# edit ex3.c again to fix printf, but don't init age
$ make ex3
cc -Wall -g ex3.c -o ex3
ex3.c: In function 'main':
ex3.c:8: warning: 'age' is used uninitialized in this function
$ ./ex3
I am 0 years old.
I am 72 inches tall.
$
Imposture Check
- Find as many other ways to break
ex3.cas you can. - Run
man 3 printfand read about the other%format characters you can use. These should look familiar if you used them in other languages (they come fromprintf). - Do you know where to find the formatting options for
printf()? - Can you figure out how to automate building this with the
Makefile? Remember that you're trying to learn to rely on yourself to do things, but that doesn't mean you can't research while you figure things out.
Register for From Impostor to Programmer
Register today for the course and get the all currently available videos and lessons, plus all future modules for no extra charge.