For-loop in C | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

For-loop in C

Hello people, why doesn't work this code here, I woulde like to output the elements of an array in a for-loop, but it won't work -------------------------------------------------------- #include <stdio.h> int main() { int ages[] = {31, 18, 24, 55, 29}; for(int i=ages[0]; i<=ages[6]; i++){ printf("%d", i[ages]); } return 0; } --------------------------------------------------- Best regards, Dietmar Erler

19th Dec 2023, 3:02 AM
Didi
Didi - avatar
2 Answers
+ 3
Because in your for loop, you reference i as the value in your list of ages. You need to set i as the index you want to access. Like so: https://sololearn.com/compiler-playground/cGOrRKOvO2wG/?ref=app
19th Dec 2023, 3:16 AM
Slick
Slick - avatar
0
Hello, thank you much for your answer, now does it work. #include <stdio.h> int main() { int ages[] = {31, 18, 24, 55, 29}; for(int i=0; i<=4; i++){ printf("%d ", ages[i]); } return 0; }
19th Dec 2023, 6:16 AM
Didi
Didi - avatar