error 'for' loop initial declaration used outside c99 mode

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Aloasch
    New Member
    • Nov 2013
    • 1

    #1

    error 'for' loop initial declaration used outside c99 mode

    i have this error while compiling the programm.

    Below is the code and thank zou in advance ;)

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    
    int main()
    {
        char* A = 0;
        char* tmp = 0;
         size_t size =0;
         char c = 0;
        while((c = fgetc(stdin)) != EOF)
        {
             size ++;
            tmp = (char*)malloc(size);
            A = (char*)realloc(A,size); 
            A[size-1] = c; 
            free(tmp);
        }
    
          for( size_t i=size ;i>0;i--)
        {
            printf("%c",A[i]);
        }
        printf("\n");
        free(A);
       
       return 0;
    }
    Last edited by Banfa; Nov 13 '13, 09:40 AM.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Line 20 you declare i as a size_t as part of your loop, C++ allows this and I think C99 allows this but it sounds like you are using a C89 compiler (that is C99 aware) and it doesn't allow this syntax.

    Declare i at the top of the function along with all the other variables (lines 7-10).

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      By the way, in line 11 you set c to the return value of fgetc. c is a char, but fgetc returns an int. Whether EOF fits in a char is compiler dependent. Your code will be more reliable if c were an int. Making this change may provoke some new type warnings that you will have to resolve.

      Comment

      Working...