Introduction:
C is a general purpose, procedural, structured programming language developed at Bell Laboratories of USA in the year 1972. It was developed by Dennis Ritchie and hence he is known as the father of C.
Use of ‘C’ Language
C was originally developed for writing system software. The applications of C is not only limited to the development of system software like Windows or Linux operating system, but also in the development of GUIs (Graphical User Interfaces) and, IDEs (Integrated Development Environments). It is widely used for writing application software including word processors, spreadsheets, games, robotics, databases and graphics applications.
Variables:
Variables are named storage locations in memory. The primary purpose of variable is to store data (value) for later use. Variable is like a container (storage area) with a name in which we can store data. To indicate the storage area, each variable should be given a unique name. Variable names are just the symbolic representation of a memory location. By using a variable name, we are referring to the data stored in the location.
Rules for Naming a variable in C:
There are some rules on choosing variable names. The following rules must be followed while naming variables:
- A variable name consists of letters (both uppercase and lowercase letters), digits, and the underscore.
- The first character must be a letter or an underscore. It can’t start with a digit (number).
- Variable names are case sensitive. The compiler treats the upper- and lower-case letters as different.
- No whitespace is allowed within the variable name.
- Special symbols such as period, semicolon, comma, slash etc. are not allowed other than underscore.
- Any reserved word (keyword) cannot be used as a variable name. e.g. int, float etc.
Data Types:
A data type refers to the type of data being used. The data type defines an attribute to the variable. It defines the set of legal values that the variable can store. A data type specifies the type of data that a variable can store such as integer, float, character etc.
Following are the primary (basic) data types used in C:
- Integer (int)
- Floating point type (float)
- Character data type (char)
Integer (int):
The data type ‘int’ represents whole numbers with a range of values supported by a particular machine. For instance, in a 16-bit word length machine, the integer values lie between -32768 to 32767.
Example:
int roll;
roll=1;
Floating point (float):
It is used to store floating point number. Floating point numbers are numbers that have a decimal point.
Example:
float a;
a=14.752;
Character data type (char):
It is usually used to store a single character. The char keyword defines a character data type.
Example:
char x;
x= ‘a’;
| Type Specifier | Description | Format Specification | Storage (machine dependent) |
| int long int | Integer data (whole numbers) | %d or %i %ld | 2 byte 4 byte |
| float double | Floating point (Decimal value) | %f or %e %f or %e | 4 byte 8 byte |
| char | Character representation String representation | %c %s | 1 byte No of character + 1 byte |
Header Files
Header files act like ‘headers’ to C programs. They are a type of file (with .h extension) that contains function declarations, macro definitions, and other entities which can be used across multiple C source files (.c files).
Here are some of the most frequently used standard library header files:
stdio.h: This is the standard input-output header in C. It contains functions for data input and output, such as printf() for output and scanf() for input.
string.h: This header file is used for manipulating strings. It includes functions like strcpy() (copy a string), strcat() (concatenate strings), and strlen() (calculate string length) etc.
math.h: This header file is used for mathematical computations. It includes a range of functions to perform complex mathematical operations like pow() (power), sqrt() (square root), sin() (sine), and cos() (cosine) etc.
Input and Output Operations:
All input and output operations are carried out through functions such as printf() and scanf(). These functions are collectively known as the standard input and output functions. The stdio.h header file provides functions scanf() for input and printf() for output.
Keywords:
Keywords are reserved words whose meaning has already been fixed to the C compiler. All keywords have a predefined meaning and these meanings cannot be changed. All keywords must be written in lowercase. Since keywords are referred names for a compiler, they cannot be used as variable names because if we do so, we are trying to assign a new meaning to the keyword, which is not allowed. We canoe redefine keywords.
For example:
int age;
Here, int is a keyword that indicates age is a variable of type int (integer).
There are 32 keywords available in C.
| 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 |
Identifiers:
Identifiers are user defined words. These are used to name the variables, functions, arrays, structures, etc. These names can be in uppercase letters, lowercase letter or a combination.
Identifiers must be unique. They are created to give a unique name to an entity to identify it during the execution of the program.
For example:
int age;
int roll;
Here, age and roll are identifiers.
Operators:
An operator can be defined as a symbol that specifies an operation to be performed. Operators help the programmer to perform computation on values.
An operator is a symbol that tells the compiler to perform certain mathematical or logical computations. The data items on which the operators act upon are called operands. Some operators require a single operand to perform operation while others might require two operands.
Categories of operators:
Operators in C can be classified into a number of categories as follows:
Assignment Operator:
An assignment operator is used to assign a value to a variable or to assign the result of an expression to a variable. The = symbol is used as an assignment operator.
The syntax of an assignment is,
Variable name=expression;
For example,
a=5;
Sum=a+b;
Arithmetic Operators:
The arithmetic operators are used to perform mathematical calculations such as addition, subtraction etc. C supports all the common arithmetic operators. These operators can operate on any built –in data types such as int, char and float. However, the modulus operator requires an integral operand (must not be float).
| Operator | Meaning |
| + | Addition |
| – | Subtraction |
| * | Multiplication |
| / | Division |
| % | Modulus |
Relational Operators:
These are used to compare the values two variable or constants. The relational operators are symbols that are used to test the relationship between variables or between a variable and a constant.
For example,
(salary==5000)
Here == is the operator that is used to test equality.
There are six relational operators provided by C. They are:
| Operator | Meaning |
| == | equal to |
| != | not equal to |
| > | greater than |
| < | less than |
| >= | greater than or equal to |
| <= | less than or equal to |
Logical Operators:
Logical operators are symbols that are used to combine two or more expressions containing relational operators. The logical operators are used to combine conditions.
Example:
((x>5)&&(x<10))
(Salary>=10000&&salary<=20000)
C has three logical operators:
|
Operator |
Meaning |
|
&& |
AND |
|
|| |
OR |
|
! |
NOT |
Increment and Decrement Operators:
The increment and decrement operators are two very useful operator used in C.
Increment operators are used to increase the value of a variable by 1. This operator is represented by ++ symbol.
We use decrement operators in C to decrement the given value of a variable by 1. This operator is represented by – – symbol.
Decision Making Statement
in C programming, decision-making statements control the flow of executionbased on specific conditions. These statements allow a program to execute different blocks of code depending on whether a given condition evaluates to true or false.
In real-life situations, we often make decisions depending on conditions (e.g., if it rains, take an umbrella; otherwise, don’t). Similarly, in C programming, we use decision-making statements to execute different parts of code depending on conditions.
Types of Decision-Making Statements
The main types of decision-making statements in C are:
If statement:
The if statement is one of the simplest decision-making statements. This statement executes a block of code only if a specified condition is true. The if statement is used to decide whether a particular statement or block of statements will be executed or not. That is, if a certain condition is true then a block of statement is executed otherwise not.
Syntax:
if (test condition) {
Code if condition is true
}
Explanation:
If: if is a keyword. This keyword initiates the conditional statement.
condition: This is a logical or relational expression enclosed in parentheses (). It is evaluated to determine if it is true or false.If the condition is true, the statements inside the block { } will execute.If the condition is false, the block is skipped.
Note: The curly braces { } are optional if the block contains only one statement, but it’s good practice to always use them for clarity and to avoid errors.
Example:

