mixed declarations and code (and size_t)?

Discussion in 'C Programming' started by Zach, Nov 15, 2010.

  1. Zach

    Zach Guest

    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 */
     
    Zach, Nov 15, 2010
    #1
    1. Advertising

  2. Zach

    Ian Collins Guest

    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
     
    Ian Collins, Nov 15, 2010
    #2
    1. Advertising

  3. Zach

    Zach Guest

    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
     
    Zach, Nov 15, 2010
    #3
  4. On Nov 15, 11:14 am, Zach <> 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.
     
    Malcolm McLean, Nov 15, 2010
    #4
  5. 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".


    '-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://toerring.de
     
    Jens Thoms Toerring, Nov 15, 2010
    #5
  6. Zach

    Zach Guest

    Thanks Malcolm and Jens :)

    Zach
     
    Zach, Nov 15, 2010
    #6
  7. Zach

    Eric Sosman Guest

    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
    lid
     
    Eric Sosman, Nov 15, 2010
    #7
  8. Zach <> 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) <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"
     
    Keith Thompson, Nov 15, 2010
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    4,483
    Evian Spring
    Jan 24, 2006
  2. Michael Mair
    Replies:
    1
    Views:
    413
    S.Tobias
    Jun 14, 2005
  3. Alex Vinokur
    Replies:
    9
    Views:
    799
    James Kanze
    Oct 13, 2008
  4. Alex Vinokur
    Replies:
    1
    Views:
    580
  5. Keith Thompson

    Mixed declarations and "code"

    Keith Thompson, May 5, 2011, in forum: C Programming
    Replies:
    3
    Views:
    281
Loading...

Share This Page