Solved

weird error in gcc

Posted on 1998-11-12
5
202 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

729 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