Compile with all the warnings enabled, and read what your compiler says:
Georgioss-MacBook-Pro:~ gsamaras$ gcc -Wall main.c
main.c:7:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
main()
^
main.c:9:26: warning: implicit truncation from 'int' to bitfield changes value
from 2 to -2 [-Wbitfield-constant-conversion]
struct st obj={1,2};
^
main.c:11:40: warning: format specifies type 'int' but the argument has type
'unsigned long' [-Wformat]
printf("Size of struct = %d\n",sizeof(obj));
~~ ^~~~~~~~~~~
%lu
3 warnings generated.
Recall, that
a signed 1 bit variable can hold only two values, -1 and 0
as you can see in this answer.
So if you use this struct instead:
struct st
{
int a:2;
int b:3;
};
you will get the desired output.
This answer gives a nice explanation as well.