Storing the marks of 100 students in 100 separate variables is unmanageable: you cannot loop over variables named m1, m2, … m100. An array solves this by giving one name to a whole row of values. This section explains what an array is and how to declare one.
What an array is
Key properties of an array:
Homogeneous: every element has the same type.
Contiguous: the elements sit next to each other in memory.
Indexed from 0: an array of n elements has indices 0 to n − 1.
Fixed size: the size is set when the array is created and cannot grow.
Random access: any element can be reached directly by its index.
Declaring an array
Syntax
data-type array-name[size];
Declaration
Meaning
Memory on a typical 64-bit system
int marks[100];
100 integers, marks[0] to marks[99]
100 × 4 = 400 bytes
float temp[7];
7 floats
7 × 4 = 28 bytes
char name[40];
40 characters
40 × 1 = 40 bytes
The memory needed is
bytes=number of elements×size of one element
and sizeof reports it: sizeof marks is 400, and sizeof marks / sizeof marks[0] gives the number of elements, 100.
Size specification with a symbolic constant
Define the size once with #define and use the name everywhere: in the declaration and in every loop. To change the size later, you edit one line.
Key takeaways
An array stores many values of one type under one name, in consecutive memory.
type name[size]; creates elements with indices 0 to size − 1.
Memory = number of elements × size of one element.
Use a #define constant for the size.
Practice questions
Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 6 · short answer
Explain two advantages of using #define SIZE 50 with int marks[SIZE]; instead of writing int marks[50];.
Show worked answer
First, readability: the name SIZE tells the reader what the number means wherever it appears, such as in for (i = 0; i < SIZE; i++). Second, maintainability: the array size is usually needed in several loops as well as the declaration; with a symbolic constant, changing the class size from 50 to 60 means editing one line, and the preprocessor updates every use, so no loop is left with an outdated limit.
MCA Online notes are made for reading on screen. Open https://mcastudy.online/mcs-201/arrays-and-strings/introduction-to-arrays-and-array-declaration/ to read this page.