BPC1UNIT4
BPC1UNIT4
Arrays in C
An array is a variable that can store multiple values. For example, if you want
to store 100 integers, you can create an array for it.
int data[100];
For example,
float mark[5];
Here, we declared an array, mark, of floating-point type. And its size is 5.
Meaning, it can hold 5 floating-point values.
It's important to note that the size and type of an array cannot be changed
once it is declared.
Suppose you declared an array mark as above. The first element is mark[0],
the second element is mark[1] and so on.
Declare an Array
Few keynotes:
● Arrays have 0 as the first index, not 1. In this example, mark[0] is the
first element.
● If the size of an array is n, to access the last element, the n-1 index is
used. In this example, mark[4]
● Suppose the starting address of mark[0] is 2120d. Then, the address of
the mark[1] will be 2124d. Similarly, the address of mark[2] will be 2128d
and so on.
This is because the size of a float is 4 bytes.
How to initialize an array?
It is possible to initialize an array during declaration. For example,
Here, we haven't specified the size. However, the compiler knows its size is 5
as we are initializing it with 5 elements.
Initialize an Array
Here,
mark[0] is equal to 19
mark[1] is equal to 10
mark[2] is equal to 8
mark[3] is equal to 17
mark[4] is equal to 9
#include <stdio.h>
int main() {
int values[5];
Run Code
Output
Enter 5 integers: 1
-3
34
0
3
Displaying integers: 1
-3
34
0
3
Here, we have used a for loop to take 5 inputs from the user and store them
in an array. Then, using another for loop, these elements are displayed on the
screen.
#include <stdio.h>
int main() {
return 0;
}
Run Code
Output
Now let's say if you try to access testArray[12]. The element is not available.
This may cause unexpected output (undefined behavior). Sometimes you
might get an error and some other time your program may run correctly.
Hence, you should never access elements of an array outside of its bound.
C Multidimensional Arrays
In C programming, you can create an array of arrays. These arrays are known
as multidimensional arrays. For example,
float x[3][4];
Here, x is a two-dimensional (2d) array. The array can hold 12 elements. You
can think the array as a table with 3 rows and each row has 4 columns.
float y[2][4][3];
Here, the array y can hold 24 elements.
Initialization of a 2d array
// Different ways to initialize two-dimensional array
Initialization of a 3d array
int test[2][3][4] = {
{{3, 4, 2, 3}, {0, -3, 9, 11}, {23, 12, 23, 2}},
{{13, 4, 56, 3}, {5, 9, 3, 5}, {3, 1, 4, 9}}};
Run Code
Output
City 1, Day 1: 33
City 1, Day 2: 34
City 1, Day 3: 35
City 1, Day 4: 33
City 1, Day 5: 32
City 1, Day 6: 31
City 1, Day 7: 30
City 2, Day 1: 23
City 2, Day 2: 22
City 2, Day 3: 21
City 2, Day 4: 24
City 2, Day 5: 22
City 2, Day 6: 25
City 2, Day 7: 26
Displaying values:
City 1, Day 1 = 33
City 1, Day 2 = 34
City 1, Day 3 = 35
City 1, Day 4 = 33
City 1, Day 5 = 32
City 1, Day 6 = 31
City 1, Day 7 = 30
City 2, Day 1 = 23
City 2, Day 2 = 22
City 2, Day 3 = 21
City 2, Day 4 = 24
City 2, Day 5 = 22
City 2, Day 6 = 25
City 2, Day 7 = 26
#include <stdio.h>
int main()
{
float a[2][2], b[2][2], result[2][2];
if (j == 1)
printf("\n");
}
return 0;
}
Run Code
Output
Sum Of Matrix:
2.2 0.5
-0.9 25.0
#include <stdio.h>
int main()
{
int test[2][3][2];
printf("\nDisplaying values:\n");
for (int i = 0; i < 2; ++i)
{
for (int j = 0; j < 3; ++j)
{
for (int k = 0; k < 2; ++k)
{
printf("test[%d][%d][%d] = %d\n", i, j, k, test[i][j][k]);
}
}
}
return 0;
}
Run Code
Output
Enter 12 values:
1
2
3
4
5
6
7
8
9
10
11
12
Displaying Values:
test[0][0][0] = 1
test[0][0][1] = 2
test[0][1][0] = 3
test[0][1][1] = 4
test[0][2][0] = 5
test[0][2][1] = 6
test[1][0][0] = 7
test[1][0][1] = 8
test[1][1][0] = 9
test[1][1][1] = 10
test[1][2][0] = 11
test[1][2][1] = 12
C Programming Strings
In C programming, a string is a sequence of characters terminated with a null
character \0. For example:
Memory Diagram
char s[5];
String Declaration in C
Here, we have declared a string of 5 characters.
String Initialization in C
Let's take another example:
Here, we are trying to assign 6 characters (the last character is '\0') to a char
array having 5 characters. This is bad and you should never do this.
char c[100];
c = "C programming"; // Error! array type is not assignable.
Output
Also notice that we have used the code name instead of &name with scanf().
scanf("%s", name);
This is because name is a char array, and we know that array names decay to
pointers in C.
Thus, the name in scanf() already points to the address of the first element in
the string, which is why we don't need to use &.
You can use the gets() function to read a line of string. And, you can use
puts() to display the string.
Output
Here, we have used gets() function to read a string from the user.
Note: The gets() function can also be to take input from the user. However, it
is removed from the C standard.
It's because gets() allows you to input any length of characters. Hence, there
might be a buffer overflow.
● C strlen()
● The strlen() function takes a string as an argument and returns its
length. The returned value is of type size_t (an unsigned integer type).
● It is defined in the <string.h> header file.
●
● Example: C strlen() function
● #include <stdio.h>
● #include <string.h>
● int main()
● {
● char a[20]="Program";
● char b[20]={'P','r','o','g','r','a','m','\0'};
●
● // using the %zu format specifier to print size_t
●
● return 0;
● }
● Run Code
● Output
● Length of string a = 7
● Length of string b = 7
●
● Note that the strlen() function doesn't count the null character \0 while
calculating the length.
C strcat()
The function definition of strcat() is:
char *strcat(char *destination, const char *source)
strcat() arguments
As you can see, the strcat() function takes two arguments:
The strcat() function concatenates the destination string and the source
string, and the result is stored in the destination string.
#include <string.h>
int main() {
strcat(str1, str2);
puts(str1);
puts(str2);
return 0;
Output
This is programiz.com
programiz.com
Note: When we use strcat(), the size of the destination string should be large
enough to store the resultant string. If not, we will get the segmentation fault
error.
C strcmp()
The strcmp() compares two strings character by character. If the strings are
equal, the function returns 0.
C strcmp() Prototype
The function prototype of strcmp() is:
strcmp() Parameters
The function takes two parameters:
● str1 - a string
● str2 - a string
Return Remarks
Value
<0 if the first non-matching character in str1 is lower (in ASCII) than
that of str2.
#include <string.h>
int main() {
int result;
return 0;
Output
strcmp(str1, str2) = 1
strcmp(str1, str3) = 0
In the program,
● strings str1 and str2 are not equal. Hence, the result is a non-zero
integer.
● strings str1 and str3 are equal. Hence, the result is 0.
C strcpy()
C strcpy()
The function prototype of strcpy() is:
● The strcpy() function copies the string pointed by source (including the
null character) to the destination.
● The strcpy() function also returns the copied string.
Example: C strcpy()
#include <stdio.h>
#include <string.h>
int main() {
strcpy(str2, str1);
puts(str2); // C programming
return 0;
Run Code
Output
C programming
Note: When you use strcpy(), the size of the destination string should be
large enough to store the copied string. Otherwise, it may result in undefined
behavior.