Output:
Pass
If-else Statements:
The if…else statement is an extension of the if statement. It is used to perform two operations for a single condition. When condition is true it performs one action by executing a statement or a set of statements. When condition is false it skips that part of statements and executes other statement or set of statements. That is when two choices of actions are required if..else statement is useful.
Syntax:
if (condition) {
Code if condition is true
}
else {
Code if condition is false
}
Explanation:
The if keyword initiates the conditional statement. The else keyword provides an alternative path. If the condition in the if statement evaluates to true, the code inside the if block is executed, and the else block is skipped. If the condition in the if statement evaluates to false, the if block is skipped, and the code inside the else block is executed.
Example:

Output:
Fail
If..else-if..else Statement
The if…else-if…else statement is an extension of the if-else statement. It allows us to test multiple conditions sequentially. The if-else if-else statement is used when we want to check multiple conditions one after another. It allows the program to make a decision among several possible options.
This is useful for scenarios like grading systems, menu selections, or categorizing values based on ranges.
Syntax:
if (condition1) {
code if condition1 is true
}
else if (condition2) {
code if condition2 is true
}
else if (condition3) {
code if condition3 is true
}
else {
code if all conditions are false
}
Explanation:
The program first evaluates condition1. If condition1 is true, the code inside the if block is executed, and the rest of the else if and else blocks are skipped. If condition1 is false, it checks the next else if condition, and so on. f none of the preceding if or else if conditions are true, the code inside the else block is executed. The else block is optional and provides a default action when no other condition is met.
Example:

