Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #11585 > unrolled thread

no warning for assigning unsigned int to plain int

Started by"V.Subramanian, India" <subramanian100in@yahoo.com>
First post2011-10-12 07:36 -0700
Last post2011-10-12 12:41 -0700
Articles 6 — 5 participants

Back to article view | Back to comp.lang.c


Contents

  no warning for assigning unsigned int to plain int "V.Subramanian, India" <subramanian100in@yahoo.com> - 2011-10-12 07:36 -0700
    Re: no warning for assigning unsigned int to plain int James Kuyper <jameskuyper@verizon.net> - 2011-10-12 12:17 -0400
    Re: no warning for assigning unsigned int to plain int Kleuskes & Moos <kleuske@somewhere.else.net> - 2011-10-12 17:28 +0000
      Re: no warning for assigning unsigned int to plain int Keith Thompson <kst-u@mib.org> - 2011-10-12 12:36 -0700
    Re: no warning for assigning unsigned int to plain int Ike Naar <ike@sverige.freeshell.org> - 2011-10-12 17:53 +0000
    Re: no warning for assigning unsigned int to plain int Keith Thompson <kst-u@mib.org> - 2011-10-12 12:41 -0700

#11585 — no warning for assigning unsigned int to plain int

From"V.Subramanian, India" <subramanian100in@yahoo.com>
Date2011-10-12 07:36 -0700
Subjectno warning for assigning unsigned int to plain int
Message-ID<4075cf5b-5ede-472b-8a8f-aaeca4205c8d@l39g2000pro.googlegroups.com>
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

[toc] | [next] | [standalone]


#11588

FromJames Kuyper <jameskuyper@verizon.net>
Date2011-10-12 12:17 -0400
Message-ID<4E95BD7E.5000502@verizon.net>
In reply to#11585
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?

[toc] | [prev] | [next] | [standalone]


#11592

FromKleuskes & Moos <kleuske@somewhere.else.net>
Date2011-10-12 17:28 +0000
Message-ID<j74in1$pp3$1@dont-email.me>
In reply to#11585
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 )
     ()~*~()   
     (_)-(_)   
-------------------------------------------------------------------------------

[toc] | [prev] | [next] | [standalone]


#11598

FromKeith Thompson <kst-u@mib.org>
Date2011-10-12 12:36 -0700
Message-ID<ln7h4at326.fsf@nuthaus.mib.org>
In reply to#11592
Kleuskes & Moos <kleuske@somewhere.else.net> 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) kst-u@mib.org  <http://www.ghoti.net/~kst>
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#11594

FromIke Naar <ike@sverige.freeshell.org>
Date2011-10-12 17:53 +0000
Message-ID<slrn3vfsj9bl0q.77u.ike@sverige.freeshell.org>
In reply to#11585
On 2011-10-12, V.Subramanian, India <subramanian100in@yahoo.com> 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.

[toc] | [prev] | [next] | [standalone]


#11599

FromKeith Thompson <kst-u@mib.org>
Date2011-10-12 12:41 -0700
Message-ID<ln39eyt2tt.fsf@nuthaus.mib.org>
In reply to#11585
"V.Subramanian, India" <subramanian100in@yahoo.com> 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) kst-u@mib.org  <http://www.ghoti.net/~kst>
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c


csiph-web