Arrays in C (Simple Beginner Guide with Examples)
When learning C programming, you will often need to store multiple values.
Instead of creating many variables, C provides a better solution:
👉 Arrays
What is an Array?
An array is a collection of elements of the same type stored in one place.
👉 Example:
Instead of writing:
int a = 10;
int b = 20;
int c = 30;You can write:
int arr[3] = {10, 20, 30};How to Declare an Array
int arr[5];This creates an array that can store 5 integers.
How to Access Array Elements
Each element in an array has an index.
👉 Index always starts from 0
printf("%d", arr[0]); // First elementExample Program
#include <stdio.h>
int main() {
int arr[3] = {10, 20, 30};
for(int i = 0; i < 3; i++) {
printf("%d\n", arr[i]);
}
return 0;
}👉 Output:
10
20
30
Important Points
- Index starts from 0
- All elements must be of same type
- Array size should be fixed
Why Use Arrays?
Arrays help you:
- Store multiple values easily
- Write cleaner code
- Work efficiently with loops
Final Thoughts
Arrays are a very important concept in C.
Once you understand arrays, you can solve many problems easily.
Thanks for reading!
Comments
Post a Comment