Learn C Programming

Lesson 3 of 8 · Meet C: Your First Programs, Symbol by Symbol

Module 1 · Meet C: Your First Programs, Symbol by Symbol

The Symbol Dictionary: Every Sign in C and What It Says

FreeReading

In this lesson

  • Name every symbol C uses, with its proper name and one use.
  • Recognise the four bracket kinds on sight and say which job each one does.
  • Tell apart the six pairs that look alike, starting with a single quote and a double quote.

Bob points at his screen and asks Maria what the squiggly thing does. Maria asks which squiggly thing. There are three on that line.

Neither of them can look it up, because neither knows what it is called. This lesson is the dictionary. It turns "what does the little hat thing do" into "what does the caret operator do in C", which has an answer in one search.

Keep this lesson open while you work through the rest of the track. It is a reference page, not a chapter to memorise.

Brackets and braces: four pairs, four jobs

C uses four kinds of bracket, and they are not interchangeable. Using the wrong one is the fastest way to a confusing message.

SymbolProper nameAlso calledUse in CFirst seen
( )Parenthesesround brackets, parensFunction calls, parameter lists, conditions, grouping, castsM1 lesson 1
{ }Bracescurly brackets, curly bracesBlocks, function bodies, initialiser listsM1 lesson 1
[ ]Bracketssquare bracketsArray declaration and indexingM9
< >Angle bracketschevronsSystem header includes, and as less-than and greater-thanM1 lesson 1

So parentheses call and group, braces hold blocks, brackets index arrays, and angle brackets do two unrelated jobs.

Arithmetic and assignment

SymbolProper nameAlso calledMeaning in CFirst seen
+Plus signplusAddition, and unary plusM1 lesson 2
-Minus signhyphen, dashSubtraction, and negationM1 lesson 4
*Asteriskstar, timesMultiplication; also declares and follows a pointerM1 lesson 2
/Forward slashslash, solidusDivision; also starts // and /* commentsM1 lesson 2
%Percent signpercent, modRemainder; also begins a printf placeholderM4
=Equals signequals, getsAssignment, never comparisonM1 lesson 2
++Double plusincrementAdd oneM4
--Double minusdecrementSubtract oneM4
+=Plus equalsadd and assignx += 3 is x = x + 3M4

Read = out loud as "gets", not as "equals". That one habit prevents the trap from the last lesson.

Comparison and logic

SymbolProper nameAlso calledMeaning in CFirst seen
==Double equalsequal toIs equal to; gives 1 or 0M1 lesson 2
!=Exclamation equalsnot equalIs not equal toM1 lesson 3
< >Less-than, greater-thanangle bracketsComparisonM1 lesson 3
<= >=Less-or-equal, greater-or-equalComparisonM1 lesson 3
&&Double ampersandlogical ANDTrue only when both sides are trueM5
||Double vertical barlogical ORTrue when either side is trueM5
!Exclamation markbang, logical NOTTurns true into false and backM5

Every symbol in this table gives you a number: 1 for true, 0 for false. Example 2 below prints all six of the comparisons.

Punctuation and structure

SymbolProper nameAlso calledUse in CFirst seen
;SemicolonEnds a statementM1 lesson 1
,CommaSeparates arguments, declarations and list itemsM1 lesson 1
.Full stopperiod, dotDecimal point, and access to a member of a structM12
:ColonLabels, case, the second half of ?M5
?Question markternaryThe conditional operator, paired with :M5
'Single quoteapostropheOne character: 'A'M1 lesson 4
"Double quotequotation markA string: "Hello"M1 lesson 1
\Backslashreverse slashStarts an escape sequence such as \nM1 lesson 1
#Hashnumber sign, poundPreprocessor directivesM1 lesson 1
&Ampersandand signAddress-of, and bitwise ANDM1 lesson 1
|Vertical barpipeBitwise ORM4
^Carethat, circumflexBitwise XORM4
~TildesquiggleBitwise NOTM4
_Underscorelow lineAllowed inside names, and the usual word separatorM1 lesson 5
->ArrowA struct member, reached through a pointerM12

That is the whole punctuation set. Nothing else in C is a symbol you have not now met.

The six pairs that look alike

These are the pairs that cost beginners hours. Each row is two symbols that sit next to each other on the keyboard and mean unrelated things.

