C Programming: Memory, Pointers and the Stack ยท Arrays and Strings
A fixed-size block of values on the stack. Partly fill one, count its length with sizeof, and find the largest.
#include <stdio.h>
int main(void) {
int marks[5] = {70, 82, 65}; /* the rest are set to 0 */
int count = sizeof(marks) / sizeof(marks[0]);
marks[3] = 90;
int best = marks[0];
for (int i = 1; i < count; i++) {
if (marks[i] > best) {
best = marks[i];
}
}
printf("count=%d best=%d\n", count, best);
return 0;
}marks changes from [70, 82, 65, 0, 0] to [70, 82, 65, 90, 0].best is created on the stack with the value 70.i is created on the stack with the value 1.marks[i] > best to decide which way the code goes.i changes from 1 to 2.marks[i] > best to decide which way the code goes.i changes from 2 to 3.marks[i] > best to decide which way the code goes.i changes from 3 to 4.marks[i] > best to decide which way the code goes.i goes out of scope and disappears.count=5 best=90.0.