C Language | Array | Start your journey with C | Part 5



Arrays in C :

An array is collection of items of same datatype stored in a continuous block of memory under a variable name.

Array declaration in C :

We can declare an array by specifying the size with a variable or by directly initializing elements of the array or by both. all type of declaring example is show below :

  • Array declaration by specifying size :
// Array declaration by specifying size 

int var[10]; 
  
// With recent C versions, you can also 
// declare an array of user specified size 

int x = 10; 
int var[n];

  • Array declaration by initializing elements :
// Array declaration by initializing elements 

int var[] = { 10, 20, 30, 40 } ;
  
// Compiler creates an array of size 4. 
// above is same as  "int var[4] = {10, 20, 30, 40}"


An Example to show that array elements are stored at contiguous locations :

#include<stdio.h>

void main() 
{ 
    // an array of 10 integers.  If var[0] is stored at 
    // address x, then var[1] is stored at x + sizeof(int) 
    // var[2] is stored at x + sizeof(int) + sizeof(int) 
    // and so on. 
    int var[5], x; 
  
    printf("Size of integer in this compiler is %lu\n", sizeof(int)); 
  
    for (x = 0; x < 5; x++) 
        // The use of '&' before a variable name, yields 
        // address of variable. 
        printf("Address var[%d] is %p\n", x, &var[i]); 
  
} 

Output :

Size of integer in this compiler is 4
Address var[0] is 0x7ffd636b4260
Address var[1] is 0x7ffd636b4264
Address var[2] is 0x7ffd636b4268
Address var[3] is 0x7ffd636b426c
Address var[4] is 0x7ffd636b4270 

---------------------------------------------------------------------------------------------------------------

                                                     ADS BY AMAZON.IN

Comments

Popular posts from this blog

TOP 10 PROGRAMMING LANGAUAGES COMMING 2021 | ARTICLE

Write a C-program to swap two number:

Print Fibonacci Series using Recursion | C program :