← The C Language
Lesson 21 of 77 · 10 XP
Arrays
Theory and problem statement
Machine translation, not yet reviewed by a native speaker.
An array is several items of the same type, lying in memory one after another:
int nums[5]; // 5 ints — the contents are garbage for now
int primes[] = {2, 3, 5, 7}; // the compiler counts the size (4)
int zeros[100] = {0}; // all items are zeros
Indices go from 0 to size - 1. C doesn't check bounds: nums[10] = 1; compiles and writes 1 into someone else's memory. This is the most famous bug in the history of C, and it's behind many security exploits (buffer overflows).
The size of an array is set when it's declared and never changes after that. The number of items can be calculated like this:
size_t n = sizeof(primes) / sizeof(primes[0]); // 16 / 4 = 4
Looping over it:
for (size_t i = 0; i < n; i++)
printf("%d ", primes[i]);
Task. Read n (no more than 100), then n numbers. Print them in reverse order, separated by spaces.
Sign up for free to solve this task right in your browser: code editor, running, test checks and hints.