S
slougheed
I encountered a problem after we had converted our declarations of
'unsigned short int' to uint16_t. In one instance, whoever did the
conversion failed to delete the 'short' keyword so we had a 'uint16_t
short'. The compiler (GNU 3.3.5) allows this but ignores the original
unsigned keyword and initialzes the variable as a signed short int. I
created a very simple test program just to see what was happening:
#include <stdio.h>
typedef unsigned short my_int16;
int main(int argc, char *argv[]) {
short s = -1;
unsigned short us = -1;
my_int16 my = -1;
my_int16 short mys = -1;
printf("%d, %d, %d, %d\n", s, us, my, mys);
return 0;
}
output is > -1, 65535, 65535, -1
So my "unsigned short short" has a value of -1. The compiler gives a
warning for the declaration of 'us' and 'my' since I'm initializing an
unsigned int to a negative value, but it has no problem with my
declaration of an 'unsigned short short'?
Should this be a warning/error? Why is this allowed?
steve
By the way, I quickly googled this error and I know that I'm not the
only one who has declared a uint16_t short, a few other people will
also see some odd behaviour if they're expecting an unsigned value.
'unsigned short int' to uint16_t. In one instance, whoever did the
conversion failed to delete the 'short' keyword so we had a 'uint16_t
short'. The compiler (GNU 3.3.5) allows this but ignores the original
unsigned keyword and initialzes the variable as a signed short int. I
created a very simple test program just to see what was happening:
#include <stdio.h>
typedef unsigned short my_int16;
int main(int argc, char *argv[]) {
short s = -1;
unsigned short us = -1;
my_int16 my = -1;
my_int16 short mys = -1;
printf("%d, %d, %d, %d\n", s, us, my, mys);
return 0;
}
output is > -1, 65535, 65535, -1
So my "unsigned short short" has a value of -1. The compiler gives a
warning for the declaration of 'us' and 'my' since I'm initializing an
unsigned int to a negative value, but it has no problem with my
declaration of an 'unsigned short short'?
Should this be a warning/error? Why is this allowed?
steve
By the way, I quickly googled this error and I know that I'm not the
only one who has declared a uint16_t short, a few other people will
also see some odd behaviour if they're expecting an unsigned value.