Declaring Arrays
Part of the Fundamentals section of Coddy's C journey — lesson 53 of 63.
Arrays in C allow you to store multiple values of the same data type under a single variable name.
Declare an integer array with 5 elements:
int numbers[5];This creates an array named numbers that can hold 5 integers.
You can also initialize an array with values when declaring it:
int numbers[5] = {10, 20, 30, 40, 50};If you initialize all elements, you can let C determine the size:
int numbers[] = {10, 20, 30, 40, 50};C will automatically create an array with 5 elements.
Arrays in C are zero-indexed, which means the first element is at index 0.
Challenge
EasyCreate a program that:
- Declares an integer array called
scoreswith 3 elements - Initializes the array with the values 75, 85, and 95
- Prints each element of the array on a new line using
printfand"%d\n"
Cheat sheet
Arrays in C store multiple values of the same data type under a single variable name.
Declare an array:
int numbers[5];Initialize an array with values:
int numbers[5] = {10, 20, 30, 40, 50};Let C determine the size automatically:
int numbers[] = {10, 20, 30, 40, 50};Arrays are zero-indexed - the first element is at index 0.
Try it yourself
#include <stdio.h>
int main() {
// Write your code here
return 0;
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
3Operators
Arithmetic OperatorsModulo OperatorIncrement/DecrementAssignment OperatorsRelational OperatorsLogical Operators Part 1Logical Operators Part 2Logical Operators Part 3Recap Challenge