C Arrays
1. What is an Array?
-
An array is a collection of elements of the same data type stored at contiguous memory locations.
-
Arrays allow you to store multiple values using a single variable name.
Syntax:
-
data_type→ type of elements (int,float,char, etc.) -
array_name→ name of the array -
size→ number of elements (must be a positive integer)
2. Declaring and Initializing an Array
Declaration Only
Declaration with Initialization
-
You can omit the size if initializing:
3. Accessing Array Elements
-
Array elements are accessed using index numbers starting from 0.
Output:
4. Modifying Array Elements
Output:
5. Looping Through an Array
Output:
6. Key Points
-
Array indices start at 0 and go up to
size - 1. -
All elements must be of the same data type.
-
Use loops to process arrays efficiently.
-
Declaring array with size 0 or negative size is invalid.
-
Accessing an index outside the declared size causes undefined behavior.
7. Example – Sum of Array Elements
Output:
