Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   mixed declarations and code (and size_t)? (http://www.velocityreviews.com/forums/t738049-mixed-declarations-and-code-and-size_t.html)

Zach 11-15-2010 08:54 AM

mixed declarations and code (and size_t)?
 
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 */

Ian Collins 11-15-2010 09:01 AM

Re: mixed declarations and code (and size_t)?
 
On 11/15/10 09:54 PM, Zach wrote:
> 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?

--
Ian Collins

Zach 11-15-2010 09:14 AM

Re: mixed declarations and code (and size_t)?
 
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

Malcolm McLean 11-15-2010 09:19 AM

Re: mixed declarations and code (and size_t)?
 
On Nov 15, 11:14*am, Zach <net...@gmail.com> wrote:
> 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.


Jens Thoms Toerring 11-15-2010 09:22 AM

Re: mixed declarations and code (and size_t)?
 
Zach <netrek@gmail.com> wrote:
> 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
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de

Zach 11-15-2010 09:57 AM

Re: mixed declarations and code (and size_t)?
 
Thanks Malcolm and Jens :)

Zach

Eric Sosman 11-15-2010 12:49 PM

Re: mixed declarations and code (and size_t)?
 
On 11/15/2010 3:54 AM, Zach wrote:
> 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).

--
Eric Sosman
esosman@ieee-dot-org.invalid

Keith Thompson 11-15-2010 04:39 PM

Re: mixed declarations and code (and size_t)?
 
Zach <netrek@gmail.com> writes:
[...]
> 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.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


All times are GMT. The time now is 01:01 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.