- 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 7: More Variables, Some Math
Let's get familiar with more things you can do with variables by declaring various ints
, floats
, chars
, and doubles
. We'll then use these in various math expressions so you get introduced to C's basic math.
#include <stdio.h>
int main(int argc, char *argv[])
{
int bugs = 100;
double bug_rate = 1.2;
printf("You have %d bugs at the imaginary rate of %f.\n",
bugs, bug_rate);
long universe_of_defects = 1L * 1024L * 1024L * 1024L;
printf("The entire universe has %ld bugs.\n",
universe_of_defects);
double expected_bugs = bugs * bug_rate;
printf("You are expected to have %f bugs.\n",
expected_bugs);
double part_of_universe = expected_bugs / universe_of_defects;
printf("That is only a %e portion of the universe.\n",
part_of_universe);
// this makes no sense, just a demo of something weird
char nul_byte = '\0';
int care_percentage = bugs * nul_byte;
printf("Which means you should care %d%%.\n",
care_percentage);
return 0;
}
Here's what's going on in this little bit of nonsense:
ex7.c:1-4
The usual start of a C program.
ex7.c:5-6
Declare an int
and double
for some fake bug data.
ex7.c:8-9
Print out those two, so nothing new here.
ex7.c:11
Declare a huge number using a new type long
for storing big numbers.
ex7.c:12-13
Print out that number using %ld
which adds a modifier to the usual %d
. Adding 'l' (the letter ell) means "print this as a long decimal".
ex7.c:15-17
Just more math and printing.
ex7.c:19-21
Craft up a depiction of your bug rate compared to the bugs in the universe, which is a completely inaccurate calculation. It's so small though that we have to use %e
to print it in scientific notation.
ex7.c:24
Make a character, with a special syntax '\0'
which creates a 'nul byte' character. This is effectively the number 0.
ex7.c:25
Multiply bugs by this character, which produces 0 for how much you should care. This demonstrates an ugly hack you find sometimes.
ex7.c:26-27
Print that out, and notice I've got a %%
(two percent chars) so I can print a '%' (percent) character.
ex7.c:28-30
The end of the main
function.
This bit of source is entirely just an exercise, and demonstrates how some math works. At the end, it also demonstrates something you see in C, but not in many other languages. To C, a "character" is just an integer. It's a really small integer, but that's all it is. This means you can do math on them, and a lot of software does just that, for good or bad.
This last bit is your first glance at how C gives you direct access to the machine. We'll be exploring that more in later exercises.
What You Should See
As usual, here's what you should see for the output:
$ make ex7
cc -Wall -g ex7.c -o ex7
$ ./ex7
You have 100 bugs at the imaginary rate of 1.200000.
The entire universe has 1073741824 bugs.
You are expected to have 120.000000 bugs.
That is only a 1.117587e-07 portion of the universe.
Which means you should care 0%.
$
How To Break It
Again, go through this and try breaking the printf
by passing in the wrong arguments. See what happens when you try to print out that nul_byte
variable too with %s
vs. %c
. When you break it, run it under Valgrind
to see what it says about your breaking attempts.
Extra Credit
- Make the number you assign to
universe_of_defects
various sizes until you get a warning from the compiler. - What do these really huge numbers actually print out?
- Change
long
tounsigned long
and try to find the number that makes that one too big. - Go search online to find out what
unsigned
does. - Try to explain to yourself (before I do in the next exercise) why you can multiply a
char
and anint
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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