CodesJava

Easy learning with example program codes

C 1-D Array


An array is a single variable which can store multiple values at a time. Thus, C array is used to hold many values under a single name. These values can be accessed the by referring to their index number.

Syntax:

data_type arrayName [arraySize];

Or

data_type arrayName [arraySize] = array{value 1, value 2, …};

Advantages of using Array in C:

  • Single variable need to be declared, instead of multiple variable, making the code user friendly.
  • Less number of lines needed in a code.
  • Traversing of all the elements is easy.
  • Functions like sorting can be easily applied to all the elements.

Example: Printing Table of 5.

#include<stdio.h>  
 
void main()
{    
int table[10] = {5,10,15,20,25,30,35,40,45,50};
int i;
for (i=1;i<11;i++)
{
printf ("5 * %d = %d", i,table[i-1]);
printf ("\n");
}
}

Output

5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
Posted in C   
Sign Up/ Sign In
Ask a Question


Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki