This warning should notify the user that a constant doesn't fit into the expected type.
For instance
char m = 476;
or
double s = 1e456;
This happens also when assigning the result of a function, and in other situations, for instance
_Bool h = 6;
Gcc however will warn sometimes, sometimes not.
Compiling the following program:
char s = 654;
_Bool h = 8;
float fnf(void) { return 1e145; }
_Bool error_p(int a)
{
int m = a != 0;
if (a != 0)
return 7;
return a != 0;
}
will produce the following warnings:
foo.c:1:10: warning: large integer implicitly truncated to unsigned type [-Woverflow]
char s = 654;
^~~
foo.c: In function ‘error_p’:
foo.c:7:6: warning: unused variable ‘m’ [-Wunused-variable]
int m = a != 0;
It detects that the assignment to a char of the number 654 will overflow
but issues an otherwise incomprehensible warning:
"integer implicitely truncated to unsigned type"...
Well at least it says something.
No warnings will be issued for the overflow of assigning a float with a constant that is beyond the dynamic range of the type, nor it will issue
a warnçing when assigning 7 to a bit...
Why is this?
Lcc-win issues a warning for all those cases. I am reviewing the
warnings of lcc-win and I would like to know if other compilers besides
gcc have the same behavior.
The compiler flags I used were:
gcc -Wall -Wpedantic
Note that clang has the same behavvior as gcc but the wording of the
warnings is much clearer:
tretbool.c:1:10: warning: implicit conversion from 'int' to 'char'
changes value from 654 to -114 [-Wconstant-conversion]
char s = 654;
Still, no warnings are issued for the other cases.
_Bool error_p(int a)
{
int m = a != 0;
if (a != 0)
return 7;
return a != 0;
}
[...] nor it will issue a warning when assigning 7 to a bit...
Why is this?
This warning should notify the user that a constant doesn't fit into the >expected type.
On 2018-10-28, jacobnavia <jacob@jacob.remcomp.fr> wrote:
_Bool error_p(int a)
{
int m = a != 0;
if (a != 0)
return 7;
return a != 0;
}
[...] nor it will issue a warning when assigning 7 to a bit...
Why is this?
It is not the case that 7 is assigned to a bit.
The value 7 is converted to _Bool, and the converted value
(here: 1) is assigned to the bit.
For a reference to the standard:
6.3.1.2 Boolean type
1 When any scalar value is converted to _Bool, the result is 0 if the
value compares equal to 0; otherwise, the result is 1.
Newsgroups: comp.lang.c,comp.compilers.lcc
jacobnavia <jacob@jacob.remcomp.fr> writes:
This warning should notify the user that a constant doesn't fit into the
expected type.
What I get here from gcc with my standard options:
warning: overflow in conversion from 'int' to 'char' changes value from '476' to '-36' [-Woverflow]
| char m = 476;
| ^~~
warning: floating constant exceeds range of 'double' [-Woverflow]
| double s = 1e456;
| ^~~~~~
warning: overflow in conversion from 'int' to 'char' changes value from '654' to '-114' [-Woverflow]
| char s1 = 654;
| ^~~
main.c: In function 'fnf':
warning: conversion from 'double' to 'float' changes value from '9.9999999999999999e+144' to '+Inff' [-Wfloat-conversion]
| float fnf( void ){ return 1e145; }
| ^~~~~
Newsgroups: comp.lang.c,comp.compilers.lcc
I used
-Wall -Wpedantic
, please note »-Wconversion« above, which might not be
included within »-Wall«
Le 28/10/2018 à 21:31, Stefan Ram a écrit :
, please note »-Wconversion« above, which might not be
included within »-Wall«
Gosh!
What a bad interface!
So, "all" doesn't mean all actually.
No warnings will be issued for the overflow of assigning a float with a constant that is beyond the dynamic range of the type, nor it will issue
a warnçing when assigning 7 to a bit...
Great!
But... that is the case for ALL constant expressions!
float m = 1e587;
1e587 is converted to float generating +inf, and THAT is stored into "m".
int32_t m = 12345678987654321112221;
The value is converted into int32_t and THAT value is stored into m.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 296 |
Nodes: | 16 (2 / 14) |
Uptime: | 45:52:04 |
Calls: | 6,648 |
Files: | 12,197 |
Messages: | 5,329,775 |