Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > mixed declarations and code (and size_t)?

Reply
Thread Tools

mixed declarations and code (and size_t)?

 
 
Zach
Guest
Posts: n/a
 
      11-15-2010
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 */
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      11-15-2010
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
 
Reply With Quote
 
 
 
 
Zach
Guest
Posts: n/a
 
      11-15-2010
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
 
Reply With Quote
 
Malcolm McLean
Guest
Posts: n/a
 
      11-15-2010
On Nov 15, 11:14*am, Zach <(E-Mail Removed)> 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.

 
Reply With Quote
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      11-15-2010
Zach <(E-Mail Removed)> 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 ___ http://www.velocityreviews.com/forums/(E-Mail Removed)
\__________________________ http://toerring.de
 
Reply With Quote
 
Zach
Guest
Posts: n/a
 
      11-15-2010
Thanks Malcolm and Jens

Zach
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      11-15-2010
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
(E-Mail Removed)lid
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      11-15-2010
Zach <(E-Mail Removed)> 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) (E-Mail Removed) <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"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Mixed declarations and "code" Keith Thompson C Programming 3 05-08-2011 08:11 AM
Templates: Code organization and forward declarations Rune Allnor C++ 1 09-25-2008 07:58 AM
.NET 2.0 VB Code and C# Code mixed evian_spring@canada.com ASP .Net 3 01-24-2006 12:51 PM
Re: C99 mixed declarations / switch case / weird syntax behavior Michael Mair C Programming 1 06-14-2005 08:23 AM
RE: VS Designer inadvertently changes control declarations in code-beh =?Utf-8?B?WXVyaSBWYW56aW5l?= ASP .Net 0 07-22-2004 09:51 PM



Advertisments