Introduction
C Programming
C Advanced
Important C Programs
Tutorials
C is a very powerful and widely used language. It is used in many scientific programming situations. It forms (or is the basis for) the core of the modern languages Java and C++. It allows you access to the bare bones of your computer.C++ is used for operating systems, games, embedded software, autonomous cars and medical technology, as well as many other applications. Don't forget to visit this section...
Array in C Language
When we work with large number of data values, we also need that many number of different variables. As the number of variables increases, the complexity of the program also increases and so the programmers get confused with the variable names. There may be situations where we need to work with large number of similar data values. To make this work more easy, C programming language provides a concept called “Array”.
An array is a special type of variable used to store multiple values of same data type at a time.
- single dimensional array.
- multi dimensional array ( discussed in the next section.)
How to declare an array
The syntax to declare an array is <datatype> <array_name> [size];
eg : int a[10];
Here, ‘a’ is an array name of size 10 and it can store integer type values.
The index (position) of the array element starts from 0 and last index is [arraysize] – 1 ( 9 -> last index in this example).
Suppose, we have to store a value at the given index (either from 0 to the last index)
[index]=value;
eg: a[2]=10 ;
it means that at 2nd index /third location of the array, 10 is stored.
Similarly, we can take input and print the values of array as following:
for input: scanf(“%d”,&a[0]); // scans the integer value at first index of the array.
for display: printf(“%d”,a[0]);
Program to take input 10 elements in an array and print them back in reverse order.
#include<stdio.h> #include<conio.h> void main() { int a[10], i=0; printf("enter ten array elements"); // take input in the array. while(i<10) { scanf("%d", &a[i]); i++; } i=9; // last index of the array because we have to print it in reverse order. while(i>=0) { printf("%d ", a[i]); i-- ; } }