- 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 5: The Structure Of A C Program
You know how to use printf
and have a couple basic tools at your disposal, so let's break down a simple C program line-by-line so you know how one is structured. In this program you're going to type in a few more things that you're unfamiliar with, and I'm going to lightly break them down. Then in the next few exercises we're going to work with these concepts.
#include <stdio.h>
/* This is a comment. */
int main(int argc, char *argv[])
{
int distance = 100;
// this is also a comment
printf("You are %d miles away.\n", distance);
return 0;
}
Type this code in, make it run, and make sure you get no Valgrind errors . You probably won't but get in the habit of checking it.
What You Should See
This has pretty boring output, but the point of this exercise is to analyze the code:
$ make ex5
cc -Wall -g ex5.c -o ex5
$ ./ex5
You are 100 miles away.
$
Breaking It Down
There's a few features of the C language in this code that you might have only slightly figured out while you were typing code. Let's break this down line-by-line quickly, and then we can do exercises to understand each part better:
ex5.c:1
An include
and it is the way to import the contents of one file into this source file. C has a convention of using .h
extensions for "header" files, which then contain lists of functions you want to use in your program.
ex5.c:3
This is a multi-line comment
and you could put as many lines of text between the /*
and closing */
characters as you want.
ex5.c:4
A more complex version of the main function
you've been using blindly so far. How C programs work is the operating system loads your program, and then runs the function named main
. For the function to be totally complete it needs to return an int
and take two parameters, an int
for the argument count, and an array of char *
strings for the arguments. Did that just fly over your head? Do not worry, we'll cover this soon.
ex5.c:5
To start the body of any function you write a {
character that indicates the beginning of a "block". In Python you just did a :
and indented. In other languages you might have a begin
or do
word to start.
ex5.c:6
A variable declaration and assignment at the same time. This is how you create a variable, with the syntax type name = value;
. In C statements (except for logic) end in a ';'
(semicolon) character.
ex5.c:8
Another kind of comment, and it works like Python or Ruby comments where it starts at the //
and goes until the end of the line.
ex5.c:9
A call to your old friend printf
. Like in many languages function calls work with the syntax name(arg1, arg2);
and can have no arguments, or any number. The printf
function is actually kind of weird and can take multiple arguments. We'll see that later.
ex5.c:11
A return from the main function, which gives the OS your exit value. You may not be familiar with how Unix software uses return codes, so we'll cover that as well.
ex5.c:12
Finally, we end the main function with a closing brace }
character and that's the end of the program.
There's a lot of information in this break-down, so study it line-by-line and make sure you at least have a little grasp of what's going on. You won't know everything, but you can probably guess before we continue.
Extra Credit
- For each line, write out the symbols you don't understand and see if you can guess what they mean. Write a little chart on paper with your guess that you can use to check later and see if you get it right.
- Go back to the source code from the previous exercises and do a similar break-down to see if you're getting it. Write down what you don't know and can't explain to yourself.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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