Primitive type:
- Variables are used to store data.
- Primitive variable can store only one value at a time.
- For example,
int main(){ int a=5; a=10; a=20; printf(“a val : %d \n”, a); // prints – 20 return 0; } |
Array:
- Array is used to store more than one value but of same type.
- For example, storing 100 students’ average marks.
Array creation:
- In array variable declaration, we need to specify the size
- Array variable stores base address of memory block.
- Array elements store in consecutive memory location.
Process Array elements:
- Array memory locations must be accessed through their index.
- Index values starts from 0 to Size-1

How to access array elements?
- Array consists multiple values.
- We can simply use loops to process array locations.
Reading | Printing |
int i; for(i=0 ; i<5 ; i++) { scanf(“%d”, &marks[i]); } | int i; for(i=0 ; i<5 ; i++) { printf(“%d”, marks[i]); } |
Length of array:
- sizeof() function returns the total size of array.
- We can find the length of array by dividing total size with single element size.
int main() { float arr[5]; printf(“Total size of array : %d \n”, sizeof(arr)); printf(“Array element size : %d \n”, sizeof(arr[0])); printf(“Length of Array : %d \n”, sizeof(arr)/sizeof(arr[0])); return 0; } |
C program to read elements into array and display:
int main () { int arr[5], i; printf(“Enter 5 elements : \n”); for(i=0 ; i<5 ; i++){ scanf(“%d”, &arr[i]); } printf(“Array elements are : \n”); for(i=0 ; i<5 ; i++){ printf(“%d \n”, arr[i]); } return 0; } |
C Program to find sum of array elements:
int main () { int arr[5] = {10, 20, 30, 40, 50}, i, sum=0; for(i=0 ; i<5 ; i++){ sum = sum + arr[i]; } printf(“Sum of Array elements : %d \n”, sum); return 0; } |
C Program to display only even numbers in the array:
int main () { int arr[8] = {8, 5, 1, 7, 4, 3, 2, 9}, i; printf(“Even numbers of Array : \n”); for(i=0 ; i<8 ; i++){ if(arr[i]%2==0) printf(“%d \t”, arr[i]); } return 0; } |
C program to display the largest element in the array:
int main() { int arr[8] = {7,2,9,14,3,27,5,4}; int large=arr[0], i; for(i=1 ; i<8 ; i++){ if(arr[i]>large) large = arr[i]; } printf(“Largest element is : %d \n”, large); return 0; } |
Pass Array as a parameter to a function:
- We can pass array as input parameter to function.
- By passing array name, the address will be passed as parameter.
- We collect the address in Called Function by declaring same type array.
void display(int[]); int main(){ int a[5] = {10, 20, 30, 40, 50}; display(a); return 0; } void display(int x[]){ int i; printf(“Array is :\n”); for(i=0 ; i<5 ; i++){ printf(“%d\n”, x[i]); } } |
