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
Solved

weird error in gcc

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Microsoft vcsetup.exe and vcssetup.exe 8 435
Convert image to byte array 8 209
C simple state machine for client server handshake 4 336
Passing a array as parameter - C 2 100
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…
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 how to create, access, and change arrays in the C programming language.

860 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