Why does it throw an error in the seventh round without semicolons (;). When do you need to put such semicolons in the code?
#include <stdio.h>
#define S 10
void print_arr(int *arr, int size) {
int *p = arr + S;
for(;arr < p; arr++) {
printf("%d ", *arr);
}
}
int main() {
int arr[S] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
print_arr(arr, S);
return 0;
}
if you try without a semicolon (;) then it gives the following error
main.c:7:23: error: expected ';' before ')' token
7 | for(arr < p; arr++) {
| ^
| ;
>Solution :
A for loop consists of four parts:
for(init;condition;iteration) { body }
For the compiler to know what is used as init, condition or iteration, you separate it via semicolon. if you want to keep one of the parts empty, you simply write a semicolon.
for(;i < 10;i++) is a loop that has no initialisation. only a condition and an iteration statement.
for(int i= 0;; i++) has no condition and
for(int i=0;i < 10;) has no iteration statement.
writing for(i < 10; i++) is simply not valid syntax.