- 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 11: While-Loop And Boolean Expressions
You've had your first taste of how C does loops, but the boolean expression i < argc
might have not been clear to you. Let me explain something about it before we see how a while-loop
works.
In C, there's not really a "boolean" type, and instead any integer that's 0 is "false" and otherwise it's "true". In the last exercise the expression i < argc
actually resulted in 1 or 0, not an explicit True
or False
like in Python. This is another example of C being closer to how a computer works, because to a computer truth values are just integers.
Now you'll take and implement the same program from the last exercise but use a while-loop
instead. This will let you compare the two so you can see how one is related to another.
#include <stdio.h>
int main(int argc, char *argv[])
{
// go through each string in argv
int i = 0;
while(i < argc) {
printf("arg %d: %s\n", i, argv[i]);
i++;
}
// let's make our own array of strings
char *states[] = {
"California", "Oregon",
"Washington", "Texas"
};
int num_states = 4;
i = 0; // watch for this
while(i < num_states) {
printf("state %d: %s\n", i, states[i]);
i++;
}
return 0;
}
You can see from this that a while-loop
is simpler:
while(TEST) {
CODE;
}
It simply runs the CODE
as long as TEST
is true (1). This means that to replicate how the for-loop
works we need to do our own initializing and incrementing of i
.
What You Should See
The output is basically the same, so I just did it a little different so you can see another way it runs.
$ make ex11
cc -Wall -g ex11.c -o ex11
$ ./ex11
arg 0: ./ex11
state 0: California
state 1: Oregon
state 2: Washington
state 3: Texas
$
$ ./ex11 test it
arg 0: ./ex11
arg 1: test
arg 2: it
state 0: California
state 1: Oregon
state 2: Washington
state 3: Texas
$
How To Break It
In your own code you should favor for-loop
constructs over while-loop
because a for-loop
is harder to break. Here's a few common ways:
- Forget to initialize the first
int i;
so have it loop wrong. - Forget to initialize the second loop's
i
so that it retains the value from the end of the first loop. Now your second loop might or might not run. - Forget to do a
i++
increment at the end of the loop and you get a "forever loop", one of the dreaded problems of the first decade or two of programming.
Extra Credit
- Make these loops count backward by using
i--
to start atargc
and count down to 0. You may have to do some math to make the array indexes work right. - Use a while loop to copy the values from
argv
intostates
. - Make this copy loop never fail such that if there's too many
argv
elements it won't put them all intostates
. - Research if you've really copied these strings. The answer may surprise and confuse you though.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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