Output:
Grade: B
Nested if Statement:
A nested if statement in C means placing one if statement inside another. It allows us to test multiple conditions in a hierarchical way, where the inner condition is checked only if the outer condition is true.
Syntax:

Explanation:
- The outer if (condition1) is evaluated first.
- If condition1 is true, the code block within the outer if statement is executed.
- Inside this block, the nested if (condition2) is then evaluated.
- If condition2 is also true, its corresponding code block is executed.
- If condition2 is false, the else block associated with the nested if (if present) is executed.
- If the initial condition1 is false, the else block of the outer if (if present) is executed, and the nested if statements are not evaluated at all.
Note: The inner if only executes if the outer if is true.
Example:

Output:
Old enough to vote.
And you are a citizen, so you can vote!
Switch Statement
The switch statement in C is a multi-way decision-making statement that allows a program to execute different blocks of code based on the value of a single variable or expression. It serves as an alternative to a series of if-else if statements, particularly when dealing with multiple conditions based on the same variable.
The switch statement tests the value of the expression or variable against a list of case value and when a match is found, a block of statements associated with that case is executed. If no match is found, it optionally executes the default case.
Syntax:

Explanation:
switch(expression): The expression inside the parentheses is evaluated. This expression usually an integer or character. Evaluated once.
case: matches expression with a constant value. No duplicates allowed. The value of the expression is compared against the value associated with each case. If a match is found, the code block following that case is executed.
break: The break statement is crucial within a switch statement. When encountered, it immediately terminates the execution of the switch block. if not used, execution “falls through” to the next case.
default: The default case is optional. If no case matches the value of the expression, the code block under the default label is executed.
Example:

Output-1:
Enter two numbers: 5 12
Choose an operation:
1. Addition
2. Subtraction
3. Multiplication
4. Division
3
Result=60
Output-2:
Enter two numbers: 4 6
Choose an operation:
1. Addition
2. Subtraction
3. Multiplication
4. Division
6
Invalid choice!
Looping (C Loops):
Looping is a technique in which a set of statements are repeatedly executed until the condition specified becomes false. So looping is based on a condition. A loop is a repeated (iterated) sequence of statements. The repetition continues while a condition is true. When condition becomes false, the loop ends.
Categories of Loops:
There are three types of loops available in C:
- For loop
- While loop
- Do…While loop
Loops can be classified as Entry Controlled loop and Exit Controlled loop. The For Loop and While Loop entry-controlled loops. In case of entry controlled loop, the condition is tested first. The statements will be executed only if the condition specified is true.
In case of an exit controlled loops, the loop statements are executed first, then the condition will be tested. If the condition is true, again the statements will be executed until the condition becomes false. The loop Do…while is exit controlled loop.
For Loop:
It is an entry-controlled loop. A for loop in C is used to execute a block of code repeatedly for a specific number of times. It consists of three parts: initialization, condition, and increment/decrement.
Syntax:
for (initialization; test condition; modifier expression) {
body of the loop;
}
Example:

Output:
1 2 3 4 5 6 7 8 9 10
While Loop:
The simplest of all the looping statements in C is the while loop. It is an Entry Controlled loop. In while loop, the condition is checked before executing the body of the loop. If condition is true only then the body of the loop is executed otherwise not.
A while loop repeatedly executes a block of code as long as a specified condition remains true. The loop continues to iterate until the condition becomes false.
Syntax:
while (test condition) {
body of the loop
}
Example:

