2
votes

I created a simple C program that creates a square according to the users input 4 = 4x4, 5 = 5x5 etc.

My program was not compiling correctly like it is below, and after a while I was able to fix it by removing indentation from the second printf.

Is this normal? or is this just my IDE? I'm very new to C but in general I've never seen indentation affect code functionality, so I just wanted to understand a bit more about that.

Thanks!

int main(void)
{
 
 int height;
 
 do 
 {
     
    height = get_int("Height: ");
 
 }
    while (height>9 || height<1);
    
 
for (int i = 0; i < height; i++) 

{
   for (int j = 0; j < height; j++)
   
     printf ("#");
     printf ("\n");

   
   }

}

Error:

clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wshadow    mario.c  -lcrypt -lcs50 -lm -o mario
mario.c:24:6: error: misleading indentation; statement is not part of the previous 'for' [-Werror,-Wmisleading-indentation]
     printf ("\n");
     ^
mario.c:21:4: note: previous statement is here
   for (int j = 0; j : mario] Error 1
4
indentation? In C there are blocks with curly braces so indentation has nothing to do with it. - Frightera
What was the compilation error? - bereal
Can you paste the error message which you get? @Starchest - KarthikNayak98
A good programmer always uses braces. Stops the embarrassment of getting caught with your trousers down. - Ed Heal
@EdHeal The funny part of this question is that OP did not show us the code which triggers this warning :D - 0___________

4 Answers

7
votes

Indentation does not affect the compile result. The "error" clang gives out should be instead a warning, but it becomes an error since clang is asked to do it because it is called with some -W options (probably instructed by the IDE):

-Wall -Werror -Wextra -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wshadow 

and between these ones, -Werror make all warnings into errors:

       -Werror
           Make all warnings into errors.

And other -Wall -Wextra etc. tells the compiler to treat some certain things that are not treated as warnings as warnings.

If -Werror is removed from the option for the compiler commandline options in the IDE, the code can be compiled successfully.

Take a closer look at the error message:

mario.c:24:6: error: misleading indentation; statement is not part of the previous 'for' [-Werror,-Wmisleading-indentation]
     printf ("\n");
     ^

Those ones in the brackets [-Werror,-Wmisleading-indentation] indicates why this comes up. If -Werror is removed, this error will restores to be a warning, and if -Wmisleading-indentation (and maybe -Wall? I am not familiar with warning options) is removed, you won't see any complains about this line, no matter whether -Werror is on.

7
votes

Warning about misleading indentation is not a part of standard C. It is an extension provided by Clang and is enabled by the -Wall switch you used (directly or through a setting in your IDE). Using -Werror elevates the warning to an error.

This is a recent addition to Clang (it is in Clang 12 but not in Apple’s Clang 11), and using -Wall is aggressive, so it may include new warning features in the future.

3
votes

Add curly braces, the compiler is not sure if you mean

for (int j = 0; j < height; j++)
{
   printf ("#");
}
printf ("\n");

or

for (int j = 0; j < height; j++)
{
   printf ("#");
   printf ("\n");
}
1
votes

You did not show us the code which triggers this warning. This warning is emitted to warn the programmer that he probably forgot the braces. It is especially handy for the programmers who save their keyboards and have a "braces" allergy. I personally always put braces. It does not hurt but often it saves hours of debugging.

This code will emit the warning:

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < height; j++)
            printf ("#");
            printf ("\n");
    }

This one will not:

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < height; j++)
            printf ("#");
        printf ("\n");
    }

https://godbolt.org/z/YqqKW8

enter image description here