I am quite confused by the following code:
#include <stdio.h>
#include <stdint.h>
int main(int argc, char ** argv)
{
uint16_t a = 413;
uint16_t b = 64948;
fprintf(stdout, "%u\n", (a - b));
fprintf(stdout, "%u\n", ((uint16_t) (a - b)));
return 0;
}
That returns:
$ gcc -Wall test.c -o test
$ ./test
4294902761
1001
$
It seems that expression (a - b) has type uint32_t. I don't uderstand why since both operators are uint16_t.
Can anyone explain this to me?
C promotes the arguments to
unsigned int
before doing the subtraction. This is standard behavior.See, for instance, In a C expression where unsigned int and signed int are present, which type will be promoted to what type? for details.