













Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!














An array is a group of elements of the same data type stored in continuous memory locations. All elements are accessed using a single name with an index.
When we need to store many values of the same type, instead of creating many variables, we can use one array.
Example: Instead of writing:
int a1, a2, a3, ..., a50;
We can write:
int a[50];
Stores similar (homogeneous) data Uses one variable name Each element has a position called index Index starts from 0 in C Elements are stored in continuous memory Size of array is fixed
Has only one index Data is stored in a single line (list form)
Example:
int a[5];
Has more than one index Used to store data in rows and columns
a) Two-Dimensional Array
Most common multi-dimensional array Looks like a table or matrix
Example:
int a[3][3];
b) n-Dimensional Array
Array with more than two dimensions Used in advanced applications
A one-dimensional array is a collection of elements of the same data type stored in continuous memory locations. It is also called a single-dimensional array because it uses only one index to access elements.
A one-dimensional array works like a list of values. Each value in the array is called an element , and every element has a position called index. The index always starts from 0 , so for an array of size n , the index ranges from 0 to n-. This helps in accessing each element easily using its position.
For an array of size 5:
Index: 0 1 2 3 4 Array: [ ] [ ] [ ] [ ] [ ]
int A[5] = {10, 12, 14, 16, 18}; char name[10] = "Computer";
Here, values are assigned one by one using their index positions. This method is useful when values are assigned during program execution.
int a[5]; a[0] = 10; a[1] = 12; a[2] = 14; a[3] = 16; a[4] = 18;
In this case, the size of the array is not specified. The compiler automatically determines the size based on the number of elements given.
int val[] = {2, 4, 5}; char s[] = "FIRST";
Accessing Elements
Array elements are accessed using their index number. For example, a[2] refers to the third element of the array. Using index makes it easy to read, update, and process elements.
Reading and Printing Elements
A loop is used to take input for each element one by one.
for(i = 0; i < n; i++) {
scanf("%d", &a[i]); }
A loop is used to display all elements of the array.
for(i = 0; i < n; i++) { printf("%d", a[i]); }
Linear search (Sequential search) is a simple method used to find an element in a list. It checks each element one by one from the beginning until the element is found or the list ends.
Start from the first element (index 0) Compare each element with the search key If a match is found → stop searching If not found → move to the next element Continue until: Element is found, or All elements are checked
Search element = 33
List: 10 14 19 26 27 31 33 35 42 44
f o r ( i = 0 ; i < n ; i + + ) { i f ( a [ i ] = = n u m ) { p r i n t f ( " F o u n d a t p o s i t i o n % d " , i + 1 ) ; f o u n d = 1 ; b r e a k ; } }
i f ( f o u n d = = 0 ) { p r i n t f ( " N o t f o u n d " ) ; } r e t u r n 0 ; }
Bubble Sort is a simple sorting method that works by comparing and swapping adjacent elements if they are in the wrong order.
Compare two neighbouring elements If the first is greater , swap them Repeat this process again and again After each round, the largest element moves to the end Continue until the array is fully sorted
👉 Just like bubbles rise to the top, the largest numbers move to the end
Unsorted array:
14 33 27 35 10
Compare 14 & 33 → OK Compare 33 & 27 → swap
Compare 33 & 35 → OK Compare 35 & 10 → swap
Result:
14 27 33 10 35
✅ Array is sorted
Algorithm (Steps)
PROGRAM:
i n t m a i n ( ) { i n t a [ 1 0 ] , n , i , j , t e m p ;
p r i n t f ( " E n t e r s i z e : " ) ; s c a n f ( " % d " , & n ) ;
i t f ( " E t l t " )
Representation
Row 0 | [0][0] [0][1] [0][2] Row 1 | [1][0] [1][1] [1][2] Row 2 | [2][0] [2][1] [2][2]
Declaration of 2D Array
Syntax:
datatype array_name[rows][columns];
Examples:
int arr[5][5]; char str[10][50];
Number of Elements
👉 Total elements = rows × columns
Example:
int A[20][10]; Total elements = 20 × 10 = 200
Initialization of 2D Array
int cube[5][2] = {1,1, 2,8, 3,27, 4,64, 5,125};
int cube[5][2] = { {1,1}, {2,8}, {3,27},
Each element is accessed using row and column index.
Example:
x[3][1]
👉 Means:
4th row (because index starts from 0) 2nd column
for(i = 0; i < m; i++) { for(j = 0; j < n; j++) { scanf("%d", &a[i][j]); } }
for(i = 0; i < m; i++) { for(j = 0; j < n; j++) { printf("%d ", a[i][j]); } }
A string in C is a one-dimensional array of characters that ends with a special character called null character ('\0').
1. strcpy()
👉 Copies one string into another
strcpy(dest, src);
2. strlen()
👉 Finds length of string
len = strlen(str);
3. strcat()
👉 Joins two strings
strcat(str1, str2);
4. strcmp()
👉 Compares two strings
Returns 0 → equal <0 → first smaller >0 → first greater
strcmp(str1, str2);
5. strrev()
👉 Reverses a string
strrev(str);
6. strchr()
👉 Finds first occurrence of a character
strchr(str, 'a');
7. strstr()
👉 Finds substring in a string
strstr(str1, str2);
👉 Converts string to lowercase
strlwr(str);
👉 Converts string to uppercase
strupr(str);
👉 Reads full string (including spaces)
gets(str);
⚠ Not safe in modern C, but used in exams.
👉 Prints string with newline
puts(str);
An enum (enumeration) is a user-defined data type in C. It is used to define a set of named integer constants. Instead of using numbers directly (like 0,1,2), we use meaningful names (like Sunday, Monday).
Wednesday = 3 ...
Assigning custom values
We can also assign our own values:
enum week { Sunday=1, Monday, Tuesday=21, Wednesday, Thursday=11, Friday, Saturday };
Sunday = 1 Monday = 2 Tuesday = 21 Wednesday = 22 Thursday = 11 Friday = 12 Saturday = 13
👉 Rule: If a value is not given, it takes previous value + 1
Simple Program Example
#include
enum week { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
int main() { enum week d;
d = Wednesday;
printf("Value = %d", d);
return 0; }
Output
Value = 3
Enum with switch case
Enum values are often used in switch statements :
e n u m w e e k { S u n d a y , M o n d a y , T u e s d a y , W e d n e s d a y , T h u r s d a y , F r i d a y , S a t u r d a y } ;
i n t m a i n ( ) { e n u m w e e k d ; d = M o n d a y ;
s w i t c h ( d ) { c a s e S u n d a y : p r i n t f ( " S u n d a y " ) ; b r e a k ; c a s e M o n d a y : p r i n t f ( " M o n d a y " ) ; b r e a k ; c a s e T u e s d a y : p r i n t f ( " T u e s d a y " ) ; b r e a k ; }
r e t u r n 0 ; }
Important Points
Enum is treated as an integer type Default starting value is 0 Values increase automatically by 1
typedef int Integer; → gives new name Integer to int So instead of writing int n, we can write Integer n Both mean the same thing
Makes program easy to read Reduces typing long data types Improves code clarity Very useful in structures and pointers
Enum is used to define a set of named constants. It is useful when a variable can take only a fixed set of values.
Enums are used to represent different states of a system:
ON / OFF Running / Stopped Active / Inactive
👉 Helps in tracking system conditions easily
Used to represent program status:
SUCCESS ERROR WARNING
👉 Makes error handling clear and readable
3. Menu-driven Programs
Used in programs where user selects options:
Add Delete Update
👉 Each option is given a name instead of number
4. Days, Months, Seasons
Used for fixed sets of values:
Days of week Months of year
👉 Example: Sunday, Monday, Tuesday...
5. Bitwise Flags / Permissions
Used in system-level programming:
File read / write / execute permissions Feature enable/disable
👉 Helps manage settings easily
6. Networking
Used to represent communication types:
TCP UDP
👉 Makes protocol handling simple
7. Game Development