Syntax and Comments

C's syntax is compact: statements end with semicolons, blocks are wrapped in braces, and whitespace is mostly ignored. This lesson covers those basic rules, identifiers and keywords, and how to write comments that help rather than clutter.

Statements and blocks

  • A statement is one instruction and ends with ;.
  • A block groups statements between { and } — the body of a function, an if or a loop.
  • C is case-sensitive: total, Total and TOTAL are three different names, and Printf is not printf.

Whitespace

Spaces, tabs and line breaks just separate words. These two programs are identical to the compiler:

C
#include <stdio.h>
int main(void){int a=2;int b=3;printf("%d\n",a*b);return 0;}
Output
6
C
#include <stdio.h>

int main(void) {
    int a = 2;
    int b = 3;
    printf("%d\n", a * b);
    return 0;
}
Output
6

Write it the second way. Indent each block by four spaces (or the style your project uses), put one statement per line, and add spaces around operators. Tools like clang-format can format code automatically.

The exceptions: preprocessor directives such as #include must be on their own line, and a string literal can't contain a raw line break. Adjacent string literals are joined, which helps with long text:

C
#include <stdio.h>

int main(void) {
    printf("This message is long, so it is split "
           "across two lines of source code.\n");
    return 0;
}
Output
This message is long, so it is split across two lines of source code.

Comments

C
#include <stdio.h>

/*
 * A block comment can span several lines.
 * It's often used at the top of a file or function.
 */
int main(void) {
    int minutes = 135;               // a line comment runs to the end of the line
    int hours = minutes / 60;        /* or a short block comment */
    printf("%d h %d min\n", hours, minutes % 60);
    return 0;
}
Output
2 h 15 min

Block comments can't be nested: the first */ ends the comment. To temporarily disable a chunk of code that contains comments, use #if 0 … #endif instead.

Good comments explain why, not what: // convert to minutes above t * 60 adds nothing, but // the sensor reports in tenths of a degree explains a surprising / 10.

Identifiers

Names for variables, functions and types are identifiers:

  • Letters, digits and underscores only, and they can't start with a digit: total_price, count2, _tmp.
  • They can't be a keyword.
  • Avoid names beginning with an underscore followed by a capital letter or a second underscore (like _Count or __x): those are reserved for the compiler and standard library.

The usual C style is snake_case for variables and functions and UPPER_CASE for macros and constants.

Keywords

C has a small set of reserved words. The ones you'll use most:

TXT
int  char  float  double  long  short  unsigned  signed  void
if  else  switch  case  default  while  do  for  break  continue  return  goto
struct  union  enum  typedef  const  static  extern  sizeof

C23 adds a few more, including bool, true, false, nullptr and constexpr (lesson 55).

Declarations before use

C reads a file from top to bottom, and you must declare something before you use it — a variable before you read it, and a function (or at least its prototype) before you call it. That's also why the #include lines go at the top.

C
#include <stdio.h>

int main(void) {
    total = 5;
    int total;
    printf("%d\n", total);
    return 0;
}
Compiler error
main.c:4:5: error: use of undeclared identifier 'total'

Try it yourself

Reformat this squashed program so it's readable, and add one comment explaining what it calculates.

Try it yourself · main.c
Edit the code, copy it into main.c, then compile and run it with cc -std=c17 -Wall main.c -o main && ./main. How to set up a C compiler
Output
Area: 60
Perimeter: 34