What is an Array in C Programming?
An array in C programming is a collection of similar data types stored at consecutive memory locations.
Example: int marks[15];
Array in C Programming: A Powerful Tool to Manage Data
When writing a program, the programmer needs to use data or values. As such, it is compulsory to declare a variable.
In C, a variable declaration looks like this: int x = 17;
Here, x is the variable name, where space allocation occurs inside the memory.
17 is the value assigned to the variable—the number 17 stores inside the allocated memory.
The programmer can use the above variable declaration method if a program requires only one or two values.
Array Examples in C Programming:
However, imagine a scenario where multiple values, like 100 or 2000, must be stored and managed. Declaring variables for each value (1 to 100 or 1 to 2000) would make the program long and challenging to read. This is where the concept of arrays comes in. Arrays offer a practical and efficient solution to such complications, significantly simplifying programming tasks and enhancing productivity.
With arrays, declaring variables becomes simpler. You only need to declare one variable but can store any number of values.
Array in C: An Example
Let’s consider a relatable scenario: a class of 15 students, each with a different score in a subject. Using the array concept, we can write a program in C to generate and manage these scores efficiently.
Here, instead of declaring different variables, a single variable can store all the values or marks of the entire class.
There are three steps to follow when declaring or creating an array.
Step 1: Define the data type of the variable.
Step 2: Specify the name of the array.
Step 3: Include the size of an array.
The format of the syntax for variable declaration is as follows,
int marks[15];
An ‘ int ‘ data type specifies that all the values or marks obtained are integers.
Then, leave space between the data type and the array’s name, here, ‘n.’
Next, specify the number of values. Here, it is 15 since there are 15 students.
Here, 15 represents the size of the array. Remember, there is no limit to the array size. The programmer can assign any number of values like 1000, 2000, etc.,
After that, an equal sign is given, and the flower brackets are opened and closed.
Within the flower brackets, specify all the marks scored by each student, followed by a comma.
Lastly, after closing the flower bracket, include a semi-colon specifying the ending of the array in the C declaration.
How to use printf() for Array:
In general, to print a variable in the statement int x=7, the programmer can write as follows,
printf(“%d”, x);
However, printing an array requires a different syntax. The above example is simple because there is only one value. However, many values exist in an array, so to call or identify each value, there should be a corresponding index. This is similar to giving roll numbers to each student. The indices start with n[0] for the first number, n[1] for the second, and vice versa.
It is as follows,
printf(“%d”, n[0]);
printf(“%d”, n[1]);
printf(“%d”, n[2]);
printf(“%d”, n[3]);
printf(“%d”, n[4]);
printf(“%d”, n[5]);
Using the above format, the programmer can access every value individually. Likewise, using a ‘for loop,’ the programmer can access all the values inside an array.
Array in C Addresses: Memory Allocation of Array Values
Finally, during the variable declaration, space is allocated for the variable inside the memory. An address, known as the variable address, is created as soon as the space allocation occurs. For integer values, there will be consecutive address allocation, but every value will take four address locations because the size of an integer is 4 bytes.