#include <iostream>
using namespace std;
struct bitfield
{
unsigned char a : 3, b : 3;
};
int main()
{
bitfield bf;
bf.a = 7;
cout << bf.a;
char c;
cin >> c;
return 0;
}
I am using VC++ with its latest compiler. When i type cast bf.a to int it gives the desired output (7). But when i dont type cast it, it gives no output and gives no errors. Why is it so?
The character (number 7) was written to the console. Character 7 is the bell character.
So you can't see it, but you can hear it. Or rather I could hear the notification sound on Windows 10 when I ran the program.
The same output is generated with:
The bell is part of a group of characters that can be referenced with escape sequences.
Note that in this case the use of a
char
bitfield is not guaranteed by the standard. See here for a question regarding the use ofchar
bitfields.