At some point in programming, individual variables stop being enough.
Storing one number is easy. Storing a hundred? That’s when arrays arrive like neatly labeled shelves, and strings step in as civilized ways to handle text.
UNIT III is about learning how to organize, process, and manipulate collections of data efficiently. It’s where C begins to feel powerful rather than primitive.
1. Arrays: Storing More with Less Effort
An array is a collection of values of the same data type, stored in contiguous memory locations.
Instead of this:
int a, b, c, d, e;
int marks[5];
One name. Many values. Clean and efficient.
2. Declaring and Referencing Arrays
Declaration
int numbers[10];
This creates space for 10 integers.
Referencing Elements
Each element is accessed using an index (subscript), starting from 0.
numbers[0] = 10;
numbers[1] = 20;
Visual Layout
Index: 0 1 2 3 4
Value: 10 20 30 40 50
Indexing mistakes are common — and unforgiving. C trusts you completely.
3. Sequential Access Using for Loops
Arrays and loops are best friends.
for (int i = 0; i < 5; i++) {
printf("%d ", numbers[i]);
}
This pattern appears everywhere: reading input, processing data, computing totals, searching, sorting.
4. Using Array Elements as Function Arguments
You can pass individual elements:
printValue(arr[2]);
Or pass the entire array:
processArray(arr, size);
Inside the function, the array behaves like a pointer to its first element.
void processArray(int a[], int n) {
for (int i = 0; i < n; i++)
printf("%d ", a[i]);
}
Arrays are always passed by reference, meaning changes inside the function affect the original array.
5. Searching an Array
Linear Search
Check each element until the target is found.
for (int i = 0; i < n; i++) {
if (arr[i] == key) {
found = 1;
break;
}
}
Simple, reliable, and perfect for small datasets.
6. Sorting an Array
Sorting brings order to chaos.
Bubble Sort (Conceptual Favorite)
for (i = 0; i < n-1; i++) {
for (j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
swap(arr[j], arr[j+1]);
}
}
}
Though not the fastest, it’s excellent for understanding comparison-based sorting.
7. Parallel Arrays and Enumerated Types
Parallel Arrays
Used when related data must stay aligned.
int rollNo[3] = {1, 2, 3};
int marks[3] = {85, 90, 78};
Index i connects roll number with its marks.
Enumerated Types
Enums make code readable and meaningful.
enum Day {MON, TUE, WED, THU, FRI};
Now MON is clearer than 0.
8. Multidimensional Arrays
Multidimensional arrays represent tables, matrices, and grids.
int matrix[3][3];
Access
matrix[1][2] = 10;
Memory View
[ ][ ][ ]
[ ][ ][ ]
[ ][ ][ ]
Nested loops are essential here.
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
printf("%d ", matrix[i][j]);
Strings in C: Text with Discipline
Unlike many languages, C treats strings as character arrays ending with a special character: '\0'.
char name[10] = "C Language";
In memory:
C L a n g u a g e \0
The null character marks the end.
9. String Basics and Library Functions
C provides powerful string-handling tools via <string.h>.
Assignment and Copy
strcpy(dest, src);
Length
strlen(str);
Substring
Handled using pointers and functions like strncpy().
10. Concatenation and Whole-Line Input
Concatenation
strcat(str1, str2);
Whole-Line Input
Avoid unsafe functions like gets(). Prefer:
fgets(name, sizeof(name), stdin);
It respects boundaries — something C programmers deeply value.
11. String Comparison
strcmp(str1, str2);
Returns:
-
0if equal -
Negative if
str1 < str2 -
Positive if
str1 > str2
String comparison is lexical, not numerical.
12. Arrays of Pointers
Instead of a 2D character array:
char names[3][20];
You can use:
char *names[] = {"Alice", "Bob", "Charlie"};
Advantages:
-
Saves memory
-
Flexible string lengths
-
Faster access
This concept quietly prepares you for dynamic memory and advanced data structures.
Why UNIT III Matters
Arrays and strings teach data organization, not just syntax.
They help you think in collections, patterns, and structures — essential for real-world problem solving.
From student records to text processing, from matrices to menus, UNIT III concepts appear everywhere in C programming.
Master them well, and C stops feeling rigid. It starts feeling precise.
PART A: Multiple Choice Questions (MCQs)
1. Array indexing in C starts from
A. 1
B. 0
C. –1
D. Depends on compiler
Answer: B
2. Which of the following declares an integer array of size 20?
A. int a;
B. int a[20];
C. array int a[20];
D. int a(20);
Answer: B
3. The name of an array represents
A. Last element address
B. Address of the first element
C. Value of the first element
D. Size of the array
Answer: B
4. Which loop is most suitable for sequential access of arrays?
A. while
B. do-while
C. for
D. if
Answer: C
5. When an array is passed to a function, it is passed
A. By value
B. By reference
C. As a structure
D. As a constant
Answer: B
6. Which searching technique checks each element one by one?
A. Binary search
B. Linear search
C. Hash search
D. Tree search
Answer: B
7. Bubble sort works by
A. Dividing the array
B. Swapping adjacent elements
C. Selecting minimum elements
D. Using recursion
Answer: B
8. Parallel arrays are used to
A. Store multidimensional data
B. Store different types of unrelated data
C. Store related data using same index
D. Replace structures
Answer: C
9. Which keyword is used to define enumerated types?
A. enum
B. define
C. typedef
D. struct
Answer: A
10. A two-dimensional array is also called
A. Linear array
B. Pointer array
C. Matrix
D. Structure
Answer: C
11. Strings in C are terminated by
A. newline
B. space
C. null character ('\0')
D. tab
Answer: C
12. Which header file supports string functions?
A. stdio.h
B. math.h
C. string.h
D. stdlib.h
Answer: C
13. Which function compares two strings?
A. strcmp()
B. strcpy()
C. strcat()
D. strlen()
Answer: A
14. Which function is used to concatenate strings?
A. strcpy()
B. strcmp()
C. strcat()
D. strlen()
Answer: C
15. Which is safer for reading a full line of input?
A. gets()
B. scanf("%s")
C. fgets()
D. getchar()
Answer: C
16. Arrays of pointers are useful because they
A. Use more memory
B. Restrict string size
C. Allow variable-length strings
D. Eliminate loops
Answer: C
17. Which of the following is a valid string declaration?
A. char s = "Hello";
B. char s[6] = "Hello";
C. char s[5] = "Hello";
D. string s = "Hello";
Answer: B
18. Binary search requires the array to be
A. Large
B. Unsorted
C. Sorted
D. Reversed
Answer: C
19. Which operator is used to access elements of a multidimensional array?
A. *
B. .
C. []
D. ->
Answer: C
20. What does strlen() return?
A. Size of array
B. Length including \0
C. Number of characters excluding \0
D. Number of words
Answer: C
PART B: Short-Answer Questions (2–4 Marks)
Define an array.
Explain array subscripting with an example.
Why are for-loops preferred for array processing?
What happens when an array is passed to a function?
Write the syntax for declaring a two-dimensional array.
Explain linear search with an example.
What is bubble sort? Write its basic logic.
Define parallel arrays. Give one example.
What is an enumerated data type? Why is it useful?
Explain multidimensional arrays with a diagram.
What is a string in C? How is it stored in memory?
Why is
'\0'important in strings?Differentiate between strcpy() and strcat().
Why is gets() unsafe?
What are arrays of pointers? Mention one advantage.
PART C: Very Short Answers (1 Mark)
Default starting index of an array?
Header file for string functions?
Function to compare strings?
Search method that checks sequentially?
Keyword used for enumeration?
Character used to terminate strings?
Name one sorting technique.
Function to read whole line input safely?
- Get link
- X
- Other Apps
- Get link
- X
- Other Apps
Comments
Post a Comment