Lesson 7 of 60 – C Syntax
12%

C Syntax

C syntax means the set of rules that we follow when writing a C program. Just like English has grammar rules, C has syntax rules that tell the compiler how a program should be written.

Note: Learning C syntax is important because even a small syntax mistake can cause a compilation error.

1. What is C Syntax?

C syntax is the collection of rules used to write valid C programs. It defines how keywords, variables, operators, functions, statements, and other elements should be written.

int age = 20;

The above statement follows basic C syntax rules.

2. Basic Structure of a C Program

A basic C program can look like this:

#include <stdio.h>

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

This program contains a header, the main() function, statements, and a return statement.

3. C is Case-Sensitive

C is a case-sensitive programming language. Uppercase and lowercase letters are treated as different characters.

int age;
int Age;

Here, age and Age are different identifiers.

4. Statements in C

A statement is an instruction that tells the program to perform an action. For example:

printf("Hello");

Most ordinary C statements end with a semicolon.

5. Semicolon

The semicolon ; is used to terminate many C statements.

int age = 20;
printf("%d", age);

Forgetting a required semicolon can produce a compilation error.

6. Curly Braces

Curly braces { } are used to define a block of code.

int main()
{
    printf("Hello");
}

The statements between the braces belong to the main() function.

7. Parentheses

Parentheses ( ) are commonly used with functions and expressions.

main()
printf("Hello");

Function names are followed by parentheses when calling or defining functions.

8. Keywords

Keywords are reserved words that have special meaning in C.

int
return
if
else
for
while

Keywords cannot normally be used as names for variables or functions.

9. Identifiers

Identifiers are names given to program elements such as variables and functions.

int age;
int marks;

Here, age and marks are identifiers.

10. Rules for Identifiers

Common identifier rules include:

  • Use letters, digits, and underscores.
  • Do not start an identifier with a digit.
  • Do not use C keywords as identifiers.
  • C is case-sensitive.
student_name
marks1
_total

11. Valid and Invalid Identifiers

Identifier Example
Valid studentName
Valid marks1
Valid _total
Invalid 1marks
Invalid student-name

12. Variables

A variable stores a value that can be used by a program. A variable declaration specifies its type and name.

int age;

A value can be assigned later:

age = 20;

13. Variable Declaration and Initialization

A variable can be declared and initialized in the same statement.

int age = 20;

Here, age is the variable and 20 is its initial value.

14. Data Types

A data type tells the compiler what kind of value a variable can store.

int age = 20;
float price = 10.5;
char grade = 'A';

Common basic data types include int, float, double, and char.

15. String Literals

Text written inside double quotation marks is a string literal.

printf("Hello World");

The text Hello World is the string passed to printf().

16. Character Constants

A single character is commonly written inside single quotation marks.

char grade = 'A';

Here, 'A' is a character constant.

17. Comments in C

Comments are used to add explanations to source code. They are not executed as program statements.

// This is a comment

/*
   This is a
   multi-line comment
*/

18. Single-Line Comments

A single-line comment starts with //.

// Display the message
printf("Hello");

Everything after // on that line is treated as a comment.

19. Multi-Line Comments

Multi-line comments are enclosed between /* and */.

/*
   Calculate total marks
   and display the result
*/

20. Whitespace

Spaces, tabs, and line breaks can make code easier to read. In many places, C ignores extra whitespace between tokens.

int age = 20;

Good formatting makes programs easier to understand and maintain.

21. Indentation

Indentation is used to visually organize blocks of code.

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

Although indentation does not normally determine block structure in C, consistent indentation improves readability.

22. Operators

Operators are symbols used to perform operations on values.

int total = 10 + 20;

Here, + is the addition operator.

23. Expressions

An expression combines values, variables, and operators to produce a value.

int result = a + b;

The expression a + b calculates a value that is assigned to result.

24. Function Calls

A function can be called by writing its name followed by parentheses.

printf("Hello");

Here, printf() is being called with a string argument.

25. C Syntax with if

Conditional statements use a specific syntax.

if (age >= 18)
{
    printf("Adult");
}

The condition is written inside parentheses and the block is written inside curly braces.

26. C Syntax with Loops

Loops also follow specific syntax rules.

for (int i = 1; i <= 5; i++)
{
    printf("%d\n", i);
}

The loop header contains initialization, condition, and update expressions.

27. Common Syntax Errors

Some common beginner mistakes are:

  • Missing semicolon.
  • Missing closing brace.
  • Misspelling a keyword.
  • Using an invalid identifier.
  • Missing quotation marks.
  • Incorrect parentheses.

28. Example with Several Syntax Rules

#include <stdio.h>

int main()
{
    int age = 20;

    if (age >= 18)
    {
        printf("Adult\n");
    }

    return 0;
}

This example uses a header, function, variable, condition, braces, parentheses, semicolons, and a function call.

29. Good C Coding Practices

  • Use meaningful variable names.
  • Indent code consistently.
  • Use comments where they improve understanding.
  • Keep statements properly terminated.
  • Use matching braces and parentheses.
  • Keep the code organized and readable.

30. Complete Syntax Example

#include <stdio.h>

int main()
{
    int marks = 80;

    printf("Marks = %d\n", marks);

    if (marks >= 40)
    {
        printf("Pass");
    }

    return 0;
}

This example demonstrates several important C syntax rules that you will use throughout your C programming course.

📌 Key Points

  • C syntax defines the rules for writing valid C programs.
  • C is case-sensitive.
  • Most C statements end with a semicolon.
  • Curly braces define blocks of code.
  • Parentheses are commonly used with functions and conditions.
  • Keywords have special meanings in C.
  • Identifiers are names given to variables, functions, and other program elements.
  • Comments are ignored during normal program execution.
  • Good indentation makes code easier to read.
  • Syntax errors must be corrected before a program can compile successfully.

🧠 Quick Quiz

Question: Which symbol is normally used to terminate a C statement?