0% found this document useful (0 votes)
76 views

Array in C Programming

An array in C programming allows storing multiple values of the same data type. Arrays are declared by specifying the data type, array name, and size in brackets. Individual elements can then be accessed via indices starting from 0. Arrays can be initialized during declaration by providing initial values inside curly braces. Once declared, the size and type of an array cannot change, though individual element values can be modified. Elements are accessed using their index and input or output similarly. Accessing elements outside the bounds of the array may result in undefined behavior.

Uploaded by

basti
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views

Array in C Programming

An array in C programming allows storing multiple values of the same data type. Arrays are declared by specifying the data type, array name, and size in brackets. Individual elements can then be accessed via indices starting from 0. Arrays can be initialized during declaration by providing initial values inside curly braces. Once declared, the size and type of an array cannot change, though individual element values can be modified. Elements are accessed using their index and input or output similarly. Accessing elements outside the bounds of the array may result in undefined behavior.

Uploaded by

basti
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Array in C Programming

WHAT IS AN ARRAY?
• It is a variable that can store multiple values.
• For example, if you want to store 5 integers, you can create an array for
it.

Example:
int data[5];
HOW TO DECLARE AN ARRAY?
SYNTAX:
dataType arrayName[arraySize];
Example:
float mark[5];
• Here, we declared an array mark, of floating-point type.
• And its size is 5.
• Meaning, it can hold 5 floating-point values.

NOTE: The size and type of an array cannot be changed once it is declared.
ACCESS ARRAY ELEMENTS
• You can access elements of an array by indices.
• Suppose you declared an array mark. The first element is mark[0], the second
element is mark[1] and so on.
NOTES:
• Arrays have 0 as first index, not 1. In this case mark[0] is the first element.
• If the size of an array is n, to access the last element, then n-1 index is used.
In this example, mark[4]
• Suppose the starting address of mark[0] is 2120. Then, the address of mark[1]
will be 2124. Similarly, the address of mark[2] will be 2128 and so on.
• This is because the size of a float is 4 bytes.
HOW TO INITIALIZE AN ARRAY?
• It is possible to initialize an array during declaration.

Example:
int mark[5] = {19, 10, 8, 17, 9};

You can also initialize an array like this:


int mark[] = {19, 10, 8, 17, 9};
• Here, we haven’t specified the size. However, the compiler knows that
its size is 5 as we are initializing with 5 elements.
CHANGE VALUE OF ARRAY ELEMENTS
Int mark[5] = {19, 10, 8, 17, 9};

// make the value of the third element to -1


mark[2] = -1;

// make the value of the fifth element to 0


mark[4] = 0;
INPUT AND OUTPUT ARRAY
ELEMENTS
• Here’s how you can take input from the user and store in an array
element.
// take input and store it in the 3rd element
scanf(“%d, &mark[2]);

Here’s how you can print an individual element of an array.


// print the third element of the array
printf(“%d, mark[2]);
Example 1: Array Input/Output
// Program to take 5 values from the user and store them in an array
// Print the elements stored in the array
#include <stdio.h>
main() {
int values[5];
printf("Enter 5 integers: ");
// taking input and storing it in an array
for(int i = 0; i < 5; ++i) {
scanf("%d", &values[i]);
}
printf("Displaying integers: ");
// printing elements of an array
for(int i = 0; i < 5; ++i) {
printf("%d\n", values[i]);
}
}
ACCESS ELEMENTS OUT OF ITS
BOUND
• Suppose you declared an array of 10 elements.
Let’s say: int testArray[10];
• You can access the array elements from testArray[0] to testArray[9];
• Now let’s say if you try to access testArray[12]. The elemtent is not
available. This may cause unexpected output (undefined behavior).
Sometimes you might get an error and some other time your program
may run correctly. Hence, you should never access elements of an
array outside of its bound.

You might also like