TRENDING TOPICS

×

Array in C

In this Tutorial we will learn array in c programming, Declaration of Array, Initialization of Array, Accessing Array elements, Types of array in C and use of array with example.

Array Definition -

Array is collection of similar data type elements or in other words an array is a linear and homogeneous data structure.

Array in C

Need of Array

When we need to store large amount of data then we can use variables, suppose if we want to store 40 integers type value then we requires 40 variables of int types to store these values, now if we want to perform some operation on these value like we want to add all these value then we requires to write these 40 variables with plus(+) symbol, Let’s see the problems which we will face during operation.

  • Declaring 40 variables is bit boring task.
  • It is difficult to memorize all variables and their values.
  • Program will become more lengthy, and it will be difficult to manage the code.
  • Any operation performed on variables can unnecessary increase lines of code.

Array

An array is collection of variables of same data type and it is referenced by a common name. An array is a linear and homogeneous data structure. Here leaner data structure means storing individual data element in a sequential order in assigned memory and Homogeneous means the all individual data elements are the same data type.

c array eample

You can assume array like as a single flor company office building, in which there are separate cabins for each employee and all cabins are attached together in a sequence like above image. Every cabin marked by a number in a sequence of 0,1,2,3,4,5. Now let’s compare it with array.

  • An Array has a name, same as array this building has a name.
  • Array elements are same data type, same as array all people working in this building are type of employee, means they are also same type.
  • We can find an array element by array name and index number, same as array we can find an employee with building name and cabin number.
  • So you can see array is a very simple way to organize data and further you will see how simply we can perform some complex operations on array very easily.

Array Declaration in C

Syntax

data_type array_name [array_size]

Example

int myNumber[5]

a. Data Type

An Array can be any kind of data types like int, float, double, string, char etc. Simply data type tell us about the type of value which can be stored in that array. For example if the data type is int the we can store only int type value in that array.

b. Array Name

Array name is just like variable name, it depends on user what array name he want to declare. The only one this important to follow that it should fallow all variable naming convention rules like

  • It should not be a c keyword name.
  • Only _ allowed as special symbol.
  • Digit are allowed in naming.
  • Alphabets from a to z are allowed.
  • Array name is case sensitive.

Array Size

Array Size is tells the number of element can be stored in array, Let's see some points.

  • Array size are denoted within [ ] sybmols. for exmple [5], [9], [20] etc.
  • Array size is represented in form of int type value like 2, 8, 20, 7 etc.
  • Arrays are fixed type of data structure means the size of array cannot be changed in run time.

Initialization of Array in C

In C programming array can be initialized by two ways

  1. Default Type Initialization
  2. Index based Initialization

Note : Array index always starts from 0, means array's first cell index is 0 and last index is (n-1), where n is size of array.

1. Default Type Initialization

Generally this type of array initialization done at the time of array initialization by hard coded values. Let’s see in action.

Syntax

data_type array_name [array_size] = {value1, value2,... };

Example

int myNumber [5] = {7,20,8,11,13};
string employee [3] = {"Tod","Rob","Mau"};
float marks [4] = {92.5,97,91.78,78.2};
int score [ ] = {3,5,7};

In this type of array initialization all value passed in {} separated by comma (,). Read below points carefully.

  • If array size is given then, number of values should not be more than size of array.
  • If values are string type then the values must be within " ".
  • If we do not declare size of array then the size of array will become equal to the values passed in array at the time of initialization.
  • In Example 4, the array size is 3 because this array size is not declare and it contains 3 element, So size of array = number of element in array.

2. Index Based Initialization

In this type of array initialization, initialization is done based on array index, means first array in declare and then initialization done latter based on index value. See below example.

Example

int myNumber [5];
myNumber [0] = 12;
myNumber [1] = 7;
myNumber [2] = 13;
myNumber [3] = 22;
myNumber [4] = 70;

You can initialize array based on index, see the below mentioned image how the array looks in memory after initialization.

array in c example

Note

Suppose you want to declare an array with large size, now at the time of initialization if you are passing less value than size then in empty cells there will be garbage value in empty cells.

Example

int myNumber [5];
myNumber [0] = 8;
myNumber [1] = 22;
myNumber [2] = 9;
array in c example

Accessing Array Element in C

Array elements are access based on their array index, array index starts with 0 and last index is (n-1) where n is the size of array. See below example to understand how we can access array elements.

int myNumber [5] = {7,20,8,11,13};

Array Elements Values

myNumber [0] == 7;
myNumber [1] == 20;
myNumber [2] == 8;
myNumber [3] == 11;
myNumber [4] == 13;

Example

printf("Array index 0 value is %d",myNumber[0])

Output

Array index 0 value is 7

Types of Array in C

In C programming array can be classified in two categories, which are listed below.

  1. One-Dimensional Array
  2. Multi-Dimensional Array

In this tutorial we will see what one-dimensional array is and how to use it in programming, and also we will see some important array examples which are several times asked in interview or in exams. We will learn multi-dimensional array in next chapter in details.

1. One-Dimensional Array

One-Dimensional array is an array which is represented either in one row or in one column in other words, it can be represented as in a single dimension-width or height. Till now all above example are one-dimensional array. Let’s see how one-dimensional array looks.

Types of Array in C

One-Dimensional Array Example

1D Array Syntax

data_type array_name [array_size] = {value1, value2,... };

Example

int rollNumbers [5] = {101,207,863,723,131};
string studentNames [3] = {"Tod","Rob","Mau"};
float ExamMarks [ ] = {232.42,311.12,212,235.10};

Note: The declaration, initialization and accessing elements of a One-Dimensional Array are same as which we have studied above. Now Let's see some important array programs based on One-Dimensional Array.

What will be output..?

If the user Enter four values 13, 20, 11, 9 then what will be the output of above program..?

Output

Value = 9
Value = 9
Value = 9
Value = 9

Explanation

Since each time we are updating num variable value with recent entered value by user, so 4th value will be the final value of variable num and we are prinitng value of variable num four times.

Array Example 1

Write a program in C, to print all element of a given int type array.

Output

13
17
19
21
23

Explanation

In above example the array size is 5 and the value stored at locations num[0], num[1], num[2], num[3], num[4] are 13,17,19,21,23. The for loop will execute 5 times and every iteration the value of i increased by 1 which is 0,1,2,3,4. In each iteration we are printing the value of num[i] which is num[0], num[1], num[2], num[3], num[4], so we will get output as above result.

Array Example 2

Write a program in C to display integer entered by user using array.

Output

Enter eight enteger values
233
432
35
43
23
54
45
12

Entered value by user are
233
432
35
43
23
54
45
12

Array Example 3

Write a program in C to display character entered by user using array.

Output

Enter ten charachers
x
b
s
e
f
e
e
f
s
k

Entered characters are
x
b
s
e
f
e
e
f
s
k

Array Example 4

Write a program in C to find sum of all integers entered by user with the help of array. OR Write a program to find sum of all element of an array.

Output

Enter 15 enteger values
9
11
13
15
17
19
21
23
25
27
29
31
33
35
37

Sum of all Entered Numbers = 345

Array Example 5

Write a program in C to find largest number from 10 numbers entered by user with the help of array. OR Write a program to find largest number of array.

Output

Enter 10 enteger values
12
13
44
312
2423
43
32
654
46
89

Largest Number = 2423

Note: More array program like shoring number in increasing or decreasing order and other programs in upcomming pages.

Advertise With Us

We offer sponsored posts and guest posts with do-follow links, back-links, and other advertising services, To advertise email us at "advertise@gdatamart.com"

© 2021 GDATAMART.COM (All Rights Reserved)