Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compilation error in C just because of indentation?

Tags:

c

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
like image 887
Starchest Avatar asked Jan 26 '26 23:01

Starchest


1 Answers

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.

like image 186
Eric Postpischil Avatar answered Jan 28 '26 16:01

Eric Postpischil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!