coderz.py

Keep Coding Keep Cheering!

Arrays in C

Matrix multiplication

Arrays in the C language are a set of homogenous data types stored at the contiguous memory location. We can define one array variable to describe individual variables rather than specifying separate variables. An index lets you access a distinct element in an array.

Structure of an array:
Arrays in C
Key points:
  • Each element of an array has the same data type and size, such as int = 4 bytes.
  • The array’s elements are kept in contiguous memory regions, with the first element kept in the smallest memory place.
  • Because we can determine the address of each element of the array with the given base address and the size of the data element, we can access elements of the array at random.
Array Declaration:

We can declare an array in the c language in the following way:

The array size must be a positive integer constant, and the type can be any valid C data type.

data_type array_name[array_size];  

Now, let us see the example to declare the array:

int marks[5];

  

Initialization of C Array:

The index of each element is the easiest way to initialize an array. We use the index  to initialize each element of the array. Consider the following example. Array Size must be a positive integer constant, and type can be any valid C data type.

  1. marks[0]=80;//initialization of array  
  2. marks[1]=60;  
  3. marks[2]=70;  
  4. marks[3]=85;  
  5. marks[4]=75;  
Example:
#include<stdio.h>  
int main(){      
int i=0;    
int marks[5];//declaration of array       
marks[0]=80;//initialization of array    
marks[1]=60;    
marks[2]=70;    
marks[3]=85;    
marks[4]=75;    
//traversal of array    
for(i=0;i<5;i++){      
printf("%d \n",marks[i]);    
}//end of for loop     
return 0;  
}    
output:
80
60
70
85
75
Declaration and Initialization of a C Array

The c array can be initialized at the time of declaration. Let’s take a look at the code.

int marks[5]=20,30,40,50,60;

There is no need to declare the size in this situation. As a result, it might alternatively be worded as follows.

 int marks[]=20,30,40,50,60; 

Note: also read about the Recursive function in C & Program to check whether a number is even or odd

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Leave a Comment

Your email address will not be published. Required fields are marked *

Advertisement