473,320 Members | 1,732 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 ;)

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4.  
  5. int main()
  6. {
  7.     char* A = 0;
  8.     char* tmp = 0;
  9.      size_t size =0;
  10.      char c = 0;
  11.     while((c = fgetc(stdin)) != EOF)
  12.     {
  13.          size ++;
  14.         tmp = (char*)malloc(size);
  15.         A = (char*)realloc(A,size); 
  16.         A[size-1] = c; 
  17.         free(tmp);
  18.     }
  19.  
  20.       for( size_t i=size ;i>0;i--)
  21.     {
  22.         printf("%c",A[i]);
  23.     }
  24.     printf("\n");
  25.     free(A);
  26.  
  27.    return 0;
  28. }
Nov 13 '13 #1
2 1685
Banfa
9,065 Expert Mod 8TB
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).
Nov 13 '13 #2
donbock
2,426 Expert 2GB
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.
Nov 13 '13 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Christophe Grimault | last post by:
Hi all, look at the following code: ////////////////////////// for(int i = 0; i<10; i++) cout << i; for(int i = 0; i<20; i++) cout << i;
32
by: Wenjie | last post by:
Hello, We had a code review with the argument of whether "i" is out of scope as illustrated below: for (int i=0; i<2004; i++) { doSomething(i); }
3
by: zeroDoNotYeSpamtype | last post by:
(I tried to post this earlier, but it seems without success) A similar question to this has been answered many times, to whit the question applying when the index variable of a for loop is...
32
by: Toby Newman | last post by:
At the page: http://www.strath.ac.uk/IT/Docs/Ccourse/subsection3_8_3.html#SECTION0008300000000000000 or http://tinyurl.com/4ptzs the author warns: "The for loop is frequently used, usually...
8
by: Pedro Pinto | last post by:
When compiling my program i got this error: Error: 'for' loop initial declaration used outside c99 mode What is it and how can i solve it? Thanks in advance! Regards
1
by: sam | last post by:
hi all, i am writing some software to model polymerisation kinetics and have created a way of doing so which involves taking ever smaller slices of time to model until some sort of convergence...
5
by: hprYeV | last post by:
I have done a reasonable amount of programming in C++, but the other day I was talking to someone after a lecture in a course on Java who said that they had not been used to the syntax of the Java...
7
by: Ravi | last post by:
I read from the book "The Complete Reference - C" that C99 allows us to declare for loop variables as for(int i=0; i < 10; i++) printf("%d",i); however, my compiler(gcc) gives me the error:...
5
by: sgurukrupagmailcom | last post by:
Hi, I haven't come accross an elegant solution to a design problem that I show below. Have a look at the piece of code here: class Exc { Exc () { System.out.println ("Haribol"); }
2
by: shalskedar | last post by:
In my database if I try to use the Subform Wizard it gives me the following error: "For Loop not initialised" If I try to use any subform in the Main form, it gives me the same error. Is this...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.