In this chapter we are going to cover the basics of c which helps as foundation of C language.
Syntax in C
The syntax of a programming language is like grammatical rules. It helps us arrange and format statements, expressions, and other program elements in a way that is logical and meaningful. By following these rules, we ensure that our programs are well-structured and work as intended.
The basic syntax of C program is given below:
//header file inclusion
#include <filename.h>
int main()
{
/* main function block */
}
In each of the upcoming chapters, we will provide the syntax for using every programming topic discussed.
Comments
While writing a program, there must be some description for the written code, this helps the reader to understand the code more easily and there might be a case where we don’t what to remove the lines but at the same time we don’t what compiler to consider those lines, to do this programmer have to comment those lines. To comment a single line, the line have to start with // and to do a multi line comment, first line have to start with /* and last line have to end with */. this commented lines are removed from the code at the time of preprocessor stage( 1st stage of compilation).
//This is a single line comment
/* This is
* a multi line
* comment
*/
Tokens
In C, a token is the smallest individual unit in a program. It can be a keyword, identifier, operator, constant, or separator. Tokens are used to construct meaningful statements and expressions in the code.
Keywords
Keywords are reserved words in C that have special meanings and predefined functionalities. These words cannot be used as identifiers (variable names or function names) because they are already assigned a specific purpose by the language.
In the C programming language, there are 32 keywords that are reserved, they are:
auto | break | case | char |
continue | default | do | double |
else | enum | extern | float |
for | goto | if | inline |
int | long | register | return |
short | signed | sizeof | static |
struct | switch | typedef | union |
unsigned | void | volatile | while |
Identifiers
Identifiers are names used to identify variables, functions, or other entities in a C program. keywords are predefined while identifiers are user defined. An identifier can be composed of letters (both uppercase and lowercase), digits, and underscore (_) but must follow certain rules, such as:
- It should not start with special characters or numbers except with underscore.
- Should not be a keyword.
Examples of identifiers include: variable names like “count,” function names like “calculate_sum,”
Separators
Separators in C are special symbols used to separate or group elements of a program. They help define the structure and syntax of the code. Common separators in C include parentheses (), braces {}, square brackets [], commas ,, semicolons ;, and the period . (for structure or member access).
Constants
In C programming, constants are fixed values that cannot be altered or modified during the execution of a program. They represent specific data values that remain constant throughout the program’s execution. Constants are used to represent values that do not change, such as mathematical values, physical constants, or fixed values required by the program logic.
There are different types of constants in C:
- Integer Constants: These are whole numbers without a decimal point. For example: 5, -10, 0.
- Floating-Point Constants: These are numbers with a decimal point. They can be represented in either standard or exponential form. For example: 3.14, -0.25, 1.5e-3.
- Character Constants: These represent individual characters enclosed in single quotes. For example: ‘A’, ‘b’, ‘3’.
- String Constants: These represent sequences of characters enclosed in double quotes. For example: “Hello”, “C programming”, “123”.
- Enumeration Constants: These are user-defined symbolic names given to a set of related integer values. They are defined using the enum keyword. For example:
enum Month { January, February, March, /* ... */ };
- Symbolic Constants: These are defined using the #define preprocessor directive and provide a way to give names to specific values. For example:
#define PI 3.14159 #define MAX_VALUE 100
Prev << Behind C program
Next >> Datatypes in C