C ยท Chapter 4 of 45

C Syntax

C syntax defines the rules for writing valid programs: how statements are structured, how blocks are grouped with curly braces, and how whitespace is treated. Statements always end with a semicolon, and blocks of code are enclosed in { } braces.

C is case-sensitive, so `Total` and `total` are different identifiers. Indentation is not required by the compiler but is essential for readable code.

Syntax
int main() {
  statement1;
  statement2;
}

Statements and blocks

Each instruction (a statement) ends with a semicolon. Related statements are grouped into a block using curly braces, such as the body of a function or an if statement.

Identifiers and case sensitivity

Names for variables and functions can contain letters, digits and underscores but cannot start with a digit. C distinguishes uppercase from lowercase letters in all identifiers.

Example 1 (c)
#include <stdio.h>

int main() {
  int age = 25;
  printf("Age: %d\n", age);
  return 0;
}
Output
Age: 25

A block is enclosed in braces, and each statement ends with a semicolon.

Example 2 (c)
#include <stdio.h>

int main() {
  int Age = 1;
  int age = 2;
  printf("%d %d\n", Age, age);
  return 0;
}
Output
1 2

Age and age are treated as two different variables because C is case-sensitive.

Key points

  • Statements end with a semicolon.
  • Curly braces { } group statements into blocks.
  • C is case-sensitive.
  • Indentation improves readability but is not required by the compiler.
๐Ÿ’ก Note: Consistent indentation style makes your code much easier to read and debug, even though C ignores whitespace.

๐Ÿ“ Quick Quiz

1. What character ends a C statement?

2. Is C case-sensitive?

3. What symbol groups statements into a block?