- Learn C The Hard Way
- Preface
- Introduction: The Cartesian Dream Of C
- Exercise 0: The Setup
- Exercise 1: Dust Off That Compiler
- Exercise 2: Make Is Your Python Now
- Exercise 3: Formatted Printing
- Exercise 4: Introducing Valgrind
- Exercise 5: The Structure Of A C Program
- Exercise 6: Types Of Variables
- Exercise 7: More Variables, Some Math
- Exercise 8: Sizes And Arrays
- Exercise 9: Arrays And Strings
- Exercise 10: Arrays Of Strings, Looping
- Exercise 11: While-Loop And Boolean Expressions
- Exercise 12: If, Else-If, Else
- Exercise 13: Switch Statement
- Exercise 14: Writing And Using Functions
- Exercise 15: Pointers Dreaded Pointers
- Exercise 16: Structs And Pointers To Them
- Exercise 17: Heap And Stack Memory Allocation
- Exercise 18: Pointers To Functions
- Exercise 19: A Simple Object System
- Exercise 20: Zed's Awesome Debug Macros
- Exercise 21: Advanced Data Types And Flow Control
- Exercise 22: The Stack, Scope, And Globals
- Exercise 23: Meet Duff's Device
- Exercise 24: Input, Output, Files
- Exercise 25: Variable Argument Functions
- Exercise 26: Write A First Real Program
- Exercise 27: Creative And Defensive Programming
- Exercise 28: Intermediate Makefiles
- Exercise 29: Libraries And Linking
- Exercise 30: Automated Testing
- Exercise 31: Debugging Code
- Exercise 32: Double Linked Lists
- Exercise 33: Linked List Algorithms
- Exercise 34: Dynamic Array
- Exercise 35: Sorting And Searching
- Exercise 36: Safer Strings
- Exercise 37: Hashmaps
- Exercise 38: Hashmap Algorithms
- Exercise 39: String Algorithms
- Exercise 40: Binary Search Trees
- Exercise 41: Using Cachegrind And Callgrind For Performance Tuning
- Exercise 42: Stacks and Queues
- Exercise 43: A Simple Statistics Engine
- Exercise 44: Ring Buffer
- Exercise 45: A Simple TCP/IP Client
- Exercise 46: Ternary Search Tree
- Exercise 47: A Fast URL Router
- Exercise 48: A Tiny Virtual Machine Part 1
- Exercise 48: A Tiny Virtual Machine Part 2
- Exercise 50: A Tiny Virtual Machine Part 3
- Exercise 51: A Tiny Virtual Machine Part 4
- Exercise 52: A Tiny Virtual Machine Part 5
- Next Steps
- Deconstructing K & RC Is Dead
Exercise 3: Formatted Printing
Keep that Makefile
around since it'll help you spot errors and we'll be adding to it when we need to automate more things.
Many programming languages use the C way of formatting output, so let's try it:
#include <stdio.h>
int main()
{
int age = 10;
int height = 72;
printf("I am %d years old.\n", age);
printf("I am %d inches tall.\n", height);
return 0;
}
Once you have that, do the usual make ex3
to build it and run it. Make sure you fix all warnings .
This exercise has a whole lot going on in a small amount of code so let's break it down:
- First you'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 you're using a variable named
age
and setting it to 10. - Next you're using a variable
height
and setting it to 72. - Then you use the
printf
function to print the age and height of the tallest 10 year old on the planet. - In the
printf
you'll notice you're passing in a string, and it's a format string like in many other languages. - After this format string, you put the variables that should be "replaced" into the format string by
printf
.
The result of doing this is you are handing printf
some variables and it is constructing a new string then printing that new string to the terminal.
What You Should See
When you do the whole build you should see something like this:
$ 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 Extra Credit section of each exercise I may have you go find information on your own and figure things out. This is an important part of being a self-sufficient programmer. If you constantly run to ask someone a question before trying to figure it out first then you never learn to solve problems independently. This leads to you never building confidence in your skills and always needing someone else around to do your work.
The way you break this habit is to force yourself to try to answer your own questions first, and to confirm that your answer is right. You do this by trying to break things, experimenting with your possible answer, and doing your own research.
For this exercise I want you to go online and find out 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 a integer. Find all of the ones available, how you can modify them, and what kind of "precisions" and widths you can do.
From now on, these kinds of tasks will be in the Extra Credit and you should do them.
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
age
variable out of the firstprintf
call 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
printf
back the way it was, and then don't setage
to an initial value by changing that line toint age;
then rebuild and run again.
## 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.
$
Extra Credit
- Find as many other ways to break
ex3.c
as you can. - Run
man 3 printf
and read about the other '%' format characters you can use. These should look familiar if you used them in other languages (printf
is where they come from). - Add
ex3
to yourMakefile
'sall
list. Use this tomake clean all
and build all your exercises so far. - Add
ex3
to yourMakefile
'sclean
list as well. Now usemake clean
will remove it when you need to.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论