PairThe first oneThe second oneHow to keep them apart
'A' and "A"One character, 1 byteA string, 2 bytes: the letter and the terminatorSingle quote holds exactly one character
= and ==Stores a valueCompares two valuesSay "gets" for one, "is equal to" for the other
& and &&Address-of, or bitwise ANDLogical ANDTwo symbols means a yes-or-no question
/ and \Division, and comment startsEscape sequencesThe one that leans forward divides
* in two jobsa * b multipliesint *p declares a pointerA type on the left means a pointer
<stdio.h> and a < bA header to includeA comparisonOnly a #include line uses the first

Exercise 2 below is a program that uses four of these six in a row, and asks you to predict what it prints.

Escape sequences: the characters with no shape

A backslash inside a string starts an escape sequence. It is how you type a character that has no visible shape of its own, or one that would otherwise end the string.

EscapeNameWhat it does
\nNewlineMove to the start of the next line
\tHorizontal tabJump to the next tab stop
\\BackslashPrint one literal backslash
\"Double quotePrint a quote without ending the string
\'Single quotePrint a literal apostrophe
\0Null characterMarks the end of a C string
\rCarriage returnMove to the start of the current line
\aAlertRing the terminal bell
\bBackspaceMove back one character
%%Percent, doubledNot an escape: this is how printf prints one %

Each escape is one character in the output, however many you typed. Counting that correctly is the brain teaser at the end.

Example 1: all four bracket kinds, annotated

One short program that uses every bracket C has, with a comment naming each job.

#include <stdio.h>        /* angle brackets: a system header */

int main(void)             /* parentheses: the parameter list */
{                          /* braces: the body of main */
    int marks[3] = {90, 85, 77};   /* brackets declare, braces initialise */

    printf("%d\n", marks[0]);      /* parentheses call, brackets index */
    return 0;
}
90

Arrays are Module 9, so do not worry about marks[3] yet. Notice only that four different brackets appear, and that swapping any pair of them breaks the program.

Run in Compiler
Example 2: every comparison, printed

Comparisons are not magic. Each one is an expression whose value is 1 or 0, and printf can print it like any other number.

#include <stdio.h>

int main(void)
{
    int a = 7;
    int b = 4;

    printf("a == b gives %d\n", a == b);
    printf("a != b gives %d\n", a != b);
    printf("a > b  gives %d\n", a > b);
    printf("a < b  gives %d\n", a < b);
    printf("a >= b gives %d\n", a >= b);
    printf("a <= b gives %d\n", a <= b);
    return 0;
}
a == b gives 0
a != b gives 1
a > b  gives 1
a < b  gives 0
a >= b gives 1
a <= b gives 0

Change b to 7 and run it again. Four of the six answers flip, and the two that do not are the pair you should be able to name.

Run in Compiler
Example 3: the escapes, all at once

This is the program to keep. It prints a tab-separated table, a quoted phrase, a Windows path and a percentage, which covers every escape you will use this month.

#include <stdio.h>

int main(void)
{
    printf("Name\tMarks\n");
    printf("Zara\t92\n");
    printf("Kenji\t87\n");
    printf("She said \"well done\"\n");
    printf("Path: C:\\progsity\\bin\n");
    printf("Discount: 15%%\n");
    return 0;
}
Name	Marks
Zara	92
Kenji	87
She said "well done"
Path: C:\progsity\bin
Discount: 15%

Look at the last two lines in the source. Two backslashes print one, and two percent signs print one. Everywhere else, the first of the pair is the instruction.

Run in Compiler

Where this is used

  • Reading a compiler message. expected '=', ',', ';', 'asm' or '__attribute__' before '-' token is unreadable until you can say "before the minus sign". The message names symbols, so you have to name them too.
  • Asking for help. A forum or a search engine answers "what does the caret operator do in C" and cannot answer "what does the little hat thing do". Exact words are a debugging tool.
  • Reading documentation. The C standard library pages describe printf in terms of conversion specifications beginning with a percent sign. The names in this lesson are the names those pages use.
  • Code review at work. "You have an ampersand where you want a double ampersand" is a sentence a reviewer says out loud. It saves the reader from re-reading the line.

Common mistakes

1. A single-quoted string.

printf('Hello\n');

GCC 12 gives warning: multi-character character constant and then a warning about passing an integer where a pointer belongs. It builds, and usually crashes. Single quotes hold exactly one character; text needs double quotes.

