Ad Space
In C, a string is a sequence of characters stored in a character array and always ends with a special null character '\0' that indicates the end of the string.
Strings are widely used for handling names, messages, and text data in C programs.
| One-Dimensional Array | Two-Dimensional Array |
|---|---|
| Stores data in a single row (linear form). | Stores data in rows and columns (matrix form). |
Declared as: datatype arrayName[size]; |
Declared as: datatype arrayName[rows][cols]; |
Accessed using one subscript → a[i] |
Accessed using two subscripts → a[i][j]. |
Example: int marks[5]; |
Example: int matrix[3][3]; |
| Suitable for storing a list of items like marks of students. | Suitable for storing tabular data like a matrix or table. |
An array in C is a collection of elements of the same data type stored in contiguous memory locations. Each element can be accessed using an index.
e.g. int marks[5]; // array of 5 integers
A multi-dimensional array is an array of arrays (2D, 3D, etc.).
General Syntax:
data_type array_name[size1][size2]...[sizeN];
Example (2D array):
int matrix[3][3]; // 3x3 integer matrix
Example (3D array):
int cube[3][3][3]; // 3x3x3 integer cube
Common String Operations in C:
strlen()
strcpy()
strcat()
strcmp()
strrev() (in some compilers)
atoi(), atof(), itoa()
In C, the function strlen() (from <string.h>) is used to find the length of a string. It counts the number of characters excluding the null character '\0'.
Syntax:
int strlen(string_name);
Example Program:
#include <stdio.h>
#include <string.h>
int main() {
char str[20] = "Hello World";
int len;
len = strlen(str);
printf("Length of the string = %d\n", len);
return 0;
}
Output:
Length of the string = 11
Syntax:
data_type array_name[rows][columns];
Example:
int a[3][3];
When declaring an array in C, the declaration contains the following main elements:
int, float, char.marks, arr.[5], [3][3].| One-Dimensional Array (1D) | Two-Dimensional Array (2D) |
|---|---|
| Stores data in a linear (single row) form. | Stores data in a tabular (rows & columns) form. |
Declared as: datatype arrayName[size]; |
Declared as: datatype arrayName[rows][cols]; |
Accessed using one subscript → arr[i]. |
Accessed using two subscripts → arr[i][j]. |
Example: int marks[5]; |
Example: int matrix[3][3]; |
| Suitable for storing a list (e.g., marks of students). | Suitable for storing tables/matrices (e.g., multiplication table). |
Ad Space