← Back to C Course | Chapter 1: Introduction & Basics | Lesson 5 of 21

C Syntax & Structure

C has strict spelling and punctuation rules, like a language with careful grammar. Statements end with semicolons, curly braces group steps, and capital letters matter.
Syntax
c
#include <header.h>

return_type function_name() {
  statement;
  statement;
}

Semicolons

Every C statement must end with a semicolon so the compiler knows exactly where one instruction stops and the next begins; forgetting one is one of the most common beginner errors, and it often produces a confusing error on the following line instead.

Example: Semicolons

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare x and set it to 10
	int x = 10;
	// Print formatted text to the screen
	printf("%d", x);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Curly Braces

Curly braces { } group multiple statements into a single block, defining the body of a function, loop, or conditional. The compiler treats everything between a matching pair as one unit that executes (or doesn't) together.

Example: Curly Braces

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	{
		// Print formatted text to the screen
		printf("Grouped inside braces");
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Tokens

A token is the smallest meaningful piece the compiler recognizes when reading your source code -- keywords, variable names, numbers, symbols like + or ;, and string literals are all individual tokens the compiler parses one at a time.

Example: Tokens

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare total and set it to 100
	int total = 100;
	// Print formatted text to the screen
	printf("%d", total);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Case Sensitivity

C treats uppercase and lowercase letters as entirely different characters, so main must be written exactly in lowercase, and variables named total and Total are two separate, independent variables in the same program.

Example: Case Sensitivity

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare total and set it to 5
	int total = 5;
	// Declare Total and set it to 10
	int Total = 10;
	// Print formatted text to the screen
	printf("%d %d", total, Total);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Indentation

Consistent indentation doesn't affect how the compiler runs your code -- C ignores whitespace -- but it's essential for you and other developers to visually trace which statements belong inside which block, especially in nested loops or conditionals.

Example: Indentation

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Check whether 1
	if (1) {
		// Print formatted text to the screen
		printf("Indentation shows nesting");
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting the semicolon at the end of a statement, so the compiler reports an error on the following line.
  2. Writing Printf or INT when C is case-sensitive and only printf and int exist.
  3. Leaving a { without its closing }, causing an 'expected } at end of input' error.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.