Basic Syntax
We have already seen the basic structure of a C program, which will help us understand other basic building blocks of the C language.
Tokens in C
A C program consists of various tokens, which can be keywords, identifiers, constants, string literals, or symbols. For example, the following C statement contains five tokens:
printf("Hello, World! \n");
These five tokens are:
printf
(
"Hello, World! \n"
)
;
Semicolon ;
In a C program, a semicolon is a statement terminator. That is, each individual statement must be ended with a semicolon. It indicates the end of a logical entity.
For example, here are two different statements:
printf("Hello, World! \n");
return 0;
Comments
C language has two ways of commenting:
// Single-line comment
A single-line comment starting with // can occupy a single line.
/* Single-line comment */
/*
Multi-line comment
Multi-line comment
Multi-line comment
*/
The /* */ format of comments can be single-line or multi-line.
You cannot nest comments within comments, and comments cannot appear within a string or character literal.
Identifiers
C identifiers are names used to identify variables, functions, or any other user-defined item. An identifier starts with a letter A-Z or a-z or an underscore _ followed by zero or more letters, underscores, and digits (0-9).
Punctuation characters such as @, $, and % are not allowed within C identifiers. C is a case-sensitive programming language. Therefore, in C, Manpower and manpower are two different identifiers. Here are some valid identifiers:
mohd zara abc move_name a_123
myname50 _temp j a23b9 retVal
Keywords
The following table lists the reserved words in C. These reserved words cannot be used as constant names, variable names, or other identifier names.
| Keyword | Description |
|---|---|
| auto | Declare automatic variable |
| break | Break out of current loop |
| case | Switch statement branch |
| char | Declare character variable or function return type |
| const | Define constant; if a variable is modified by const, its value cannot be changed |
| continue | End current loop, begin next iteration |
| default | Default branch in switch statement |
| do | Loop body |
| double | Declare double precision floating-point variable or function return type |
| else | Conditional statement negative branch (used with if) |
| enum | Declare enumeration type |
| extern | Declare variable or function defined elsewhere |
| float | Declare floating-point variable or function return type |
| for | A type of loop |
| goto | Unconditional jump statement |
| if | Conditional statement |
| int | Declare integer variable or function |
| long | Declare long integer variable or function return type |
| register | Declare register variable |
| return | Subroutine return statement (can take parameters or not) |
| short | Declare short integer variable or function |
| signed | Declare signed type variable or function |
| sizeof | Calculate size of data type or variable (in bytes) |
| static | Declare static variable |
| struct | Declare structure type |
| switch | Used for switch statement |
| typedef | Used to give a type a new name |
| unsigned | Declare unsigned type variable or function |
| union | Declare union type |
| void | Declare function with no return value or no parameters, or declare void pointer |
| volatile | Indicates that a variable can be changed implicitly |
| while | Loop condition for loop statement |
C99 Added Keywords
| _Bool | _Complex | _Imaginary | inline | restrict |
C11 Added Keywords
| _Alignas | _Alignof | _Atomic | _Generic | _Noreturn | | _Static_assert | _Thread_local | | | |
Whitespace in C
A line containing only whitespace, possibly with a comment, is known as a blank line, and the C compiler completely ignores it.
In C, whitespace is used to describe blank spaces, tabs, newline characters, and comments. Whitespace separates different parts of a statement so that the compiler can identify where one element of a statement (like int) ends and the next begins. Therefore, in the statement:
int age;
Here, int and age must be separated by at least one whitespace character (usually a space), so the compiler can distinguish between them. On the other hand, in the statement:
fruit = apples + oranges; // Get the total number of fruits
The whitespace characters between fruit and =, or = and apples are not necessary, but you can add them as needed to enhance readability.