Solved

weird error in gcc

Posted on 1998-11-12
5
196 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops 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.

705 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

20 Experts available now in Live!

Get 1:1 Help Now