C Syntax


Every programming language has a set of rules that define how instructions are written and executed. These rules are called syntax. Just like grammar in English ensures that sentences make sense, syntax in programming makes sure that your code is valid and understandable to the compiler.

In this chapter, you’ll learn how C programs are structured, how statements are written, and how different elements like variables, data types, and semicolons come together to form a complete program.

What is C Syntax?

C syntax refers to the basic structure and rules that define how a valid C program is written. These rules decide how keywords, operators, and identifiers are arranged so the compiler can understand and execute the instructions correctly.

If you make a syntax error — for example, forget a semicolon or misspell a keyword — the compiler will throw an error, and your program won’t run until you fix it.

Basic Structure of a C Program

A C program generally follows a simple structure. Let’s look at an example:

#include <stdio.h>

int main() {
    printf("Hello, World!");
    return 0;
}

Here’s how this structure works:

  1. Preprocessor Directive:
    The first line #include <stdio.h> tells the compiler to include the standard input/output library before compiling the program. This library contains commonly used functions like printf() and scanf().

  2. Main Function:
    The main() function is where the program starts execution. Every C program must have one and only one main() function.

  3. Curly Braces {}:
    These mark the beginning and end of a block of code. Anything inside the braces belongs to that block.

  4. Statements:
    Each line inside the block is a statement. In this example, printf("Hello, World!"); is a statement that prints text on the screen.

  5. Semicolon ;:
    Every statement in C must end with a semicolon. It tells the compiler where one instruction ends and the next begins.

  6. Return Statement:
    return 0; indicates the program executed successfully. The value 0 is returned to the operating system.

Case Sensitivity in C

C is a case-sensitive language. That means main, Main, and MAIN are three different identifiers.

For example:

int num = 5;
printf("%d", Num); // Error: ‘Num’ is undefined

The above code will throw an error because num and Num are not the same variable.

Comments in C

Comments are ignored by the compiler. They are used to explain code and make it easier to understand.

There are two types of comments:

  1. Single-line comment:

    // This is a single-line comment
    
  2. Multi-line comment:

    /* This is
       a multi-line
       comment */
    

Good programmers use comments to describe the purpose of code blocks, especially in long programs.

Tokens in C

The smallest individual units in a C program are called tokens. A token can be a keyword, identifier, constant, string literal, or symbol.

C tokens are divided into six main categories:

  1. Keywords – Reserved words like int, return, if, else, etc.

  2. Identifiers – Names given to variables, functions, and arrays.

  3. Constants – Fixed values like 10, 3.14, 'A'.

  4. Strings – A sequence of characters enclosed in double quotes ("Hello").

  5. Operators – Symbols used to perform operations (+, -, *, /).

  6. Special Characters – Braces {}, brackets [], commas ,, semicolons ;, etc.

Keywords in C

C has a set of reserved words that have predefined meanings. You cannot use them as variable names.
Examples include:

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

If you try to use a keyword as a variable name, you’ll get a compilation error.

Identifiers

Identifiers are names given to elements like variables, arrays, and functions. They help the compiler identify what’s being referred to.

Rules for naming identifiers:

  • Must begin with a letter or underscore (_).

  • Can contain letters, digits, and underscores.

  • Cannot contain spaces or special characters.

  • Are case-sensitive.

  • Cannot be a keyword.

Examples:
Valid identifiers: name, _age, marks1
Invalid identifiers: 1stName, total%, void

Variables and Data Types

A variable is a named memory location used to store data. Each variable must have a type that defines the kind of data it can hold.

Example:

int age = 25;
float height = 5.6;
char grade = 'A';

Here:

  • int is used for integers.

  • float is used for decimal values.

  • char is used for single characters.

C requires you to declare variables before using them.

Input and Output

C uses functions from the stdio.h library for input and output.

Output using printf():

printf("My age is %d", age);

%d is a format specifier used for integers.

Input using scanf():

scanf("%d", &age);

The & symbol is used to store the input value in the variable age.

Whitespace and Indentation

Whitespace in C (like spaces, tabs, and newlines) is mostly ignored by the compiler. However, proper indentation and spacing make code easier to read.

Example:

int main() {
    int num = 10;
    printf("%d", num);
    return 0;
}

This is much easier to understand than writing everything in one line.

Statements and Blocks

C statements are instructions that tell the computer what to do. They can be simple (like an assignment) or compound (like a set of statements inside { }).

Example:

int a = 5;
int b = 10;
int sum = a + b;
printf("%d", sum);

A block refers to a group of statements enclosed within { }. Blocks are used in loops, conditionals, and functions.

Example Program

Let’s combine everything you’ve learned so far:

#include <stdio.h>

int main() {
    int a, b, sum;

    // Asking user for input
    printf("Enter two numbers: ");
    scanf("%d %d", &a, &b);

    // Calculating sum
    sum = a + b;

    // Displaying result
    printf("Sum = %d", sum);

    return 0;
}

This simple program takes two numbers from the user and prints their sum.
It includes all key syntax rules: header inclusion, main function, variable declaration, input/output, and semicolons.

Summary of the Tutorial

C syntax defines how programs are written and interpreted. It includes everything from structure and keywords to statements and data types. Once you understand the syntax well, you can easily read, write, and debug C programs.

The best way to master syntax is through practice — write small programs, experiment with variable names, and try to identify syntax errors yourself. Each mistake helps you understand the language more deeply.


Practice Questions

  1. What is meant by syntax in a programming language, and why is it important?

  2. Identify the error in the following code snippet:

    int Main() {
        printf("Hello");
        return 0;
    }
    
  3. Explain the role of the #include <stdio.h> statement in a C program.

  4. Write a C statement to declare an integer variable named score and initialize it with 50.

  5. Which of the following are valid identifiers in C: _total, 2ndValue, grade, float?

  6. What is the difference between a single-line comment and a multi-line comment in C?

  7. Write a small C program that takes two numbers as input and prints their difference.

  8. Why is C considered a case-sensitive language? Give an example.

  9. List the six types of tokens in C and give a brief example for each.

  10. Correct the syntax error in the following code:

    int a = 5
    int b = 10;
    sum = a + b;

Try a Short Quiz.

coding learning websites codepractice

No quizzes available.

Go Back Top