
A collection of similar types of objects stored in sequential memory locations under a same header or variable name is referred to as an array in C. Elements are the individual values in an array.
In simple words, what is an array in C? In C, an array is a collection of elements of the same type that are stored in memory in a sequential order.
Table of Contents
Advantages of using Array in C
- It is simple to visit all of the items or to move from one element to another.
- Sorting, searching, reversing, and other data structure operations are also feasible.
- Multiple names are more difficult to recall than a single name.
Limitations of Array in C
What are Array’s limitations? The following information must be provided to the compiler:
- You can’t change the size of an array once it’s been constructed because it’s static.
- At runtime, memory cannot be allocated. Array allocates memory at compile time.
- Only elements of the same kind are stored in an array unused memory cannot be used while using an array.
In C, how do you declare arrays?
When declaring the array, you must include the following information.
- The array’s type (int, float, char etc.)
- Name of the Array
- The array’s number of subscripts (Dimensioning the array, whether it is 1D or 2D)
- You have to assign the the total number of memory locations.
Array Types in C Programming
- Single Dimensional Array
- Multiple Dimensional Array
Single Dimensional Array or 1D Array
A single dimensional array represents a single row or column of elements.
Syntax of 1D Array
storage_class data_type array_name[index];
Example of 1D Array
int intArray[10];
float salary[20];
char name[100];
1D Array Initialization
storage_class data_type array_name[index] = {element1, element2, element3... };
Example of 1D Array Initialization
int intArray[5] = {10, 20, 30, 40, 50};
// Representation in the memory
intArray[0]=10;
intArray[1]=20;
intArray[2]=30;
intArray[3]=40;
intArray[4]=50;
Write a program of one dimensional Array
#include <stdio.h>
int main()
{
int i;
int a[5] = {10, 20, 30, 40, 50};
printf( );
for( )
{
printf( );
}
return 0;
}
Output: Elements of array: 1 2 3 4 5
Two Dimensional Array or 2D Array
The two-dimensional array represents more than one row and column of elements.
Syntax of 2D Array in C
storage_class data_type array_name[rows][cols];
Example of 2D Array
int intArray[10][10];
float salary[20][3];
Note: The number of elements in a two-dimensional array is determined by the array's size. There will be 3 * 3 = 9 elements in the arr[3][3] array.
Write a program of two dimensional Array
#include <stdio.h>
int main()
{
int a[10][10];
int i, j, row = 3, col = 3;
printf( );
for( )
{
for( )
{
scanf( );
}
}
printf( );
for( )
{
for( )
{
printf( );
}
printf( );
}
return 0;
}
Output: Enter array elements: 1 2 3 4 5 6 7 8 9 Output of array elements 1 2 3 4 5 6 7 8 9
Note: The storage class is optional in the declaration, and it can be one of the variable's scopes, such as automatic, external, static, or register.
Character Array in C
Character arrays are used to declare alpha numeric characters in programming languages. This array can include both single characters and single integers.
Syntax of Character Array in C
storage_class char_data_type array_name[index];
Example of Character Array in C
char name[100];
char alphbets[26];
Character Array Initialization
storage_class char_data_type array_name[index] = {element1, element2, element3... };
Example of Character Array Initialization
char vowels[] = {'a', 'e', 'i', 'o', 'u'};
// Representation in the memory
vowels[0]='a';
vowels[1]='e';
vowels[2]='i';
vowels[3]='o';
vowels[4]='u';
char gender[2] = {'M', 'F', '\0'};
char site[7] = "GeekCer";
Reading string from the keyboard and display on screen
#include <stdio.h>
int main()
{
char site[7];
printf( );
scanf( );
printf( );
return 0;
}
Output: Enter website name: GeekCer Website name is GeekCer
getchar() function in C
To read a single character from the keyboard, use the getchar() method.
#include <stdio.h>
int main()
{
char ch;
printf( );
ch = getchar();
printf( );
return 0;
}
Output: Enter any character:G Entered character is G
putchar() function in C
To print a single character on the screen, use the putchar() function.
#include <stdio.h>
int main()
{
char ch;
printf( );
ch = getchar();
printf( );
putchar(ch);
return 0;
}
Output: Enter any character: G Entered character is G
This page discusses the several types of arrays, including one-dimensional and two-dimensional arrays. We’ve also talked about the character array, which is the most significant topic for programmers.
Click here to visit this page which will provide you programs related to arrays, character arrays, and other topics.
If you have any questions or concerns, please email us at geekcer.code@gmail.com. Or, you can write us on the contact us page.