• AVR Freaks

Compiler bug. if() statement - missing code

Page: < 12 Showing page 2 of 2
Author
1and0
Access is Denied
  • Total Posts : 10223
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Compiler bug. if() statement - missing code 2017/07/03 09:22:27 (permalink)
+1 (1)
pcmgold
4) It also specifically states that "when dealing with signed char = constant of 0xFF  or '\FF' it is to be treated/interpenetrated as -1, and with unsigned char as 255."  This is given as an example in the 'C standard'

Where in the Standard did you quote that phrase from? The constant 0xFF is actually a signed int with value of 0x00FF. The char '\xFF' is a signed char in XC16 that gets promoted to the int 0xFFFF when an operation is performed on it.
 
<edit>
When it (0x00FF or 0xFFFF) is assigned to a char type, the int value is truncated to fit into the char type.
</edit>
 
pcmgold
In the event of a comparison ie if(==) first all the operators undergo the normal conversion (ie highest "rank" as described above ) for the arithmetic comparison.   the 'result' from the test is to be of type int either 1 or 0, true or false.  ( it is the result that is to be promoted/demoted to type int not the the operators being compared.)

No, it is the operands that get promoted, not the result.
post edited by 1and0 - 2017/07/03 09:45:04
#21
jtemples
عُضْوٌ جَدِيد
  • Total Posts : 11524
  • Reward points : 0
  • Joined: 2004/02/13 12:31:19
  • Location: Southern California
  • Status: offline
Re: Compiler bug. if() statement - missing code 2017/07/03 10:30:03 (permalink)
+2 (2)
highest "rank"

 
If you're reading about "rank", you're reading the wrong part of the standard.  Here is how to find the parts of the standard that are relevant to the code you posted:
 
1) Start with the equality operator in 6.5.9.  It tells you, "If both of the operands have arithmetic type, the usual arithmetic conversions are performed."  Clearly, both operands have arithmetic type.
 
2) Now go to 6.3.1.8 to read about the usual arithmetic conversions.  The first few paragraphs talk about floats and doubles which obviously don't apply.  Then comes the critical part: "Otherwise, the integer promotions are performed on both operands. [...] If both operands have the same type, then no further conversion is needed."  So once the integer promotions have been performed, we're done.
 
3) 6.3.1.1 section 2 explains the integer promotions, which tells you "c" is promoted to an int.  0xFF is already an int.
#22
1and0
Access is Denied
  • Total Posts : 10223
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Compiler bug. if() statement - missing code 2017/07/03 10:36:52 (permalink)
0
@jtemples: Any comment on Post #20?
#23
CinziaG
die fucking humans
  • Total Posts : 3145
  • Reward points : 0
  • Joined: 2016/12/07 14:20:36
  • Location: Wien
  • Status: online
Re: Compiler bug. if() statement - missing code 2017/07/03 11:33:17 (permalink)
+1 (1)
1and0
@jtemples: Any comment on Post #20?





I read it as
signed char c=0xff
c promoted = 0xffff
compared to 0xff is false
...

in 2018 you signed for your annihilation. in 2019 it will come ;) I promise
my most wonderful creations here
https://www.youtube.com/c...dPFRvtwsbSTXp6Sk6azGOQ
#24
1and0
Access is Denied
  • Total Posts : 10223
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Compiler bug. if() statement - missing code 2017/07/03 11:53:59 (permalink)
+2 (2)
CinziaG
 
I read it as
signed char c=0xff
c promoted = 0xffff
compared to 0xff is false
...