Output:
1 2 3 4 5 6 7 8 9 10
Do-while Loop:
It is similar to the while loop except that it tests the condition at the end of the loop body. It is an exit-controlled loop statement.
In do…while loop, the body of the loop is always executed at least once. After the body is executed, then it checks the condition. If the condition is true, then it will again execute the body of the loop otherwise control is transferred out of the loop. In do…while loop, the condition is always checked after the body of the loop.
Syntax:
do {
body of the loop
}
while (condition);
Example:

Output:
1 2 3 4 5
Break and Continue Statement
In C programming, break and continue are control flow statements used within loops to alter their normal execution.
Break Statement:
The break statement in C is a jump statement used to terminate the loop or switch statement immediately. When break statement is encountered, loop is terminated and control is transferred to the statement immediately after loop. It is commonly used when a specific condition is met within a loop, and further iterations are no longer required. The break statement is usually associated with if statement.
Syntax:
break;
Example:

Output:
1 2 3 4 5
Continue Statement:
The continue statement is used to skip the current iteration of a loop and proceed to the next iteration. When continue is encountered, the remaining statements in the current iteration of the loop are skipped, and the loop proceeds to its next iteration. The continue statement can be used only inside a looping statement. It is usually associated with if statement. The continue statement is commonly used when certain conditions within a loop require skipping some processing for the current iteration.
Syntax:
continue;
Example-1:

Output:
1 2 3 4 6 7 8 9 10
Example-2

Output:
1 2 3 4 5 6 7 8 9 10 20 21 22 23 24 25 26 27 28 29 30
Nested Loop in C
Nested loops are loops inside another loop. A nested loop consists of an “outer loop” and one or more “inner loops”. The inner loop is entirely contained within the outer loop’s body.
For each single iteration of the outer loop, the inner loop completes all of its iterations. This process repeats until the outer loop finishes all its iterations.
Any type of loop in C ( for, while, or do-while) can be nested within another loop of any type. For example, a for loop can contain a while loop, or a while loop can contain another while loop.
Example of a nested for loop:

Output:
* * * *
* * * *
* * * *
Arrays in C:
An array is a collection of elements of similar data type. Array is useful when a group of elements are to be represented by a common name. An array is a collection of homogeneous data.
Array is used to store a group of data items that belong to the same data type. An array is a group of elements that share a common name, and is differentiated from one another by their positions within the array.
Types of Arrays
One Dimensional Array:
A one-dimensional array in C is a linear collection of elements of the same data type, stored in contiguous memory locations. Each element is accessed using an index, starting from 0.
Syntax:
dataType arrayName[arraySize];
Example:
int marks[5];
declares an array named marks that can hold 5 integer values.
Programming Example:

Output:
Array elements are: 10 20 30 40 50
Two Dimensional Arrays:
A two-dimensional (2D) array in C programming is essentially an array of arrays, often visualized as a matrix with rows and columns. Two-dimensional arrays represent data in rows and columns, similar to a matrix in which each element is specified by two subscripts. Elements in two dimensional arrays commonly referred to by x[i][j] where ‘i’ is the row number and ‘j’ is the column number.
Syntax:
data_type array_name[rows][columns];
Example:
int matrix[2][3] = {{1,2,3}, {4,5,6}};
Programming Example:

Output:
All elements of the matrix:
1 2 3 4
5 6 7 8
9 8 7 6
Structure
In C programming, a structure is a collection of different data items which are referenced by single name. A structure is a collection of heterogeneous data. That is, a structure is a set of data items belonging to different data types. It is a group of variables of different data types referenced by a single name.
Defining a Structure:
We define a structure using the struct keyword, followed by a tag name (the structure’s name) and a block of curly braces containing the member variables.
struct Student {
char name[50];
int rollNo;
float marks;
};
Here,
struct: Keyword to define a structure.
Student: The tag name (name of the structure).
name, rollNo, marks: These are the members of the structure, representing different attributes of a student. They can be of various data types.
Declaring Structure Variables:
Once a structure is defined, we can declare variables of that structure type.
struct Student s1; // Declares a structure variable named s1
struct Student s2, s3; // Declares multiple structure variables
Initializing Structures:
We can initialize structure variables at the time of declaration using an initializer list or by assigning values to individual members later.
// Initializing at declaration
struct Student s1 = {“Raja”, 102, 92.5};
// Initializing later
struct Student s2;
strcpy(s2.name, “Lovely”);
s2.roll_number = 101;
s2.marks= 87
Accessing Structure Members:
We access individual members of a structure variable using the dot operator (.).
printf(“Name: %s”, s1.name);
printf(“\nRoll Number: %d”, s1.rollNo);
printf(“\Marks: %f”, s1.marks);
Example – 1:

