mixed declarations and code (and size_t)?

Z

Zach

I am compiling (gcc) with the options: -ansi -pedantic -Wall -W -
Waggregate-return -Wcast-align -Wcast-qual -Wnested-externs -Wpointer-
arith -Wundef -Wshadow -Wbad-function-cast -Wmissing-prototypes -
Wstrict-prototypes -Wmissing-declarations -Wconversion -Winline

I get the following warning messages on my code:
ll9.c: In function 'main':
ll9.c:30: warning: ISO C90 forbids mixed declarations and code
ll9.c: In function 'process_tokens':
ll9.c:116: warning: ISO C90 forbids mixed declarations and code
ll9.c:119: warning: conversion to 'int' from 'size_t' may change the
sign of the result

It seems to be defaulting to C90 but I did not specify "-std=c90".
Anyways I want to remove these warnings so here is the relevant code:

ll9.c:30 char *delims = {" "};
ll9.c:116 int numchars;
ll9.c:119 numchars = strlen(c);

I use line 30 when I call strtok(). What exactly does "mixed
declarations and code" mean? The result of strlen() is an int so the
warning message doesn't make sense to me. How should I change my code
to remove these warnings?

Zach

http://www.fidei.org /* blog */
http://gytha.oggthebase.org /* a python netrek client */
 
I

Ian Collins

I am compiling (gcc) with the options: -ansi -pedantic -Wall -W -
Waggregate-return -Wcast-align -Wcast-qual -Wnested-externs -Wpointer-
arith -Wundef -Wshadow -Wbad-function-cast -Wmissing-prototypes -
Wstrict-prototypes -Wmissing-declarations -Wconversion -Winline

I get the following warning messages on my code:
ll9.c: In function 'main':
ll9.c:30: warning: ISO C90 forbids mixed declarations and code
ll9.c: In function 'process_tokens':
ll9.c:116: warning: ISO C90 forbids mixed declarations and code
ll9.c:119: warning: conversion to 'int' from 'size_t' may change the
sign of the result

It seems to be defaulting to C90 but I did not specify "-std=c90".
Anyways I want to remove these warnings so here is the relevant code:

ll9.c:30 char *delims = {" "};
ll9.c:116 int numchars;
ll9.c:119 numchars = strlen(c);

Make numchars a size_t.
I use line 30 when I call strtok(). What exactly does "mixed
declarations and code" mean? The result of strlen() is an int so the
warning message doesn't make sense to me. How should I change my code
to remove these warnings?

You haven't really posted enough context.

Why not just compile as C99?
 
Z

Zach

I have removed all the warnings by compiling the code as C99 and
changing:

size_t numchars;
size_t i;

I never used this type before. So int is signed and size_t is
unsigned?

Zach
 
M

Malcolm McLean

I have removed all the warnings by compiling the code as C99 and
changing:

size_t numchars;
size_t i;

I never used this type before. So int is signed and size_t is
unsigned?
Yes. Also, size_t is not necessarily the same width as an int.

I will resist the usual post about size_t.
 
J

Jens Thoms Toerring

Zach said:
I am compiling (gcc) with the options: -ansi -pedantic -Wall -W -
Waggregate-return -Wcast-align -Wcast-qual -Wnested-externs -Wpointer-
arith -Wundef -Wshadow -Wbad-function-cast -Wmissing-prototypes -
Wstrict-prototypes -Wmissing-declarations -Wconversion -Winline
I get the following warning messages on my code:
ll9.c: In function 'main':
ll9.c:30: warning: ISO C90 forbids mixed declarations and code
ll9.c: In function 'process_tokens':
ll9.c:116: warning: ISO C90 forbids mixed declarations and code
ll9.c:119: warning: conversion to 'int' from 'size_t' may change the
sign of the result
It seems to be defaulting to C90 but I did not specify "-std=c90".

