Array in C
Array in C
What is Array?
Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the
same type. An array is used to store a collection of data, but it is often more useful to think of an
array as a collection of variables of the same type.
Advantage of C Array
1) Code Optimization: Less code to the access the data.
2) Ease of traversing: By using the for loop, we can retrieve the elements of an array easily.
3) Ease of sorting: To sort the elements of the array, we need a few lines of code only.
4) Random Access: We can access any element randomly using the array.
Disadvantage of C Array
1) Fixed Size: Whatever size, we define at the time of declaration of the array, we can't exceed the
limit.
Declaring Array -
To declare an array in C, a programmer specifies the type of the elements and the number of
elements required by an array as follows −
The above statement will take the 5th element from the array and assign the value to salary variable.
Initialization of C Array
The simplest way to initialize an array is by using the index of each element. We can initialize each
element of the array by using the index. Consider the following example.
marks[0]=80;//initialization of array
marks[1]=60;
marks[2]=70;
marks[3]=85;
marks[4]=75;
Initialization of 2D Array in C
In the 1D array, we don't need to specify the size of the array if the declaration and initialization are
being done simultaneously. However, this will not work with 2D arrays. We will have to define at least
the second dimension of the array. The two-dimensional array can be declared and defined in the
following way.
int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
The string can be defined as the one-dimensional array of characters terminated by a null
('\0'). The character array or the string is used to manipulate text such as word or sentences.
Each character in the array occupies one byte of memory, and the last character must always
be 0. The termination character ('\0') is important in a string since it is the only way to identify
where the string ends. When we define a string as char s[10], the character s[10] is implicitly
initialized with the null in the memory.
By char array
By string literal
char ch[10]={'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't', '\0'};
We have used scanf to accept the input from the user. However, it can also be used in the case of
strings but with a different scenario. Consider the below code which stores the string while space is
encountered.
#include<stdio.h>
void main ()
{
char s[20];
printf("Enter the string?");
scanf("%s",s);
printf("You entered %s",s);
}
C String Functions
The strlen() function returns the length of the given string. It doesn't count null character '\0'.
#include<stdio.h>
#include <string.h>
main(){
char ch[20]={'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't', '\0'};
printf("Length of string is: %d",strlen(ch));
}
The strcmp(first_string, second_string) function compares two string and returns 0 if both strings are
equal.
Here, we are using gets() function which reads string from the console.
#include<stdio.h>
#include <string.h>
main(){
char str1[20],str2[20];
printf("Enter 1st string: ");
gets(str1);//reads string from console
printf("Enter 2nd string: ");
gets(str2);
if(strcmp(str1,str2)==0)
printf("Strings are equal");
else
printf("Strings are not equal");
}
C Math Functions
There are various methods in math.h header file. The commonly used functions of math.h
header file are given below.
Let's see a simple example of math functions found in math.h header file.
#include<stdio.h>
#include <math.h>
main(){
printf("\n%f",ceil(3.6));
printf("\n%f",ceil(3.3));
printf("\n%f",floor(3.6));
printf("\n%f",floor(3.2));
printf("\n%f",sqrt(16));
printf("\n%f",sqrt(7));
printf("\n%f",pow(2,4));
printf("\n%f",pow(3,3));
printf("\n%d",abs(-12));
}
Sample Example of Array
#include <stdio.h>
main()
{
int avg = 0;
int sum =0;
int x=0;
avg = sum/4;
printf("Average of entered number is: %d", avg);
}