+ 1

Hey, I got a doubt. đŸ„ș🙏 What does colon (:) mean in structures? In below program what is colon (:) doing? (Check description)

//Language: C Programming #include <stdio.h> typedef struct { int bit1 : 1; int bit2 : 2; int bit30 : 30; } bits; int main() { printf("%d", sizeof(bits)); return 0; } Please explain me what is the role of colon (:) in structures? I will be really thankful! 🙏đŸ„ș

24th Jan 2021, 8:16 AM
Mahima Rajvir Singh
Mahima Rajvir Singh - avatar
3 Answers
+ 5
you can read more about bits here https://www.bogotobogo.com/cplusplus/quiz_bit_manipulation.php An integer usually has four bytes = 32 bits. You're using one bit of the first int for bit1 and two more bits for bit2. There's 29 bits remaining, but bit3 takes 30 bits. So you need two integers to store all (1+2+30=) 33 bits. Two integers = 2*4 bytes = 8 bytes
24th Jan 2021, 8:47 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 3
Thank you so much get and ♚♚ ! đŸ™ŒđŸ»âœš
24th Jan 2021, 8:59 AM
Mahima Rajvir Singh
Mahima Rajvir Singh - avatar
+ 2
typedef struct { int bit1 : 1; // <int> with 1 bit (automatically converted into <signed char> because less than or equal to 8 bits) int bit2 : 2; // <int> with 2 bits (automatically converted into <signed char> because less than or equal to 8 bits) int bit30 : 30; // <int> with 30 bits (3 bytes and 6 bits) } bits;
24th Jan 2021, 8:35 AM
#0009e7 [get]
#0009e7 [get] - avatar