C Language Notes
Unit 3: Arrays and Strings in C
1. Arrays:
- An array is a collection of elements of the same data type stored in contiguous memory
locations.
- Syntax:
data_type array_name[size];
Example:
int arr[5] = {1, 2, 3, 4, 5};
- Types of Arrays:
a. One-dimensional: Linear collection of elements.
b. Multi-dimensional: Example is a 2D array for matrices.
- Array operations:
- Traversing: Accessing elements using index.
- Updating: Modifying elements.
2. Strings:
- A string is an array of characters ending with a null character '\0'.
- Declaration:
char str[50] = "Hello";
- Common string functions (from <string.h>):
- strlen(): Finds the length of the string.
Page 1
C Language Notes
- strcpy(): Copies one string to another.
- strcat(): Concatenates two strings.
- strcmp(): Compares two strings.
Understanding arrays and strings is essential for data manipulation and handling.
Page 2