Unit 4
Array
4.2 One dimensional Array
4.3 Declaration of 1D
4.4 Initialization of 1D
4.5 2D arrays
4.6 initialization of 2D
4.7 Declaring and Initializing String Variable.
4.8. Reading string from terminal
4.9. writing string to screen
4.10 putting strings together
4.11 comparison of two strings
4.12 string handling functions.
4.1 Introduction
Array in C is one of the most used data structures in C programming. It is a
simple and fast way of storing multiple values under a single name.
What is Array in C?
An array in C is a fixed-size collection of similar data items stored in
contiguous memory locations. It can be used to store the collection of
primitive data types such as int, char, float, etc., and also derived and user-
defined data types such as pointers, structures, etc.
,Array Declaration
In C, we have to declare the array like any other variable before using it. We
can declare an array by specifying its name, the type of its elements, and the
size of its dimensions.
Syntax of Array Declaration
data_type array_name [size];
or
data_type array_name [size1] [size2]...[sizeN];
where N is the number of dimensions.
The C arrays are static in nature, i.e., they are allocated memory at the compile
time.
//simple array using static allocation
#include <stdio.h>
, int main()
{
int arr[5] = { 10, 20, 30, 40, 50 };
for (int i = 0; i < 5; i++)
{
printf("\n Numbers in Array=%d",arr[i]);
}
return 0;
}
//simple array using dynamic allocation
#include <stdio.h>
int main()
{
int a[5],i;
printf("Enter the numbers");
for ( i = 0; i < 5; i++)
{
scanf("%d",&a[i]);
}
for ( i = 0; i < 5; i++)
{
printf("\n %d",a[i]);
}
return 0;
}
Array
4.2 One dimensional Array
4.3 Declaration of 1D
4.4 Initialization of 1D
4.5 2D arrays
4.6 initialization of 2D
4.7 Declaring and Initializing String Variable.
4.8. Reading string from terminal
4.9. writing string to screen
4.10 putting strings together
4.11 comparison of two strings
4.12 string handling functions.
4.1 Introduction
Array in C is one of the most used data structures in C programming. It is a
simple and fast way of storing multiple values under a single name.
What is Array in C?
An array in C is a fixed-size collection of similar data items stored in
contiguous memory locations. It can be used to store the collection of
primitive data types such as int, char, float, etc., and also derived and user-
defined data types such as pointers, structures, etc.
,Array Declaration
In C, we have to declare the array like any other variable before using it. We
can declare an array by specifying its name, the type of its elements, and the
size of its dimensions.
Syntax of Array Declaration
data_type array_name [size];
or
data_type array_name [size1] [size2]...[sizeN];
where N is the number of dimensions.
The C arrays are static in nature, i.e., they are allocated memory at the compile
time.
//simple array using static allocation
#include <stdio.h>
, int main()
{
int arr[5] = { 10, 20, 30, 40, 50 };
for (int i = 0; i < 5; i++)
{
printf("\n Numbers in Array=%d",arr[i]);
}
return 0;
}
//simple array using dynamic allocation
#include <stdio.h>
int main()
{
int a[5],i;
printf("Enter the numbers");
for ( i = 0; i < 5; i++)
{
scanf("%d",&a[i]);
}
for ( i = 0; i < 5; i++)
{
printf("\n %d",a[i]);
}
return 0;
}