Yes, it seems counterintuitive.
  char c;
  c = 0xFF;
  if (c == 0xFF) // test is false
  {

#25
jtemples
عُضْوٌ جَدِيد
  • Total Posts : 11524
  • Reward points : 0
  • Joined: 2004/02/13 12:31:19
  • Location: Southern California
  • Status: offline
Re: Compiler bug. if() statement - missing code 2017/07/03 12:01:13 (permalink)
+2 (2)
When you assign 0xFF to c, it takes that bit pattern, which is the bit pattern for -1 in a signed char.  At that point, you have the same scenario as post #1. This might be what the OP was doing, but we still don't know.
#26
NorthGuy
Super Member
  • Total Posts : 5880
  • Reward points : 0
  • Joined: 2014/02/23 14:23:23
  • Location: Northern Canada
  • Status: offline
Re: Compiler bug. if() statement - missing code 2017/07/03 12:47:03 (permalink)
+2 (2)
  char c;
  c = 0xFF;
  if (c == 0xFF) // test is false
  {

 
C is concerned with values, not presentations. 0xff has value 255 and this value is preserved in all promotions and conversions (if possible).
 
"c" (if signed) can hold any value from -128 to +127. It cannot be 255.
 
if "c" was unsigned, the possible values would be between 0 and 255 then the comparison would be true.
 
  int8_t c;
  c = 255; // what? "c" cannot hold this. Are you crazy?
  if (c = 255) // test is false; sure it is false. The maximum value "c" can hold is 127.
  {

#27
jtemples
عُضْوٌ جَدِيد
  • Total Posts : 11524
  • Reward points : 0
  • Joined: 2004/02/13 12:31:19
  • Location: Southern California
  • Status: offline
Re: Compiler bug. if() statement - missing code 2017/07/03 12:50:02 (permalink)
+2 (2)
As far as the claim that there was no warning:
    signed char c = 0xFF;
    if (c == 0xFF) {
warning: comparison is always false due to limited range of data type
That's using -Wall -Wextra.
#28
1and0
Access is Denied
  • Total Posts : 10223
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Compiler bug. if() statement - missing code 2017/07/03 12:59:17 (permalink)
+2 (2)
NorthGuy
C is concerned with values, not presentations. 0xff has value 255 and this value is preserved in all promotions and conversions (if possible).

Agree. If c was signed, c = -1 comparing to 255 would be false. If c was unsigned, c = 255 comparing to 255 would be true.
 

  if (c = 255) // test is false; sure it is false. The maximum value "c" can hold is 127.


That should return true. Smile: Smile
#29
NorthGuy
Super Member
  • Total Posts : 5880
  • Reward points : 0
  • Joined: 2014/02/23 14:23:23
  • Location: Northern Canada
  • Status: offline
Re: Compiler bug. if() statement - missing code 2017/07/03 14:05:09 (permalink)
+1 (1)
1and0
  if (c = 255) // test is false; sure it is false. The maximum value "c" can hold is 127.

That should return true. Smile: Smile



How in the world did I manage to loose the second "="? I was copying and pasting. Must've been the forum software. LoL!
#30
Weydert
Super Member
  • Total Posts : 483
  • Reward points : 0
  • Joined: 2008/07/02 04:22:40
  • Location: Aachen/Germany
  • Status: offline
Re: Compiler bug. if() statement - missing code 2017/07/03 22:35:42 (permalink)
0
 at post #27:
C is concerned with values, not presentations.

 
signed char c;
c=0xFFFF;

main.c:44:5: warning: overflow in implicit constant conversion
(XC16 V1.31)

 
at post #26
my post #19 was only explaining post #1 in my own words, not a separate issue.
Not asking for 'bug or not?'. Only wondering whats going on.
 
at post #28
OK, -Wextra is the trick.
 
edited:
By the way, which GUI option for -Wextra?
 
second edit:
quoting my own post:
I have no C compiler from Microsoft, only C++ 6.0: in this case 0xFF is treated as a byte and not as an integer

No, I'm wrong with this. signed char c is sign extended (to 32 bit for x86/32bit mode) and then compared (32 bit) with 0x000000FF.
 
post edited by Weydert - 2017/07/04 01:53:23

Attached Image(s)

#31
Page: < 12 Showing page 2 of 2
Jump to:
© 2020 APG vNext Commercial Version 4.5