I tried assigning a signed int to unsigned int.
#include <stdio.h>
int main()
{
int a;
unsigned int b;
scanf("%d", &a);
b = a;
printf("%d %u\n", a, b);
return 0;
}
I was hoping that compiling this would cause a warning that I am assigning an int value to unsigned int variable. But I did not get any warning.
$ gcc -std=c99 -Wall -Wextra -pedantic foo.c
$ echo -1 | ./a.out
-1 4294967295
Next I tried to assigning an unsigned int to signed int.
#include <stdio.h>
int main()
{
int a;
unsigned int b;
scanf("%u", &b);
a = b;
printf("%d %u\n", a, b);
return 0;
}
Still no warning.
$ gcc -std=c99 -Wall -Wextra -pedantic bar.c
$ echo 4294967295 | ./a.out
-1 4294967295
Two questions:
- Why are no warnings generated in these cases even though the input gets modified during the conversions?
- Is a type cast necessary in either of the cases?