Arrays are block of contiguous memory location that can store collection of similar type of data items. The data is present in a linear fashion and each data item has an index number.
C arrays start with index 0. The size of the array is the size of the data type times the number of data items. Arrays can be one dimentional or multidimensional.
dataType arrayName[arraySize];
//At initialization
dataType arrayName[3] = {1,2,3};
//or
dataType arrayName[] = {1,2,3};
dataType arrayName[arraySize1][arraySize2];
//At initialization
arrayName[][2] = {1,2,3,4};
//or
arrayName[2][2] = {1,2,3,4};
To store multiple integers with a single variable name, one has to use an integer array. To create an integer array, an additional size parameter is required along with the variable name in square brackets. Similarly, floating point arrays and other data type arrays can be created.
#include<stdio.h>
//g_array of size 10 initialized with 0 by default
int g_array[10];
//g2_array initialised by 1
int g2_array[10] = {1};
int main(void)
{
//my_array[0] = 1, my_array[1] = 2, ..., my_array[4] = 5
int my_array[5] = {1,2,3,4,5};
//new_array is unitialized, contains garbage values
int new_array[100];
int i;
//Finding number of data items present (Avoid hard coding)
int sizeof_new_array = sizeof(new_array)/sizeof(new_array[0]);
printf("Size of new_array: %d\n",sizeof_new_array);
for(i = 0;i < sizeof_new_array;i++)
new_array[i] = i+1;
printf("g_array[0]: %d \n",g_array[0]);
printf("g2_array[0]: %d \n",g2_array[0]);
printf("new_array[50]: %d \n",new_array[50]);
return 0;
}
Size of new_array: 100
g_array[0]: 0
g2_array[0]: 1
new_array[50]: 51
In C, one has to use a character array to store strings. Character arrays are ended with a null character '\0' which denotes end of the string. So to store a string of a size n, atleast a character array of n+1 is needed.
#include<stdio.h>
//g_array initialized with '\0'
char g_array[5];
int main(void)
{
//my_array initialised with string "hello"
char my_array[6] = {'h','e','l','l','o','\0'};
//my2_array initialised with hello, run-time space allocation
char my2_array[] = "hello";
printf("my_array: %s\n",my_array);
printf("my_array[0]: %c\n",my_array[0]);
printf("g_array: %s\n",my_array);
return 0;
}
my_array: hello
my_array[0]: h
g_array:
#include<stdio.h>
int main(void)
{
int row,col;
/*
00,01,02,
10,11,12,
20,21,22
*/
int matrix[3][3] = {1,0,0,
0,1,0,
0,0,1};
for(row=0;row<3;row++) //Controls Row
{
for(col=0;col<3;col++) //Controls Column
printf("%d ",matrix[row][col]);
printf("\n");
}
return 0;
}
1 0 0
0 1 0
0 0 1
#include<stdio.h>
int main(void)
{
int i,j;
//name_db[][100] = {"sam","ron","lana","tim","shawn","sofia"} is valid
char name_db[6][100] = {"sam","ron","lana","tim","shawn","sofia"};
for(i=0;i<6;i++) //Controls Column
printf("%s\n",name_db[i]);
return 0;
}
sam
ron
lana
tim
shawn
sofia
C Programming
Overview Hello World Installation Comments Header Files Data Types Tokens Operators Input/Output Format Specifiers Decision Control Loops Type Qualifiers Storage class specifier Arrays Enums Structures Unions Functions Recursions Inline Functions Pointers Pointer Arithematic Call by value and call by reference Linked Lists Passing of Arrays in Functions Preprocessor and Macros Mathematical Operations String Operations Bit Fields Command Line Arguments Variable Arguments Error Handling Type Casting Typedef File Handling Multi Processing Threads Outdated C Programming Memory Management Compilation C Programs Interview Questions MiscellaneousTrending
C Programming
Remember to follow community guidelines and don't go off topic.