no warning for assigning unsigned int to plain int

Discussion in 'C Programming' started by V.Subramanian, India, Oct 12, 2011.

  1. Consider the following program no_warning.c :

    #include <stdlib.h>
    #include <stdio.h>

    int main()
    {
    size_t length = 4294967295U;
    int size = 4294967295U;

    size = length;

    return EXIT_SUCCESS;
    }

    When I compile this program with
    gcc (GCC) 3.4.3 20050227 (Red Hat 3.4.3-22.1)
    as
    gcc -std=c99 -pedantic -Wall -Wextra no_warning.c
    it did not generate any warning.

    Consider the statements:

    int size = 4294967295U;

    size = length;

    In both these statements, I am assigning 'unsigned int' to 'plain
    int'. Still the compiler accepts them without issuing any warnings. I
    am unable to understand this.

    Please explain.

    Thanks
    V.Subramanian
    V.Subramanian, India, Oct 12, 2011
    #1
    1. Advertising

  2. V.Subramanian, India

    James Kuyper Guest

    On 10/12/2011 10:36 AM, V.Subramanian, India wrote:
    > Consider the following program no_warning.c :
    >
    > #include <stdlib.h>
    > #include <stdio.h>
    >
    > int main()
    > {
    > size_t length = 4294967295U;
    > int size = 4294967295U;
    >
    > size = length;
    >
    > return EXIT_SUCCESS;
    > }
    >
    > When I compile this program with
    > gcc (GCC) 3.4.3 20050227 (Red Hat 3.4.3-22.1)
    > as
    > gcc -std=c99 -pedantic -Wall -Wextra no_warning.c
    > it did not generate any warning.


    6.5.16.1p1 requires that:
    > One of the following shall hold:96)
    > — the left operand has qualified or unqualified arithmetic type and the right has arithmetic type;


    size_t and int are both arithmetic types, so that requirement has been
    met. There's no other relevant constraint on the types, so no diagnostic
    is required. An implementation is allowed to complain about anything it
    wants, but there's no requirement that it complain about this. Why did
    you think that there was?
    James Kuyper, Oct 12, 2011
    #2
    1. Advertising

  3. On Wed, 12 Oct 2011 07:36:00 -0700, V.Subramanian, India wrote:

    > Consider the following program no_warning.c :
    >
    > #include <stdlib.h>
    > #include <stdio.h>
    >
    > int main()
    > {
    > size_t length = 4294967295U;
    > int size = 4294967295U;
    >
    > size = length;
    >
    > return EXIT_SUCCESS;
    > }
    >
    > When I compile this program with
    > gcc (GCC) 3.4.3 20050227 (Red Hat 3.4.3-22.1) as
    > gcc -std=c99 -pedantic -Wall -Wextra no_warning.c
    > it did not generate any warning.
    >
    > Consider the statements:
    >
    > int size = 4294967295U;
    >
    > size = length;
    >
    > In both these statements, I am assigning 'unsigned int' to 'plain int'.
    > Still the compiler accepts them without issuing any warnings. I am
    > unable to understand this.
    >
    > Please explain.


    It should not generate a warning. You invoke an implicit cast from size_t to
    int. This is perfectly acceptable according to the C standard. One of the
    regulars will undoubtedly provide chapter and verse...

    -------------------------------------------------------------------------------
    ________________________________________
    / Once upon a time, four AMPHIBIOUS HOG \
    | CALLERS attacked a family of |
    | DEFENSELESS, SENSITIVE COIN COLLECTORS |
    | and brought DOWN their PROPERTY |
    \ VALUES!! /
    ----------------------------------------
    \
    \
    ___
    {~._.~}
    ( Y )
    ()~*~()
    (_)-(_)
    -------------------------------------------------------------------------------
    Kleuskes & Moos, Oct 12, 2011
    #3
  4. V.Subramanian, India

    Ike Naar Guest

    On 2011-10-12, V.Subramanian, India <> wrote:
    > Consider the following program no_warning.c :
    >
    > #include <stdlib.h>
    > #include <stdio.h>
    >
    > int main()
    > {
    > size_t length = 4294967295U;
    > int size = 4294967295U;
    >
    > size = length;
    >
    > return EXIT_SUCCESS;
    > }
    >
    > When I compile this program with
    > gcc (GCC) 3.4.3 20050227 (Red Hat 3.4.3-22.1)
    > as
    > gcc -std=c99 -pedantic -Wall -Wextra no_warning.c
    > it did not generate any warning.
    >
    > Consider the statements:
    >
    > int size = 4294967295U;
    >
    > size = length;
    >
    > In both these statements, I am assigning 'unsigned int' to 'plain
    > int'. Still the compiler accepts them without issuing any warnings. I
    > am unable to understand this.


    If you're using gcc, you can use the -Wconversion compiler option.
    Ike Naar, Oct 12, 2011
    #4
  5. Kleuskes & Moos <> writes:
    [...]
    > It should not generate a warning. You invoke an implicit cast from size_t to
    > int. This is perfectly acceptable according to the C standard. One of the
    > regulars will undoubtedly provide chapter and verse...


    An implicit *conversion*. A "cast" is an explicit operator that
    specifies a conversion.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Oct 12, 2011
    #5
  6. "V.Subramanian, India" <> writes:
    > Consider the following program no_warning.c :
    >
    > #include <stdlib.h>
    > #include <stdio.h>
    >
    > int main()
    > {
    > size_t length = 4294967295U;
    > int size = 4294967295U;
    >
    > size = length;
    >
    > return EXIT_SUCCESS;
    > }
    >
    > When I compile this program with
    > gcc (GCC) 3.4.3 20050227 (Red Hat 3.4.3-22.1)
    > as
    > gcc -std=c99 -pedantic -Wall -Wextra no_warning.c
    > it did not generate any warning.
    >
    > Consider the statements:
    >
    > int size = 4294967295U;
    >
    > size = length;
    >
    > In both these statements, I am assigning 'unsigned int' to 'plain
    > int'. Still the compiler accepts them without issuing any warnings. I
    > am unable to understand this.


    The language standard doesn't require a diagnostic for a conversion,
    either explicit or implicit, from unsigned int to int. There is no
    violation of any constraint or syntax rule (and no #error directive).
    The standard also doesn't distinguish between warnings and fatal
    errors.

    Of course a compiler can choose to warn about anything it likes.
    And in this case, since the compiler can tell that the value being
    converted is outside the range of the target type, and so the result
    is implementation-defined, a (perhaps optional) warning would be
    quite reasonable. But the standard doesn't require compilers to
    be reasonable.

    (If you want gcc to warn you about this, use the "-Wconversion"
    option. I don't know why "-Wconversion" isn't included in "-Wall"
    or "-Wextra"; that's a gcc question, not a C question.)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Oct 12, 2011
    #6
    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. Timo Freiberger
    Replies:
    3
    Views:
    923
    Bob Hairgrove
    Oct 30, 2004
  2. George Marsaglia

    Assigning unsigned long to unsigned long long

    George Marsaglia, Jul 8, 2003, in forum: C Programming
    Replies:
    1
    Views:
    653
    Eric Sosman
    Jul 8, 2003
  3. er
    Replies:
    6
    Views:
    472
    Andre Kostur
    Sep 14, 2007
  4. ciccio

    int*unsigned int = unsigned?

    ciccio, Jun 4, 2010, in forum: C++
    Replies:
    2
    Views:
    390
    Öö Tiib
    Jun 4, 2010
  5. pozz
    Replies:
    12
    Views:
    712
    Tim Rentsch
    Mar 20, 2011
Loading...

Share This Page