Sample Video Frame
Exercise 6: Memorizing C Syntax
After learning the operators, it's time to memorize the keywords and basic syntax structures you'll be using. Trust me when I tell you that the small amount of time spent memorizing these things will pay huge dividends later as you go through the book.
As I mentioned in Exercise 5, you don't have to stop reading the book while you memorize these things. You can and should do both. Use your flash cards as a warm up before coding that day. Take them out and drill them for 15-30 minutes, then sit down and do some more exercises in the book. As you go through the book, try to use the code you're typing as more of a way to practice what you're memorizing. One trick is to build a pile of flash cards containing operators and keywords that you don't immediately recognize while you're coding. After you're done for the day, practice those flash cards for another 15-30 minutes.
Keep this up and you'll learn C much faster and more solidly than you would if you just stumbled around typing code until you memorized it second hand.
The Keywords
The keywords of a language make up words that augment the symbols so that the language reads well. There are some languages like APL that don't really have keywords. There are other languages like Forth and Lisp that are almost nothing but keywords. In the middle are languages like C, Python, Ruby, and many more that mix sets of keywords with symbols to create the basis of the language.
WARNING!
The technical term for processing the symbols and keywords of a programming language is lexical analysis. The word for one of these symbols or keywords is a lexeme.
|Keywords |
Operator | Description |
---|---|
auto | Give a local variable a local lifetime. |
break | Exit out of a compound statement. |
case | A branch in a switch-statement. |
char | Character data type. |
const | Make a variable unmodifiable. |
continue | Continue to the top of a loop. |
default | Default branch in a switch-statement. |
do | Start a do-while loop. |
double | A double floating point data type. |
else | An else branch of an if-statement. |
enum | Define a set of int constants. |
extern | Declare an identifier is defined externally. |
float | A floating point data type. |
for | Start a for-loop. |
goto | Jump to a label. |
if | Starts an if-statement. |
int | An integer data type. |
long | A long integer data type. |
register | Declare a variable be stored in a CPU register. |
return | Return from a function. |
short | A short integer data type. |
signed | A signed modifier for integer data types. |
sizeof | Determine the size of data. |
static | Preserve variable value after its scope exits. |
struct | Combine variables into a single record. |
switch | Start a switch-statement. |
typedef | Create a new type. |
union | Start a union-statement. |
unsigned | An unsigned modifier for integer data types. |
void | Declare a data type empty. |
volatile | Declare a variable might be modified elsewhere. |
while | Start a while-loop. |
Syntax Structures
I suggest you memorize those, as well as memorizing the syntax structures. A syntax structure is a pattern of symbols that make up a C program code form, such as the form of an if-statement
or a while-loop
. You should find most of these familiar, since you already know one language. The only trouble is then learning how C does it.
Here's how you read these:
Anything in
ALLCAPS
is meant as a replacement spot or hole.Seeing
[ALLCAPS]
means that part is optional.The best way to test your memory of syntax structures is to open a text editor, and where you see
switch-statement
, try to write the code form after saying what it does.
An if-statement
is your basic logic branching control:
if(TEST) {
CODE;
} else if(TEST) {
CODE;
} else {
CODE;
}
A switch-statement
is like an if-statement
but works on simple integer constants:
switch (OPERAND) {
case CONSTANT:
CODE;
break;
default:
CODE;
}
A while-loop
is your most basic loop:
while(TEST) {
CODE;
}
You can also use continue
to cause it to loop. Call this form while-continue-loop
for now:
while(TEST) {
if(OTHER_TEST) {
continue;
}
CODE;
}
You can also use break
to exit a loop. Call this form while-break-loop
:
while(TEST) {
if(OTHER_TEST) {
break;
}
CODE;
}
The do-while-loop
is an inverted version of a while-loop
that runs the code then tests to see if it should run again:
do {
CODE;
} while(TEST);
It can also have continue
and break
inside to control how it operates.
The for-loop
does a controlled counted loop through a (hopefully) fixed number of iterations using a counter:
for(INIT; TEST; POST) {
CODE;
}
An enum
creates a set of integer constants:
enum { CONST1, CONST2, CONST3 } NAME;
A goto
will jumpt to a label, and is only used in a few useful situations like error detection and exiting:
if(ERROR_TEST) {
goto fail;
}
fail:
CODE;
A function
is defined this way:
TYPE NAME(ARG1, ARG2, ..) {
CODE;
return VALUE;
}
That may be hard to remember, so try this example to see what's meant by TYPE
, NAME
, ARG
and VALUE
:
int name(arg1, arg2) {
CODE;
return 0;
}
A typedef
defines a new type:
typedef DEFINITION IDENTIFIER;
A more concrete form of this is:
typedef unsigned char byte;
Don't let the spaces fool you; the DEFINITION
is unsigned char
and the IDENTIFIER
is byte
in that example.
A struct
is a packaging of many base data types into a single concept, which are used heavily in C:
struct NAME {
ELEMENTS;
} [VARIABLE_NAME];
The [VARIABLE_NAME]
is optional, and I prefer not to use it except in a few small cases. This if commonly combined with typedef
like this:
typedef struct [STRUCT_NAME] {
ELEMENTS;
} IDENTIFIER;
Finally, union
creates something like a struct
, but the elements will overlap in memory. This is strange to understand, so simply memorize the form for now:
union NAME {
ELEMENTS;
} [VARIABLE_NAME];
A Word of Encouragement
Once you've created flashcards for each of these, drill them in the usual way by starting with the name side, and then reading the description and form on the other side. In the video for this exercise, I show you how to use Anki to do this efficiently, but you can replicate the experience with simple index cards, too.
I've noticed some fear or discomfort in with students who are asked to memorize something like this. I'm not exactly sure why, but I encourage you to do it anyway. Look at this as an opportunity to improve your memorization and learning skills. The more you do it the better at it you get and the easier it gets.
It's normal to feel discomfort and frustration. Don't take it personally. You might spend 15 minutes and simply hate doing it and feel like a total failure. This is normal, and it doesn't mean you actually are a failure. Perseverance will get you past the initial frustration, and this little exercise will teach you two things:
You can use memorization as a self-evaluation of your competence. Nothing tells you how well you really know a subject like a memory test of its concepts.
The way to conquer difficulty is a little piece at a time. Programming is a great way to learn this because it's so easy to break down into small parts and focus on what's lacking. Take this as an opportunity to build your confidence in tackling large tasks in small pieces.
A Word of Warning
I'll add a final word of warning about memorization. Memorizing a large quantity of facts doesn't automatically make you good at applying those facts. You can memorize the entire ANSI C standards document and still be a terrible programmer. I've encountered many supposed C experts who know every square inch of standard C grammar but still write terrible, buggy, weird code, or don't code at all.
Never confuse an ability to regurgitate memorized facts with ability to actually do something well. To do that you need to apply these facts in different situations until you know how to use them. That's what the rest of this book will help you do.
Register for Learn C the Hard Way
Register today for the course and get the all currently available videos and lessons, plus all future modules for no extra charge.