Sample Video Frame

Created by Zed A. Shaw Updated 2024-02-17 04:54:36

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 |

OperatorDescription
autoGive a local variable a local lifetime.
breakExit out of a compound statement.
caseA branch in a switch-statement.
charCharacter data type.
constMake a variable unmodifiable.
continueContinue to the top of a loop.
defaultDefault branch in a switch-statement.
doStart a do-while loop.
doubleA double floating point data type.
elseAn else branch of an if-statement.
enumDefine a set of int constants.
externDeclare an identifier is defined externally.
floatA floating point data type.
forStart a for-loop.
gotoJump to a label.
ifStarts an if-statement.
intAn integer data type.
longA long integer data type.
registerDeclare a variable be stored in a CPU register.
returnReturn from a function.
shortA short integer data type.
signedA signed modifier for integer data types.
sizeofDetermine the size of data.
staticPreserve variable value after its scope exits.
structCombine variables into a single record.
switchStart a switch-statement.
typedefCreate a new type.
unionStart a union-statement.
unsignedAn unsigned modifier for integer data types.
voidDeclare a data type empty.
volatileDeclare a variable might be modified elsewhere.
whileStart 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:

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:

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.

Previous Lesson Next Lesson

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.