*Iteration*
Iteration in C programming is the process of repeatedly executing a block of code as long as a
specified condition is true. It allows efficient repetition of tasks without writing the same code
multiple times. C supports three types of loops for iteration: for loop, while loop, and do-while loop,
each suited for different scenarios of repetition.
● It automates repeated execution of code based on conditions or fixed counts.
● Three main loops are used: for loop (known iterations), while loop (condition-based), and
do-while loop (executes at least once).
● Iteration is essential for tasks like traversing arrays, processing data repeatedly, and
simplifying complex problems into manageable steps.
*Loops*
Loops in C are essential for executing code repeatedly, making programs more efficient and reducing
redundancy. The three main types are the for loop, used for a known number of iterations; the while
loop, which executes as long as a condition holds true; and the do-while loop, which executes at
least once before checking the condition.
● The for loop is used when the number of iterations is known beforehand, with syntax
including initialization, condition, and update expressions.
● The while loop executes repeatedly as long as its condition remains true, evaluated before
each iteration.
● The do-while loop runs at least once and then continues executing as long as the condition
evaluates to true, with the condition checked after the execution of the loop body.
*Uses of while loop*
The while loop in C is used to repeatedly execute a block of code as long as a specified condition
remains true. It is called an entry-controlled loop because the condition is checked before the loop
body is executed, which means if the condition is false initially, the loop body won’t run at all. This
loop is useful when the number of iterations is not known in advance and depends on dynamic
conditions.
● To execute a block of code repeatedly until a certain condition is met, such as reading input
until a valid value is entered.
● To perform iteration over elements when the number of iterations is uncertain, like
processing elements from a data stream.
● To implement countdowns, accumulations, or repetitive calculations that continue until a
condition changes outside a fixed range.
*Do While loop*
The do-while loop in C is a variant of the while loop where the loop body is executed first before the
condition is tested. This ensures that the loop executes at least once, regardless of the condition.
The syntax is:
do {
// code block to be executed
} while (condition);
Key Points:
● The loop body runs once before the condition is checked.
● The loop continues executing as long as the condition evaluates to true.
● Useful for scenarios where the code must run at least once, such as menu-driven programs
or input validation.
Example:
int i = 0;
do {
printf("%d\n", i);
i++;
} while (i < 5);
This prints numbers 0 to 4.
The do-while loop is called an exit-controlled loop because the condition is evaluated after the loop
execution. It is commonly used when at least one iteration is required regardless of the condition.
*For Loop*
The for loop in C is a control structure used to execute a block of code repeatedly for a known
number of times. It combines initialization, condition checking, and update in a single line, making it
concise and easy to manage. The loop continues as long as the condition remains true.
For Loop Syntax:
for (initialization; condition; update) {
// code to be executed
How it works:
● Initialization runs once before the loop starts.
● The condition is checked before each iteration; if false, the loop exits.
● The code inside the loop executes if the condition is true.
● The update statement executes after each iteration.
Example:
#include <stdio.h>
int main() {
for (int i = 0; i < 5; i++) {
printf("%d\n", i);
return 0;
*Multiple Loop*
In C, multiple loops refer to using loops inside other loops, known as nested loops. These nested
loops allow the execution of a loop inside the body of another loop, enabling complex iterations like
working with multi-dimensional arrays or generating patterns.
Types of Nested Loops in C:
● Nested for loop: A for loop inside another for loop, often used for iterating over multi-
dimensional data.
● Nested while loop: A while loop inside another while loop, useful for more flexible looping
conditions.
● Nested do-while loop: A do-while loop inside another do-while loop, ensuring each loop
executes at least once.
Example of Nested for Loop in C:
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
printf("%d ", i * j);
printf("\n");
Output:
text
123
246
369
Nested loops are commonly used for tasks like multiplying tables, traversing multi-dimensional
arrays, and generating complex patterns. The inner loop completes all its iterations for each iteration
of the outer loop, resulting in a total number of iterations equal to the product of the individual
loops' iterations
*Variables in c*
Variables in C are named containers used to store data values during the execution of a program.
Each variable has a specific data type that determines the kind of data it can hold, such as integers,
floating-point numbers, or characters. Variables must be declared before use, specifying their type
and optionally initializing them with a value.
Key Points about Variables in C:
● Variables are declared using a data type followed by a variable name, for example: int
age; or float salary = 2500.50;.
● Common data types include int (integer numbers), float (decimal numbers), char (single
characters), and double (double precision floating-point numbers).
● Variable names must start with a letter or underscore, and cannot be keywords or contain
spaces; meaningful names improve code readability.
Variables are essential for storing inputs, intermediate results, and outputs in programming,
enabling dynamic and flexible computation.
*Uses of break*
The break statement in C is used to immediately exit a loop or switch case, regardless of the loop's
original termination condition. When encountered inside a loop (for, while, or do-while), it stops the
loop execution instantly and transfers control to the statement following the loop. This is useful for
prematurely ending loops based on a specific condition.
Uses of break in C:
● To exit from a loop when a particular condition is met, such as stopping a search once the
target is found.
● To prevent infinite loops by breaking out based on a dynamic condition inside while or do-
while loops.
● To terminate cases in a switch statement to avoid fall-through from one case to the next.
Example using break in a for loop:
for (int i = 0; i < 10; i++) {
if (i == 5) {
break;
printf("%d\n", i);
*Goto and Continue statements*
The goto and continue statements in C are used for controlling the flow of program execution in
different ways.
goto Statement:
● The goto statement allows unconditional jumping to a labeled part of the program within
the same function.
● It is useful for error handling, exiting from multiple nested loops, or skipping certain code
sections.
● Syntax:
goto label;
...
label: statement;
● Example:
int i = 0;
start:
printf("%d\n", i);
i++;
if (i < 5)
goto start;
● Use of goto can lead to unclear, hard-to-read code, so it is generally used sparingly.
continue Statement:
● The continue statement skips the current iteration of a loop and immediately proceeds to
the next iteration.
● It is used within loops like for, while, or do-while.
● Syntax:
c
continue;
● Example:
for (int i = 1; i <= 5; i++) {
if (i == 3)
continue; // skip printing 3
printf("%d\n", i);
*Array in c*
An array in C is a collection of elements of the same data type stored in contiguous memory
locations. It allows storing multiple values in a single variable, accessed by their index positions.
Arrays enable efficient data management when dealing with lists or sequences of values.
Key Points about Arrays in C:
● Declared using the syntax: data_type array_name[array_size]; where array_size is a fixed
constant.
● Array elements are accessed using zero-based indices. For example, arr[0] accesses the first
element.
● Arrays store elements of the same type, such as integers, floats, or characters, and the size
must be defined during declaration.
Example:
int numbers[5] = {10, 20, 30, 40, 50};
printf("%d", numbers[2]); // Outputs 30
Array Notation and Representation in c:
Array notation and representation in C refer to how arrays are declared, indexed, and stored in
memory.
Array Notation:
● Arrays in C are declared using the syntax:
\text{data_type array_name[array_size];e];}
where data_type is the type of elements, and array_size is the number of elements.
● Elements are accessed by indices enclosed in square brackets [], starting at index 0.
● Example:
∫ numbers={10 ,20 ,30 , 40 , 50 }; [11]
Accessing the third element: numbers[2] gives 30.
*Array Representation*
● Arrays are stored in contiguous memory locations, meaning elements are placed one after
another in memory.
● The address of the first element (array_name) is the base address, and other elements are
located by adding the index offset multiplied by the size of the data type.
● For example, if the base address of numbers is 1000 and an int occupies 4
bytes, numbers[3] is at address 1000+3 × 4=1012.
● This contiguous arrangement enables efficient access and manipulation by calculating
addresses directly.
Manipulating array elements in C involves accessing, updating, traversing, and processing the
individual elements of an array using indices and loops.
Ways to Manipulate Array Elements:
● Accessing Elements: Use the array name with an index in square brackets to retrieve a
value, e.g., arr[2].
● Updating Elements: Assign new values to array elements by specifying the index, e.g., arr[0]
= 10;.
● Traversing Arrays: Use loops (for, while) to iterate through each element for reading or
modification.
● Performing Operations: Apply calculations or logic to elements while traversing, like
summing, searching, or sorting.
Example of Manipulating Array Elements:
#include <stdio.h>
int main() {
int arr[5] = {2, 4, 8, 12, 16};
// Updating first element
arr[0] = 1;
// Traversing array to print all elements
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
printf("\n");
// Printing array elements in reverse
for (int i = 4; i >= 0; i--) {
printf("%d ", arr[i]);
printf("\n");
return 0;
*Multi Dimensional Array*
n C, multidimensional arrays are arrays of arrays, allowing storage of data in multiple dimensions
such as two-dimensional (2D) or three-dimensional (3D) arrays. They are useful for representing
tabular data, matrices, or more complex data structures.
Using Multidimensional Arrays in C:
Declaration:
data_type array_name[size1][size2]...[sizeN];
● For example, a 2D integer array with 2 rows and 3 columns:
int matrix[2][3] = { {1, 4, 2}, {3, 6, 8} };
Accessing Elements:
● Access elements using multiple indices, like matrix[row][column].
● Example: matrix[0][2] accesses the element in the first row and third column.
Traversing Multidimensional Arrays:
● Use nested loops, one for each dimension, to iterate through all elements.
● Example for 2D arrays:
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", matrix[i][j]);
printf("\n");
Example - 2D Array:
#include <stdio.h>
int main() {
int arr[2][2] = { {10, 20}, {30, 40} };
printf("2D Array Elements:\n");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
printf("%d ", arr[i][j]);
printf("\n");
return 0;
*Character Array in C*
A character array in C is an array where each element is of type char, typically used to store
sequences of characters or strings. Unlike general arrays that hold numbers or other data types, a
character array stores characters and is often null-terminated to mark the end of the string.
Key Points about Character Arrays in C:
● Declared like: char arr[size]; where size is the number of characters the array can hold.
● Initialized using string literals, e.g., char str[] = "Hello"; automatically adds a null terminator \
0 at the end.
● Can be initialized with individual characters using braces: char str[] = {'H', 'e', 'l', 'l', 'o', '\0'};
● The null character \0 is essential as it marks the end of the string in memory.
● Elements are accessed using indices like arr[0] for the first character.
Example:
c
#include <stdio.h>
int main() {
char str[] = "Learn C";
printf("%s\n", str); // Prints the whole string
printf("First character: %c\n", str[0]); // Prints 'L'
return 0;
*String in c*
In C, a string is represented as an array of characters terminated by a null character \0. Strings in C
are not a separate data type but handled as character arrays, allowing manipulation through the
array elements.
Key Points about Strings in C:
● A string is a sequence of characters stored in contiguous memory locations ended by a null
terminator \0.
● Strings are declared as character arrays. For example, char str[] = "Hello"; automatically
includes the null terminator.
● String handling functions in the <string.h> header, such as strlen(), strcpy(), strcat(),
and strcmp(), provide convenient ways to manipulate strings:
● strlen() returns the length excluding the null character.
● strcpy() copies one string to another.
● strcat() concatenates two strings.
● strcmp() compares two strings lexicographically.
Example:
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello";
char str2[20];
strcpy(str2, str1); // Copy str1 to str2
strcat(str2, " World"); // Concatenate string to str2
printf("%s\n", str2); // Output: Hello World
printf("Length: %lu\n", strlen(str2)); // Output: Length: 11
return 0;
*Structure in c*
A structure in C is a user-defined data type that allows grouping variables of different types under a
single name. It lets you combine related data items of various types (such as int, float, char) into one
unit, which is useful for representing complex objects like a student, book, or employee.
Key Points:
● Defined using the struct keyword followed by the structure name and member declarations
inside curly braces.
● Members can be of different data types.
● Accessed using the dot operator (.) with structure variables.
● Memory for structure variables is allocated only when declared.
● Structures help organize related data easily and improve code readability.
Syntax:
struct StructureName {
data_type member1;
data_type member2;
// more members
};
Example:
#include <stdio.h>
struct Person {
char name[50];
int age;
float salary;
};
int main() {
struct Person p1;
// Assign values
[Link] = 30;
[Link] = 45000.50;
strcpy([Link], "John Doe");
// Access members
printf("Name: %s\n", [Link]);
printf("Age: %d\n", [Link]);
printf("Salary: %.2f\n", [Link]);
return 0;
*Union in c*
A union in C is a user-defined data type similar to a structure, but with a key difference: all members
of a union share the same memory location. This means that at any given time, a union can store a
value for only one of its members, making it memory efficient when you need to work with different
data types but only one value at a time.
Key Points about Union in C:
● Declared using the union keyword followed by member declarations.
● All members share the same memory, so the size of the union is equal to the size of its
largest member.
● Only one member can hold a meaningful value at any point.
● Access members using the dot (.) operator.
● It is useful in memory optimization and for storing different types of data in the same
location sequentially.
Syntax:
union UnionName {
data_type member1;
data_type member2;
// more members
};
Example:
#include <stdio.h>
union Data {
int i;
float f;
char str[20];
};
int main() {
union Data data;
data.i = 10;
printf("data.i: %d\n", data.i);
data.f = 220.5;
printf("data.f: %.2f\n", data.f);
strcpy([Link], "C Programming");
printf("[Link]: %s\n", [Link]);
return 0;
*Enumeration in c*
An enumerated data type (enum) in C is a user-defined data type that consists of a set of named
integer constants. It allows assigning meaningful names to integral values, improving code
readability and maintainability. Enums represent a collection of related constants under a single type
name.
Key Points about Enumerated Data Type in C:
● Defined using the enum keyword followed by a name and a list of named constants inside
curly braces.
● By default, the first named constant is assigned the value 0, and subsequent constants are
incremented by 1.
● You can manually assign specific values to the constants if needed.
● Enums improve code clarity by replacing numeric constants with descriptive names.
Syntax:
enum EnumName {
constant1,
constant2,
constant3,
...
};
Example:
enum week { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
int main() {
enum week today;
today = Wednesday;
printf("Day number: %d\n", today); // Output: Day number: 3
return 0;
Note:
● Enum members follow standard identifier naming rules.
● Enum variables can store only the values defined in the enumeration.
Enums are widely used in situations like representing days of the week, states of a process, or
options in menus to make the code more understandable and manageable.
*Array of Structure*
An array of structures in C is a collection where each element is a structure of the same type. It
allows you to store multiple instances of a structure in a single array, making it easy to organize and
manage related data records.
Key Points:
● Declared like an ordinary array but with structure type, e.g., struct Student
students[10]; creates an array of 10 Student structures.
● Each element in the array is a distinct structure, so you can access members using array
indexing and dot operator, e.g., students[0].name.
● Useful for storing lists of entities like students, employees, or products with multiple
attributes.
Example:
#include <stdio.h>
#include <string.h>
struct Student {
char name[50];
int age;
float marks;
};
int main() {
struct Student students[3];
// Initializing values
strcpy(students[0].name, "Nikhil");
students[0].age = 20;
students[0].marks = 85.5;
strcpy(students[1].name, "Shubham");
students[1].age = 22;
students[1].marks = 90.0;
strcpy(students[2].name, "Vivek");
students[2].age = 25;
students[2].marks = 78.0;
// Printing student details
for (int i = 0; i < 3; i++) {
printf("Student %d:\n", i + 1);
printf("Name: %s\n", students[i].name);
printf("Age: %d\n", students[i].age);
printf("Marks: %.2f\n\n", students[i].marks);
return 0;
This prints each student's details stored in the array of structures.
Array of structures helps in managing complex data sets cleanly and efficiently by grouping relevant
data in one unit and then using arrays for multiple records.
*Passing Array in C*
In C, passing an array to a function allows the function to access and modify the original array
elements. Arrays are passed by reference effectively because when you pass an array, what is
actually passed is the pointer to the first element of the array, not the entire array copy.
How to Pass Arrays in C:
● Pass an array by specifying the array name in the function call.
● The function parameter is declared as an array without size, or as a pointer, e.g., void
func(int arr[]) or void func(int *arr).
● It is common to pass the size of the array as an additional argument to know the number of
elements.
Example:
#include <stdio.h>
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
printf("\n");
int main() {
int numbers[] = {1, 2, 3, 4, 5};
printArray(numbers, 5);
return 0;
Output:
text
12345
Important Notes:
● Changes made to the array inside the function affect the original array since it operates on
the original memory.
● Passing multidimensional arrays requires specifying the size of all dimensions except the
first.
● Arrays can also be passed to functions as pointers, and both declarations are functionally
equivalent.
Passing arrays in C facilitates modular programming by allowing functions to process large data sets
without copying them
*Passing array to Function in C*
Passing an array to a function in C enables the function to access and manipulate the elements of
the original array. In C, arrays are passed to functions by passing a pointer to the first element of the
array, rather than copying the entire array. This mechanism is known as passing by reference.
How to Pass an Array to a Function:
● Pass the array name (which acts as a pointer to the first element) as an argument.
● Declare the function parameter as an array without specifying the size or as a pointer.
● It is common to pass the size of the array as a separate parameter so the function knows
how many elements to process.
Syntax:
void functionName(int arr[], int size);
or
void functionName(int *arr, int size);
Example:
#include <stdio.h>
// Function to print elements of array
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
printf("\n");
int main() {
int numbers[] = {1, 2, 3, 4, 5};
printArray(numbers, 5); // Passing array and size to function
return 0;
Output:
text
12345
Important Notes:
● Modifications made to array elements inside the function affect the original array.
● Passing arrays like this avoids copying the entire array, making it efficient.
● For multidimensional arrays, sizes of all dimensions except the first must be specified in the
function parameter.
This method is widely used in C to handle arrays efficiently by passing just the reference (address) to
the function.