We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 9
‘nrr2r2028 20.40 CArays
3
w Tutorialsy —Exercisesw Servicese§ QO Log in
schools
C Arrays
[crv] Looe |
Arrays
Arrays are used to store multiple values in a single variable, instead of declaring
separate variables for each value.
To create an array, define the data type (like int ) and specify the name of the array
followed by square brackets [].
To insert values to it, use a comma-separated list, inside curly braces:
int myNumbers[] = {25, 50, 75, 100);
We have now created a variable that holds an array of four integers.
Access the Elements of an Array
To access an array element, refer to its index number.
Array indexes start with 0: [0] is the first element. [1] is the second element, etc,
This statement accesses the value of the first element [0] in myNumbers
Example
nitpsdlmwwadschools.comiele_arays.ahp 9‘nrr2r2028 20.40 CArays
3
w Tutorialsy —Exercisesw Servicese§ QO Log in
schools
Change an Array Element
To change the value of a specific element, refer to the index number:
Example
myNumbers[@] = 33;
Example
int myNumbers[] = {25, 58, 75, 100};
myNumbers[@] = 335
printf("%d", myNumbers[@]);
// Now outputs 33 instead of 25
Loop Through an Array
You can loop through the array elements with the for loop.
The following example outputs all elements in the myNumbers array:
nitpsdlmwwadschools.comiele_arays.ahp
219‘nrr2r2028 20.40 CArays
3
w Tutorialsy —Exercisesw Servicese§ QO Log in
Ane mywuMver >t) = 1429 205 125 a00ss
int i;
for (i = 0; i < 45 itt) {
printf("%d\n", myNumbers[i]);
Set Array Size
Another common way to create arrays, is to specify the size of the array, and add
elements later:
Example
// Declare an array of four integers:
int myNumbers[4];
// Add elements
myNumbers[@] = 253
myNumbers[1] = 50;
myNumbers[2] = 753
myNumbers[3] = 100;
Using this method, you should know the number of array elements in advance,
in order for the program to store enough memory.
You are not able to change the size of the array after creation.
Get Array Size or Length
nitpsdlmwwadschools.comiele_arays.ahp a9‘nrr2r2028 20.40 CArays
3
w Tutorialsy —Exercisesw Servicese§ QO Log in
schools
Laaiipie
int myNumbers[] = {10, 25, 50, 75, 100};
printf("%lu", sizeof(myNumbers)); // Prints 2@
Why did the result show 2@ instead of 5, when the array contains 5 elements?
- It is because the sizeof operator returns the size of a type in bytes.
You learned from the Data Types chapter that an int type is usually 4 bytes, so from
the example above, 4 x 5 (4 bytes x 5 elements) = 20 bytes.
Knowing the memory size of an array is great when you are working with larger
programs that require good memory management.
But when you just want to find out how many elements an array has, you can use the
following formula (which divides the size of the array by the size of one array
element):
Example
int myNumbers[] = (10, 25, 50, 75, 100);
int length = sizeof(myNumbers) / sizeof(myNumbers[@]) ;
printf("%d", length); // Prints 5
Making Better Loops
nitpsdlmwwadschools.comiele_arays.ahp 49‘nrr2r2028 20.40 CArays
3
w Tutorialsy —Exercisesw Servicese§ QO Log in
schools
loops that work for arrays of any size, which is more sustainable.
Instead of writing:
Example
int myNumbers[] = (25, 58, 75, 100}5
int i;
for (i i < 45 ist) {
printf(” myNumbers[i])5
Example
int myNumbers[] = {25, 5@, 75, 100}5
int length = sizeof(myNumbers) / sizeof(myNumbers[@]);
int i;
for (i i < lengths it+) {
printf("%d\n", myNumbers[4]) 5
Real-Life Example
To demonstrate a practical example of using arrays, let's create a program that
calculates the average of different ages:
nitpsdlmwwadschools.comiele_arays.ahp
59‘nrr2r2028 20.40 CArays
3
w Tutorials~ Exercisesw Serviesey§ QO SignUp Login
CSS JAVASCRIPT SQL. PYTHON. «JAVA. PHP. HOWTO.
imi airay suurang warrerene ages
int ages[] = {20, 22, 18, 35, 48, 26, 87, 70};
float avg, sum = 0;
int i;
// Get the length of the array
int length = sizeof(ages) / sizeof(ages[@]);
// Loop through the elements of the array and accumulate the sum
for (int i = 0; i < length; it+) {
sum += ages[i];
// Calculate the average by dividing the sum by the length
avg = sum / length;
// Print the average
printf("The average age is: %.2f", avg);
Try it Yourself »
C Exercises
Test Yourself With Exercises
Exercise:
Create an array of type int called myNumbers .
{25, 50, 75, 100};
htipsuiwaew.wdechools.comicle_arrays.ahp as‘nrr2r2028 20.40 CArays
v’ Tutorials» Exercisese Servieesey§ QO SignUp Login
CSS JAVASCRIPT SQL. PYTHON. «JAVA. Ss PHP, «Ss HOWTO—W3,CSS_—C
Start the Exercise
Log in to track progress
J] &
Earn Your
Certificate Today
And Unlock Tomorrow's
Opportunities!
Cella Tell
Don't let hesitation hold you back.
a “The besttime to starts now.
COLOR PICKER
htipsuiwaew.wdechools.comicle_arrays.ahp 719‘nir212023 2040,
3
WW tutoriatse Exercises» Services»
= CSS JAVASCRIPT SQL
3
WW srsces
GET CERTIFIED
Top Tutorials
HTML Tutorial
SS Tutorial
JavaScript Tutorial
How To Tutorial
‘SQL Tutorial
Python Tutorial
Wa.cSS Tutorial
Bootstrap Tutorial
PHP Tutorial
Java Tutorial
C++ Tutorial
JQuery Tutorial
Top References
HTML Reference
CSS Reference
JavaScript Reference
SQL Reference
Python Reference
W3.CSS Reference
Bootstrap Reference
PHP Reference
HTML Colors
Java Reference
‘Angular Reference
‘JQuery Reference
Top Examples
HTML Examples
SS Examples
“JavaScript Examples
How To Examples
SQL Examples
Python Examples
W2.CSS Examples
Bootstrap Examples
PHP Examples
Java Examples
XML Examples
Query Examples
htipsifwnww.w3schools.comicle_arrays.ohp
corays
Q 0 SignUp Login
JAVA PHP = HOWTO = W3.CSS_—C
UPGRADE NEWSLETTER
REPORT ERROR
Get Certified
HTML Certificate
css certificate
JavaScript Certificate
Front End Certificate
SQL Certificate
Python Certificate
PHP Certificate
Query Certificate
Java Certificate
c++ centficate
# Certificate
XML Certificate
as‘nir212023 2040, CArays
3
WW tutoriaisy — Brercisese Servieesey Q O SignUp Login
schools
CSS JAVASCRIPT SQL. PYTHON = JAVA. PHP-—«S« HOWTO _—W3.CSS.
\W3Schools is optimized for learning and training. Examples might be simplified to
improve reading and learning
Tutorials, references, and examples are constantly reviewed to avold errors, but we
cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted our
terms of use, cookie and privacy policy.
Copyright 1999-2023 by Refsnes Data. All Rights Reserved, W3Schools Is Powered by
W3.CSS,
htipsifwnww.w3schools.comicle_arrays.ohp