educative.io

C: Error: 'for' loop initial declarations are only allowed in C99 or C11 mode

While working on Exercises from the course “Learn C from Scratch” I came across this problem many times. Sometimes the code works fine on my computers Xcode but this error is visible on Educative. What is the issue?

Here is my code:

char * toUpperCase(char * province)
{
  int i=0; 

  while (province[i]!= 0) 
  { 
    if ((province[i] >= 'a') && (province[i] <= 'z')) 
    {
      province[i] = province[i] - 32;
    }

    i++;
  } 
  return province;
}

Here is the error:

main.c: In function ‘toUpperCase’:
main.c:7:3: error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode
for (int c = 0; c < strlen(province); c++){
^
main.c:7:3: note: use option -std=c99, -std=gnu99, -std=c11 or -std=gnu11 to compile your code


Course: Learn C from Scratch - Free Interactive Course
Lesson: Exercises on Strings

Hello @Gosia_Gniadek

The code you provided initially uses a while loop, not a for loop. Your error message refers to a for loop not present in the code you posted.

The code you posted uses a while loop to iterate through the characters of the province string and convert lowercase letters to uppercase. There is no issue with the loop itself.

I test the posted code on the lesson. The error is not generating.
Please have a look at the following screenshot.

Note: I have tested it with and without the #include and all the possible variations.

I hope this will help.
Happy learning.