2. A double-quoted single character.

char grade = "A";

On GCC 12 this is warning: initialization of 'char' from 'char *' makes integer from pointer without a cast, and an error on GCC 14. You stored an address in a one-byte box. What you wanted was 'A'.

3. One backslash where you meant a literal backslash.

printf("Path: C:\progsity\bin\n");

There is no message you can rely on here. \p is undefined behaviour that GCC quietly prints as p, and \b is a real escape, the backspace, which eats the character before it. The output looks almost right and is not. Double every literal backslash.

4. A hyphen in a name.

int total-marks = 90;

The message is error: expected '=', ',', ';', 'asm' or '__attribute__' before '-' token. C read the hyphen as subtraction and then found nothing to subtract from. Names use the underscore, which is why the next lesson calls it total_marks.

Brain teaser

Amara writes this one line and claims the string holds six characters. Bob counts nine, because that is what he can see in the source.

printf("a\\b\"c\n");

Answer three questions. What exactly appears on the screen? How many characters does the printed text have? And how many bytes would the string "a\\b\"c" take up in memory, with no \n in it?

Count every escape pair as one character. Then remember the invisible character C adds at the end of every string, the one in the escape table with the value zero.

Exercise 1Easy

Zara was taking notes by hand and wrote the names of the symbols instead of the symbols. Put the real symbols back, so the program prints the expression and its value.

Input. One line with two integers a b.

Output. One line, exactly: (a + b) * 2 = X, with the two numbers and the answer filled in.

Constraints. 0 <= a, b <= 1000.

Sample. Input 7 4 gives (7 + 4) * 2 = 22. The parentheses and the asterisk are printed as characters, and the doubling is real arithmetic.

#include <stdio.h>

int main(void)
{
    int a, b;
    scanf("%d %d", &a, &b);

    /* Print: open-paren, a, space, plus, space, b, close-paren,
       space, asterisk, space, 2, space, equals, space, the value. */
    printf("%d\n", 0);

    return 0;
}

Graded against hidden tests in the module's Problems lesson, as restore-symbols.

Run in Compiler
Exercise 2Easy

Predict the four lines this program prints, on paper, before you run it. Then run it and check.

#include <stdio.h>

int main(void)
{
    int a = 6;
    int b = 3;
    char grade = 'A';

    printf("%d\n", a & b);
    printf("%d\n", a && b);
    printf("%c\n", grade);
    printf("%d\n", a / b);
    return 0;
}

What to notice. Lines 1 and 2 differ by one character and give different answers. Bitwise AND is Module 4, so for now you only need to see that the two symbols are not the same symbol.

Check yourself. Write down what each of the four lines is called before you look: bitwise AND, logical AND, a character, integer division. If you can name all four, this lesson has done its job.

Run in Compiler

Common doubts

  • Do I have to memorise all of these?

    No. You need the four bracket names and the six look-alike pairs. The rest is a page to come back to, which is why the last column says where each symbol first appears.

  • Why does C use so much punctuation?

    Because it was designed for a slow terminal in 1972, where a short symbol beat a long word. Python chose words for some of the same jobs, which is easier to read and more to type.

  • Are the "also called" names wrong?

    They are informal, and everybody uses them. Say curly brackets if you like, and know that the standard and every error message call them braces.

  • Which symbols can I use in a name?

    Letters, digits and the underscore, and a name may not start with a digit. Everything else in these tables is an operator or punctuation, so it ends the name.

  • What about the symbols I see in other people's code, like <<?

    They are combinations of the ones here. << is two less-than signs and means a bit shift, from Module 4. If you can name the parts, you can look up the pair.

Key takeaways

  • Four bracket kinds, four jobs: parentheses call, braces block, brackets index, angle brackets include or compare.
  • Every symbol has a proper name, and error messages use it.
  • Single quotes hold one character; double quotes hold a string.
  • & and &&, = and ==, / and \ are three pairs that look alike and mean unrelated things.
  • An escape sequence is one character in the output, however many you typed.
  • Two backslashes print one, and two percent signs print one.

Next you will see why a character is really a small number, and the complete table that says which number each character is.

End of lesson 3

Mark it done, and your progress moves with you.

Next: The Complete ASCII Table, All 128 Rows