Day 4
Day 4
BASICS OF C
What will you learn?
Arrays
Types of Arrays
Strings
String Handling Functions
ARRAYS
ARRAY
An array is a user defined variable that contains a set of data items of same
type that share a common name those are stored in continuous memory
locations.
long, etc.
• Array-name is name of the array used to identify
• Size is the number of values that the array can hold. Once you define
can hold 10 integers. Each of these elements is accessed with the use
of index.
MEMORY REPRESENTATION OF AN ARRAY:
Marks
Index 0 1 2 3 4 5 6 7 8 9
The value of index starts from 0 and ends with (n – 1), where n is size
of an array. In the above example, index ranges from 0 to 9. This
means that the index of first element of an array is 0, second element
index is 1, third element index is 2, etc.
For example, the third element of “marks” array is accessed as
marks[2]. Similarly, sixth element is accessed as marks[5].
INITIALIZATION OF AN ARRAY
Declaring of an array means that it only allocated the memory
to store the values to the specified size of an array. No values are
stored in the array. So storing elements into an array at the time of its
declaration is called initialization. An array is initialized as follows:
marks[0] 90
marks[1] 87
marks[2] 75
marks[3] 81
marks[4] 86
ASSIGNMENT OF VALUES
Here, elements of an array are given values individually using the
assignment operator. This can be done in the following way:
int i, marks[10];
for(i=0;i<10;i++)
scanf(“%d”, &marks[i]);
DISPLAYING OF ELEMENTS:
As in the case of inputting values into an array, for displaying the
array elements also a loop structure is used.
int i, marks[10];
for(i=0;i<10;i++)
printf(“%d\t”, marks[i]);
TWO – DIMENSIONAL ARRAYS
An array is said to be two – dimensional array when it is used
the two subscripts to access the elements in the array - one index for
the row and the other index for the column. Two – Dimensional arrays
are used to store the data in the form of matrices.
Column Index 0 1 2 3
Row Index 0
1
2
DECLARATION OF 2D ARRAY
A two – dimensional array is declared as follows:
For example, to store the marks of 3 students in 5 subjects, then we declare the array as:
int marks[3][5];
The above statement creates an array with the size of 3 rows and 5 columns as shown
below:
For example,
{ 1 14 15 16
for ( j = 0 ; j < 3 ; j++)
{
i=2
scanf(“%d”, &marks [i][j]);
} j =3
}
DISPLAYING ELEMENTS OF 2D ARRAY
As in the case of inputting values into an array, for displaying the array elements
also a nested loop structure is used. The code should be like as follows:
int marks[2][3];
for ( i = 0 ; i < 2 ; i++)
{
for ( j = 0 ; j < 3 ; j++)
{
printf(“%d ”,marks[i][j]);
}
}
STRINGS
STRING HANDLING
STRING
“A string is defined as sequence of characters that is treated as
single data item”.
Examples “abcd”
“Ab123cde”
“Ramu”
declaration is as follows:
char string_name[size];
In the above syntax the size specifies the number of characters in the string_name.
For example
char city[10];
We can declare the string as a pointer as follows:
char *ptr;
Here the ptr is a pointer variable and the memory is allocated dynamically for it only
[0] [13]
I n i t i a l v a l u e \0 ? ? …
STRING IN C – INPUT/OUTPUT
The placeholder %s is used to represent string arguments in printf and scanf.
Example:
char message1[12] = "Hello world";
printf(“%s”,message1);
H e l l o w o r l d \0
Example:
char message2[12];
scanf(“%s”,message2);
H e l l o \0 ? ? ? ? ? ?
STRING IN C – LIBRARY FUNCTIONS