Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why in C without a semicolon gives an error?

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++) {
      |                       ^
      |                       ;

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading