I just don't get it how can we find the relationship between bits and int variable with a number stored in it
First it's important to understand that the number of bits used to store an int
is a fixed number on the specific system being used. Different systems may use a different number of bits.
BUT ... The number of bits doesn't depend on the value held by the int
. The number of bits used to store an int
is a system-specific constant.
You can fid the number of bits like:
printf("int is %zu chars and each char is %d bits so in total %zu bits\n",
sizeof(int), // Chars per int
CHAR_BIT, // Bits per char
sizeof(int) * CHAR_BIT); // Bits per int
On my system I get:
int is 4 chars and each char is 8 bits so in total 32 bits
Once you have calculated the number of bits, you can create an array of that size. Either a VLA (variable length array) or a dynamic allocated array (i.e. using malloc
).