Solved

weird error in gcc

Posted on 1998-11-12
5
198 Views
Last Modified: 2010-04-15
I compiled the codes below with gcc 2.7.2.3-8

#include <stdio.h>

int main(int argc, char *argv[]) {
 printf ("test");
 int i;  
 for (i = 0; i < 5; i++)
  printf ("%d", i);
 return 0;
}

The error I got:
  In function `main`:
line 6: parse error before int
line 7: i undeclared (first use this function)

HOWEVER, if I move 'int i;' before printf function, I didn't get the error. I never learn ANSI C, does ANSI C say that variable declaration should be done before calling printf function? Also, if I write 'for (int i = 0; ;) I will get errors. So, in C we can't put 'int' inside the for()?

0
Comment
Question by:screwdriver
  • 4
5 Comments
 
LVL 10

Accepted Solution

by:
rbr earned 20 total points
ID: 1254240
change the 2 lines printf and int

int i;
printf ("test");

0
 
LVL 10

Expert Comment

by:rbr
ID: 1254241
You can't use a declaration only after a {
e.g. you can write

int (i=0;i<100;i++) {
      int j=i;
      printf ("%d",j);
}

0
 
LVL 10

Expert Comment

by:rbr
ID: 1254242
Sorry not int (i=0;i<100;i++)
for (i=0;i<100;i++)
a for opens a new block so new local declarations are allowed. There for you can write for (int i...).
It's the same for while, if, switch, ..
You can also write
int main(int argc, char *argv[]) {
     printf ("test");
       {
       int i;   
       for (i = 0; i < 5; i++)
          printf ("%d", i);
       return 0;
     }
}
0
 
LVL 10

Expert Comment

by:rbr
ID: 1254243
Sorry another error. There for you can write for (int i...). (Wrong sentence)
So you can't write for (int i...) Local declarations are only allowed after {.
0
 

Author Comment

by:screwdriver
ID: 1254244
I prefer to hear explanation about ANSI C rule. I have already figured out that I should move int before printf to get rid off the error.
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now