Inspired from this question, I have:
#include<stdio.h>
struct st
{
int a:1;
int b:2;
};
int main()
{
struct st obj={1, 2};
printf("a = %d\nb = %d\n",obj.a,obj.b);
}
and I get:
Georgioss-MacBook-Pro:~ gsamaras$ gcc -Wall main.c
main.c:10:26: warning: implicit truncation from 'int' to bitfield changes value
from 2 to -2 [-Wbitfield-constant-conversion]
struct st obj={1, 2};
^
1 warning generated.
Georgioss-MacBook-Pro:~ gsamaras$ ./a.out
a = -1
b = -2
I think I understand why both bitfields fail to hold their values (as per this answer), but I don't understand why the compiler warns about 2
only, instead of 1
too! Any ideas?
I am using in my Mac:
Georgioss-MacBook-Pro:~ gsamaras$ gcc -v
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 8.1.0 (clang-802.0.38)
Target: x86_64-apple-darwin16.3.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
In an old Linux system, with gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5), I got no related warning.
In a Debian installation, with gcc version 4.9.2 (Debian 4.9.2-10) , I got no related warning!