Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   no warning for assigning unsigned int to plain int (http://www.velocityreviews.com/forums/t754887-no-warning-for-assigning-unsigned-int-to-plain-int.html)

V.Subramanian, India 10-12-2011 02:36 PM

no warning for assigning unsigned int to plain int
 
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

James Kuyper 10-12-2011 04:17 PM

Re: no warning for assigning unsigned int to plain int
 
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?

Kleuskes & Moos 10-12-2011 05:28 PM

Re: no warning for assigning unsigned int to plain int
 
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 )
()~*~()
(_)-(_)
-------------------------------------------------------------------------------

Ike Naar 10-12-2011 05:53 PM

Re: no warning for assigning unsigned int to plain int
 
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.

Keith Thompson 10-12-2011 07:36 PM

Re: no warning for assigning unsigned int to plain int
 
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"

Keith Thompson 10-12-2011 07:41 PM

Re: no warning for assigning unsigned int to plain int
 
"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"


All times are GMT. The time now is 04:03 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.