Output:
Student 1:
————————-
Name: Daisy
Roll Number: 101
Marks: 88.50
Student 2:
———————–
Name: Nayna
Roll Number: 102
Marks: 92.00
Example – 2:

Output:
Enter details for student 1:
Name: Archita
Age: 19
Gender(M/F): F
Height(in ft.): 5.3
Enter details for student 2:
Name: Borokha
Age: 18
Gender(M/F): F
Height(in ft.): 5.2
Enter details for student 3:
Name: Priya
Age: 18
Gender(M/F): F
Height(in ft.): 5.4
Enter details for student 4:
Name: Daisy
Age: 19
Gender(M/F): F
Height(in ft.): 5.3
Enter details for student 5:
Name: Roktim
Age: 17
Gender(M/F): M
Height(in ft.): 5.5
Student Information:
Student 1:
Name: Archita
Age: 19
Gender: F
Height: 5.300000
Student 2:
Name: Borokha
Age: 18
Gender: F
Height: 5.200000
Student 3:
Name: Priya
Age: 18
Gender: F
Height: 5.400000
Student 4:
Name: Daisy
Age: 19
Gender: F
Height: 5.300000
Student 5:
Name: Roktim
Age: 17
Gender: M
Height: 5.500000
Functions
A function is a block of codes or a group of statements that together perform a specific task. Every C program must have at least one function, which is the main() function. We can divide our code into separate functions. A program can have any number of functions. The main() function is the starting point of a program.
Type of Function:
Functions can be broadly classified into two major categories,
- Standard library functions or Built-in Functions
- User-defined functions
Standard Library Function or Built-in Functions:
Standard library functions or simply library functions are built-in functions. There functions are already defined in the C compilers. These are system defined functions. These functions available in C libraries, such as the printf(), scanf(), sqrt() etc. They are used by including the relevant header file (e.g., <stdio.h>, <math.h>). C provides a rich set of standard library functions and built-in functions for various tasks.
Example:

Output:
The square root of 25 is 5
2 raised to the power of 4 is 16
User Defined Functions:
User-defined functions created by the programmer to perform specific tasks according to their program’s needs. User defined functions are defined by the user. C allows us to create functions according to our need. If there is no appropriate library function for specific requirement, we can create our own function to perform the desired operation. Once a function is defined, we can easily call and use the function whenever it requires.
Example-1:

Output:
Hello Students …
Welcome to C Programming Class.
Example-2:

Output:
Enter 2 numbers: 23 12
Sum: 35
Pointer in C
In C programming, a pointer is a special type of variable that stores the memory address of another variable. It is called pointer because it points to a particular location in memory by storing address of that location. Unlike regular variables that hold data values directly, a pointer holds the location where a specific data value is stored in memory.
Declaring a Pointer:
Like variables, pointers in C language have to be declared before they can be used in program. A pointer variable is declared by placing an asterisk (*) before its name. The type of the pointer must match the type of the variable it intends to point to.
Syntax:
datatype *pointer_name;
Example:
int *ptr;
Address-of Operator (&):
The address-of operator (&) is used to retrieve the memory address of a variable.
Example:
int num = 10;
int *ptr = # // ‘ptr’ stores the address of ‘num’
Dereference Operator (*):
The dereference operator (*), also known as the indirection operator, is used to access the value stored at the memory address pointed to by a pointer.
Programming Example:

Output:
Value of num: 10
Address of num: 000000000062FE44
Value of num via ptr=10