Video Coming Soon...
WARNING: This exercise needs a full review to match recent changes to C. If you find problems please report them with the bug feature (top right, bug icon).
05: C Operator Reference
This is less of an exercise you do and more a reference that gives you the names and purpose of the symbols and words you encounter in C code. There's two ways to use this, and both are valid at the same time:
- Read through this to get an idea of what's here, then continue with the course. When you see something you don't recognize come back here to find it. Knowing the names of things you use will help you remember them.
- Spend some time now memorizing these symbols so you're primed to know them later. Doing this can make it easier for you later when you code.
If you're the kind of impostor who doesn't actually know the names of things, then I suggest you take time and memorize them. This is especially true if you're like me and hate memorizing.
If you're the kind of impostor who will obsessively memorize everything and never actually do anything with the information then do not memorize these. Browse them, get the gist, and move on diving in and looking these up as you need.
How to Memorize
The best way to memorize something is a fairly simple process:
- Create a set of flashcards that have a symbol on one side and the description on the other. You could also use a program called Anki to do this on your computer. I prefer creating my own because it helps me memorize them as I make them.
- Randomize the flashcards and start going through them on one side. Try your best to remember the other side of the card without looking.
- If you can't recall the other side of the card, then look, repeat the answer to yourself, and then put that card into a separate pile.
- Once you go through all the cards you'll have two piles: one pile of cards you recalled quickly, and another you failed to recall. Pick up the fail pile and drill yourself on only those cards.
- At the very end of the session, which is usually 15-30 minutes, you'll have a set of cards you just can't recall. Take those cards with you wherever you go, when you have have free time, practice memorizing them.
There are many other tricks to memorizing things, but I've found that this is the best way to build instant recall on things you need to be able to use immediately. The symbols, keywords, and syntax of C are things you need instant recall on, so this method is the best for this task.
Also remember that you need to do both sides of the cards. You should be able to read the description and know what symbol matches it, as well as the description for a symbol.
Finally, you don't have to stop while you're memorizing these operators. The best approach is to combine this with exercises in this book so you can apply what you've memorized. See the next exercise for more on this.
The List of Operators
The first operators are the arithmetic operators, which are very similar to almost every other programming language. When you write the cards, the description side should say that it's an arithmetic operator, and what it does.
| Arithmetic Operator | Description |
|---|---|
| + | Add |
| - | Subtract |
| * | Multiply |
| / | Divide |
| % | Modulus |
| ++ | Increment |
| -- | Decrement |
Relational operators test values for equality, and again they are very common in programming languages.
| Relational Operator | Description |
|---|---|
| == | Equal |
| != | Not equal |
| > | Greater than |
| < | Less than |
| >= | Greater than equal |
| <= | Less than equal |
Logical operators perform logic tests, and you should already know what these do. The only odd one is the logical ternary, which you'll learn later in the book.
| Logical Operator | Description |
|---|---|
| && | Logical and |
| || | Logical or |
| ! | Logical not |
| ? : | Logical ternary |
Bitwise operators do something you likely won't experience often in modern code. They alter the bits that make up bytes and other data types in various ways. I won't cover this in my book, but they are very handy when working with certain types of lower level systems.
| Bitwise Operator | Description |
|---|---|
| & | Bitwise and |
| | | Bitwise or |
| ^ | Bitwise xor |
| ~ | Bitwise ones compliment |
| << | Bitwise shift left |
| >> | Bitwise shift right |
Assignment operators simply assign expressions to variables, but C combines a large number of other operators with assignment. So when I say and-equal, I mean the bitwise operators, not the logical operators.
| Assignment Operator | Description |
|---|---|
| = | Assign equal |
| += | Assign plus-equal |
| -= | Assign minus-equal |
| *= | Assign multiply-equal |
| /= | Assign divide-equal |
| %= | Assign modulus-equal |
| <<= | Assign shift-left-equal |
| >>= | Assign shift-right-equal |
| &= | Assign and-equal |
| ^= | Assign xor-equal |
| |= | Assign or-equal |
I'm calling these data operators but they really deal with aspects of pointers, member access, and various elements of data structures in C.
| Data Operator | Description |
|---|---|
| sizeof() | Get the size of |
| [] | Array subscript |
| & | The address of |
| * | The value of |
| -> | Structure dereference |
| . | Structure reference |
Finally, there are a few miscellaneous symbols that are either frequently used for different roles (like ,), or don't fit into any of the previous categories for various reasons.
| Misc. Operator | Description |
|---|---|
| , | Comma |
| ( ) | Parenthesis |
| { } | Braces |
| : | Colon |
| // | Single-line comment start |
| /* | Multi-line comment start |
| */ | Multi-line comment end |
The Keywords
The keywords (also called a "lexeme") 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.
| 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. |
Study your flashcards while you continue with the book. If you spent 15-30 minutes a day before studying, and another 15-30 minutes before bed, then you could most likely memorize all of these in a few weeks.
Register for From Impostor to Programmer
Register today for the course and get the all currently available videos and lessons, plus all future modules for no extra charge.