Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What is the UTLR of -1?
#1
I'm attempting to determine the essential type of the following expression:

Code:
(unsigned short)1 - (unsigned short)2

This is a operation subject to arithmetic conversion (D.7.9), both operands are essentially unsigned, and the expression is an integer constant expression, so according to item 4.1, the essential type of the result is the UTLR of the result.

However, the result is a signed integer with the value -1. The following program demonstrates this:
Code:
#include <stdio.h>


#define TYPE(x) _Generic((x), \
    int: printf("int\n"), \
    unsigned int: printf("unsigned int\n"), \
    default: printf("other\n") \
)

int main() {
    TYPE(1u - 2u); 
    TYPE(1 - 2); 
    TYPE((unsigned short)1 - (unsigned short)2);
}



So we have to compute the UTLR of a negative value, which is not defined.

Is there an error in my reasoning, or is this a limitation in the essential type system?
Reply
#2
(09-06-2026, 09:36 AM)Loïc Wrote: I'm attempting to determine the essential type of the following expression:

Code:
(unsigned short)1 - (unsigned short)2

This is a operation subject to arithmetic conversion (D.7.9), both operands are essentially unsigned, and the expression is an integer constant expression, so according to item 4.1, the essential type of the result is the UTLR of the result.

However, the result is a signed integer with the value -1.

From a C language perspective, integer promotion will be in effect here, so the result is an int with the value of -1

This has highlighted a gap in the UTLR definition, which the MISRA C WG will consider [ID: 480 applies]
Posted by and on behalf of the MISRA C Working Group
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)