Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: no warning for assigning unsigned int to plain int |
| Date | 2011-10-12 12:41 -0700 |
| Organization | None to speak of |
| Message-ID | <ln39eyt2tt.fsf@nuthaus.mib.org> (permalink) |
| References | <4075cf5b-5ede-472b-8a8f-aaeca4205c8d@l39g2000pro.googlegroups.com> |
"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"
Back to comp.lang.c | Previous | Next — Previous in thread | Find similar | Unroll thread
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
csiph-web