Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

weird error in gcc

Posted on 1998-11-12
5
Medium Priority
?
205 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files 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.
Suggested Courses

618 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