Warning

A

arunix

hello all;-)
i have a problem here with code.
when i use gcc -Wall -Wextra -ansi -pedantic scnaf_pg.c
then this warning is isuued by the compiler what is this.
i dont know much about C.
Thanks.

warning: ISO C90 forbids mixed declarations and code.

here is the code below.

#include<stdio.h>
#define Lower 1
#define upper 11

int main()
{
int num;
printf("Please Enter Value:- ");
scanf("%d",&num);
int lop,result;
for(lop= Lower; lop!= upper; lop++)
{
result = (lop * num);
printf("%d\n",result);
}
return 0;
}
 
N

Nick Keighley

hello all;-)
yo

i have a problem here with code.
when i use  gcc -Wall -Wextra -ansi -pedantic scnaf_pg.c
then this warning is isuued by the compiler what is this.
i dont know much about C.

look for a good tutorial. K&R is good but works to an older standard.
Thanks.

warning: ISO C90 forbids mixed declarations and code.

compilers do their best to help you! The older C standard is usually
referred to as C89 or C90 (it came out in 1989 and minor mods were
incorporated in 1990). It is telling you that that standard does not
allow you to mix declarations and code. At the head of a block (this
is usually a complete function) come all the declarations, then comes
the code (statements); once you have written a statement you cannot
make any more declarations, in that block.

A newer standard came out in 1999 (so-called C99) that did not have
this restriction. But C99 is less widely impleemnted and hence less
portable.

There is probably some way to tell gcc that you want C99.

Did it not tell you which line the error was on?

here is the code below.

#include<stdio.h>

most people put a space in there
#include said:
#define Lower 1
#define upper 11

bad practice to use lower case l (ell) it is easily confused with 1
(one)
int main()

int main (void)
is better
{
  int num;
<- declaration
  printf("Please Enter Value:- ");
<- statement
  scanf("%d",&num);
  int lop,result;
<- declaration
not allowed in C90
 
I

Ian Collins

hello all;-)
i have a problem here with code.
when i use gcc -Wall -Wextra -ansi -pedantic scnaf_pg.c
then this warning is isuued by the compiler what is this.
i dont know much about C.
Thanks.

warning: ISO C90 forbids mixed declarations and code.

here is the code below.

#include<stdio.h>
#define Lower 1
#define upper 11

int main()
{
int num;
printf("Please Enter Value:- ");
scanf("%d",&num);
int lop,result;

You have mixed declarations with code, as the compiler told you. Use
C99 mode instead.
 
K

Keith Thompson

Nick Keighley said:
bad practice to use lower case l (ell) it is easily confused with 1
(one)

There's no lower case 'l'.

However, it is good practice to use all-caps names for macros:

#define LOWER 1
#define UPPER 11
 
K

Keith Thompson

arunix said:
Thanks for Good Advice

it didnt show any line no for the error.....

That's very surprising. In my experience, gcc *always* shows line
numbers for error messages (when it can). When I compiled your
program, I got:

c.c: In function `main':
c.c:10: warning: ISO C90 forbids mixed declarations and code

Incidentally, I find that warning somewhat misleading. It uses the word
"code" to mean "statements", but I consider declarations to be "code" as
well. A better wording would be "ISO C90 forbids mixed declarations and
statements".
 
A

Andrew Poelstra

That's very surprising. In my experience, gcc *always* shows line
numbers for error messages (when it can). When I compiled your
program, I got:

c.c: In function `main':
c.c:10: warning: ISO C90 forbids mixed declarations and code

Incidentally, I find that warning somewhat misleading. It uses the word
"code" to mean "statements", but I consider declarations to be "code" as
well. A better wording would be "ISO C90 forbids mixed declarations and
statements".

Platform Builder 5 gives you a "missing semicolon" error, if not
something even more insane. It took me 3 hours to figure out the
problem (working on other code without a compiler).

So I like gcc's message.
 
K

Keith Thompson

Andrew Poelstra said:
Platform Builder 5 gives you a "missing semicolon" error, if not
something even more insane. It took me 3 hours to figure out the
problem (working on other code without a compiler).

So I like gcc's message.

Ok, it's better than "missing semicolon", but it could be better still.

Probably Platform Builder 5 only handles C90, and doesn't have a mode in
which it accepts mixed declarations and statements. When it sees a
declaration where it wasn't expecting one, it's simply a syntax error.

gcc, on the other hand, has modes in which it does accept mixed
declarations and statements. Presumably after it sees a declaration
following a statement, it then checks the mode in which it's currently
operating and issues a diagnostic if necessary.

Compilers can produce better error messages if they're aware of common
errors.
 
N

Noob

Keith said:
Incidentally, I find that warning somewhat misleading. It uses the word
"code" to mean "statements", but I consider declarations to be "code" as
well. A better wording would be "ISO C90 forbids mixed declarations and
statements".

Do you think the GCC maintainers would accept a patch for
such a minor improvement ?

(I'm willing to to do it, since it's easy enough.)
 
T

Tom St Denis

Ok, it's better than "missing semicolon", but it could be better still.

Probably Platform Builder 5 only handles C90, and doesn't have a mode in
which it accepts mixed declarations and statements.  When it sees a
declaration where it wasn't expecting one, it's simply a syntax error.

gcc, on the other hand, has modes in which it does accept mixed
declarations and statements.  Presumably after it sees a declaration
following a statement, it then checks the mode in which it's currently
operating and issues a diagnostic if necessary.

Compilers can produce better error messages if they're aware of common
errors.

I agree that they should replace "code" with "statements" but honestly
it'd probably confuse a lot of C developers who aren't that technical
with the language (sad but true...)

Tom
 
B

Ben Bacarisse

Noob said:
Do you think the GCC maintainers would accept a patch for
such a minor improvement ?

Some more data: GCC makes the distinction very clear in some other
cases. For example

label:
int x;

provokes:

error: a label can only be part of a statement and a declaration is
not a statement
 
K

Keith Thompson

Tom St Denis said:
[...]
I agree that they should replace "code" with "statements" but honestly
it'd probably confuse a lot of C developers who aren't that technical
with the language (sad but true...)

Do you really think that a message saying

warning: ISO C90 forbids mixed declarations and statements

would be more confusing than the existing message? I would think
that even a programmer who isn't entirely clear on what the words
"declaration" and "statement" mean could figure it out.
 
K

Keith Thompson

Noob said:
Do you think the GCC maintainers would accept a patch for
such a minor improvement ?

(I'm willing to to do it, since it's easy enough.)

I don't know, but I know how you could find out. :cool:}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top