CKTIS / C
The unsigned short type
unsigned short is an unsigned integer type with a range of at least 0–65535; it need not be exactly 16 bits wide.
Version: C23; repository compiler flag, production runtime not attested
Syntax
unsigned short name;
unsigned short int name;unsigned short and unsigned short int name the same unsigned integer type. Its range is 0 through USHRT_MAX from <limits.h>; USHRT_MAX is at least 65535. Its size need not be two bytes.
Conversion from an integer type to unsigned short reduces the value modulo the mathematical value USHRT_MAX + 1: (unsigned short)-1 yields USHRT_MAX. Conversion of a finite double to unsigned short truncates toward zero and does not wrap. If the truncated value is outside the range of unsigned short, the behavior is undefined. Conversion is not a substitute for input validation.
When integer promotions apply, an unsigned short value is promoted to int if int can represent the entire range of unsigned short, and to unsigned int otherwise. These promotions do not apply in every context. For example, if count has type unsigned short, sizeof(count) gives the size of that type without promoting count. Do not assume every operation happens in unsigned short. Use %hu with printf.
Examples
#include <limits.h>
#include <stdio.h>
int main(void)
{
unsigned short count = 42;
unsigned short wrapped = (unsigned short)-1;
printf("count = %hu\n", count);
printf("wrapped equals USHRT_MAX: %d\n", wrapped == USHRT_MAX);
return 0;
}
Result
count = 42 wrapped equals USHRT_MAX: 1
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.