The ones who are crazy enough to think they can change the world are the ones who do.
- Steve Jobs

C Arrays

What is Array

Arrays are one of a kind of data structure because arrays defines the way of arranging the data, which allows us to manipulated the data in interesting ways.Array is a collection of data, which is very similar to a matrix but a difference is that array can hold only data of similar datatypes i.e) We could have an array of intergers or an array of characters or an array of floating point. It reduces the programmers task.

C Arrays
3 x 3
1  1  1
1  1  1
1  1  1
3 x 3
0.3  0.2  0.1
0.2  0.3  0.2
0.1  0.1  0.3
3 x 3
a  b  c
d  e  f
g  h  i

Syntax - Array

Syntax
data_type arrayname[index];
  • An array variable can be declared as any standard data type.
  • The index value is size of an array.
  • An array index always starts from 0.

Valid Array declaration

Here we go through some valid array declaration.

  • int arr[5]    = {1, 2, 3, 4, 5};
  • char arr[5] = {'a', 'b', 'c', 'd', 'e'};
  • char arr[5] = {'a', 98, 'c', 100, 'e'};
  • float arr[3] = {1.0, 2.5, 3.9};

Invalid Array declaration

Here we go through some invalid array declaration.

  • char arr[5] = {a, b, c, d, e};
  • int arr[5] = { '1', '2', '3', '4', '5'}
  • char arr[2] = {"Invalid", "array"} ;

Why Array

Array is most useful when programmers need to declare multiple variable of same type to store similar data(e.g. marks of 20 students). For example, if a user wish to store marks of 5 students, then he might have to declare 5 different variables to store each student's mark. This scenario will be tough, if the user has to store 10 or more students marks. So, c provides the alternative one named array.

C program - Without Using Arrays

Let's workout a program to demonstrate Arrays in C.

noarray.c
#include <stdio.h>
int main()
{
int s1, s2, s3, s4, s5;
printf ("Enter students marks details ");
printf ("\ns1 = ");
scanf ("%d", &s1);
printf ("\ns2 = ");
scanf ("%d", &s2);
printf ("\ns3 = ");
scanf ("%d", &s3);
printf ("\ns4 = ");
scanf ("%d", &s4);
printf ("\ns5 = ");
scanf ("%d", &s5);
printf ("\n---Students marks details---\n ");
printf ("s1 = %d\n", s1);
printf ("s2 = %d\n", s2);
printf ("s3 = %d\n", s3);
printf ("s4 = %d\n", s4);
printf ("s5 = %d\n", s5);
return 0;
}
  • Enter students marks details
  • s1 = 78
  • s2 = 90
  • s3 = 87
  • s4 = 92
  • s5 = 95
  • ---Students marks details---
  • s1 = 78
  • s2 = 90
  • s3 = 87
  • s4 = 92
  • s5 = 95

Note:

The above program reads and prints the marks of all the 5 students by declaring 5 variables to hold each student's mark. So, the program uses many statements.

C program - Using Arrays

Let's workout a program to demonstrate Arrays in C.

array.c
#include <stdio.h>
int main()
{
int s[5], i;
printf ("Enter students marks details ");
for(i = 0; i < 5; i++)
{
printf ("\ns%d = ", i + 1);
scanf ("%d",&s[i]);
}
printf ("\n---Students marks details--- ");
for(i = 0; i < 5; i++)
{
printf ("\ns%d = %d ", i + 1, s[i]);
}
return 0;
}
  • Enter students marks details
  • s1 = 78
  • s2 = 90
  • s3 = 87
  • s4 = 92
  • s5 = 95
  • ---Students marks details---
  • s1 = 78
  • s2 = 90
  • s3 = 87
  • s4 = 92
  • s5 = 95

Note:

In the above program the 's' array name with subscript of 5. The same output with less number of statements in a program even we have to store 1000 students marks.

Report Us

We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.

Report