'-ansi' is just another way of specifying '-std=c89', so drop that
and use '-std=c99' if you want what gcc implements of C99.
Anyways I want to remove these warnings so here is the relevant code:
ll9.c:30 char *delims = {" "};
ll9.c:116 int numchars;
ll9.c:119 numchars = strlen(c);
I use line 30 when I call strtok(). What exactly does "mixed
declarations and code" mean?

In C89 all variables had to be defined before the first
executable statement, so something like

int i;
i = 3;
int j;

was not allowed. Move the definitions of the variables to the
start of the function (or the block) they belong to) and the
warnings will go away. And, BTW, in

char *delims = {" "};

you don't need any curly braces around the string literal.
The result of strlen() is an int so the warning message
doesn't make sense to me.

No, the return value of strlen() is size_t, i.e. an unsigned
integer type. So the return value has a different signedness
from the type of variable you assign it to. And the range of
values that can be stored in a size_t can be larger than that
of an int. You asked gcc explicitly with '-Wconversion' to
warn you about such implicit conversions of that kind. So make
'numchars' a size_t instead of an int (or, as a worse alter-
native, cast to int) and also that warning should vanish.

Regards, Jens
 
E

Eric Sosman

I am compiling (gcc) with the options: -ansi -pedantic -Wall -W -
Waggregate-return -Wcast-align -Wcast-qual -Wnested-externs -Wpointer-
arith -Wundef -Wshadow -Wbad-function-cast -Wmissing-prototypes -
Wstrict-prototypes -Wmissing-declarations -Wconversion -Winline

I get the following warning messages on my code:
ll9.c: In function 'main':
ll9.c:30: warning: ISO C90 forbids mixed declarations and code
ll9.c: In function 'process_tokens':
ll9.c:116: warning: ISO C90 forbids mixed declarations and code
ll9.c:119: warning: conversion to 'int' from 'size_t' may change the
sign of the result

It seems to be defaulting to C90 but I did not specify "-std=c90".

You specified "-ansi".
Anyways I want to remove these warnings so here is the relevant code:

ll9.c:30 char *delims = {" "};
ll9.c:116 int numchars;
ll9.c:119 numchars = strlen(c);

I use line 30 when I call strtok(). What exactly does "mixed
declarations and code" mean?

It means declarations and executable statements are mixed
together, instead of all declarations preceding all statements.
The result of strlen() is an int so the
warning message doesn't make sense to me.

The result of strlen() is not an int, but a size_t.
How should I change my code
to remove these warnings?

(1) Use command-line flags appropriate to the dialect you want
to compile, and (2) store size_t values in size_t variables. It
would also be a good idea to (3) examine your use of the formerly-
int-now-size_t variables to see whether any tests and so on need
adjustment (for example, a size_t is *always* >=0).
 
K

Keith Thompson

Zach said:
It seems to be defaulting to C90 but I did not specify "-std=c90".

As others have mentioned, yes you did; "-ansi" is synonymous with
"-std=c90" or "-std=c89".
Anyways I want to remove these warnings so here is the relevant code:

ll9.c:30 char *delims = {" "};
ll9.c:116 int numchars;
ll9.c:119 numchars = strlen(c);

I use line 30 when I call strtok(). What exactly does "mixed
declarations and code" mean? The result of strlen() is an int so the
warning message doesn't make sense to me. How should I change my code
to remove these warnings?

The phrase "mixed declarations and code" is a bit misleading, since by
at least one perfectly reasonable meaning of the word "code",
declarations *are* code. What it really means is "mixed decarations
and statements".

What it really means is that in C89/C90, a block (a sequence delimited
by "{" and "}") must contain zero or more declarations followed by zero
or more statements. C99 permits declarations and statements to be
freely mixed.

Note that this restriction in C90 applies only to the top level of a
block. Nested blocks are statements, and they can contain declarations.
For example, the following is valid in both C90 and C99:

#include <stdio.h>
int main (void) {
int x = 10; /* declaration */
printf("x = %d\n", x); /* statement */
{
int y = 20; /* declaration */
printf("y = %d\n", y); /* statement */
}
return 0;
}

But if you remove the inner curly braces, it's legal only in C99.
 

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