no warning for assigning unsigned int to plain int

  • Thread starter V.Subramanian, India
  • Start date
V

V.Subramanian, India

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
 
J

James Kuyper

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?
 
K

Kleuskes & Moos

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 )
()~*~()
(_)-(_)
-------------------------------------------------------------------------------
 
I

Ike Naar

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.
 
K

Keith Thompson

Kleuskes & Moos said:
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.
 
K

Keith Thompson

V.Subramanian said:
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.)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top