C Programming - Structures, Unions, Enums - Discussion
Discussion Forum : Structures, Unions, Enums - Point Out Errors (Q.No. 3)
3.
Point out the error in the program?
#include<stdio.h>
int main()
{
struct a
{
float category:5;
char scheme:4;
};
printf("size=%d", sizeof(struct a));
return 0;
}
Answer: Option
Explanation:
Bit field type must be signed int or unsigned int.
The char type: char scheme:4; is also a valid statement.
Discussion:
25 comments Page 1 of 3.
Prashant o_O said:
1 decade ago
Bit field can not be used for floating values
it can be used with => signed int ,unsigned int and char
the meaning of "char scheme:4;"
is only 4-bit are reserved for "scheme" not 4-byte(people always make that confusion) here is one example
[1][1][1][1][1][1][1][1] -->8-bit field for char
but for char scheme:4;
the first 4 are not reserved and the last 4 will be reserved for "scheme".
like this (assume 0 for unreserved and 1 for reserved)
[0][0][0][0][1][1][1][1]
it can be used with => signed int ,unsigned int and char
the meaning of "char scheme:4;"
is only 4-bit are reserved for "scheme" not 4-byte(people always make that confusion) here is one example
[1][1][1][1][1][1][1][1] -->8-bit field for char
but for char scheme:4;
the first 4 are not reserved and the last 4 will be reserved for "scheme".
like this (assume 0 for unreserved and 1 for reserved)
[0][0][0][0][1][1][1][1]
Arun Prasad said:
1 decade ago
When we run this program it will show an error that "Bit Field must be an Integral Type". This means that bit fields must be of type signed int or unsigned int and char. We cannot define float data type. Therefore,
int a:3; /* valid */
float b:3; /* Invalid */
One more thing, If we do not mention the type then also it will show an error.
struct a
{ /*
category: 5; Invalid code
scheme: 4; */
}
int a:3; /* valid */
float b:3; /* Invalid */
One more thing, If we do not mention the type then also it will show an error.
struct a
{ /*
category: 5; Invalid code
scheme: 4; */
}
Ritesh_IIIT said:
1 decade ago
@Prashant O_O: Friend I ran this code on gcc compiler after making few changes, here is my code:
#include<stdio.h>
int main()
{
struct a
{
int category:5;
char scheme:4;
};
printf("size=%d", sizeof(struct a));
return 0;
}
And according to your suggestion output must be 9 but it is 8 even take any values in the bit field still the output will be 8. Can you please explain me this.
#include<stdio.h>
int main()
{
struct a
{
int category:5;
char scheme:4;
};
printf("size=%d", sizeof(struct a));
return 0;
}
And according to your suggestion output must be 9 but it is 8 even take any values in the bit field still the output will be 8. Can you please explain me this.
Girish Adhvaitha said:
8 years ago
Float and double will fallow the IEEE-754 standard to store the values into the memory.
If we use that as a bit-field member in a structure. It won't able to follow the method so in big field concept float I avoided to use and also for this reason if you store the same value in float and double, they won't be equal. Because the storage method is different for float and double
Float x=1.1f,
Double y=1.1,
X n't equal to Y.
If we use that as a bit-field member in a structure. It won't able to follow the method so in big field concept float I avoided to use and also for this reason if you store the same value in float and double, they won't be equal. Because the storage method is different for float and double
Float x=1.1f,
Double y=1.1,
X n't equal to Y.
(1)
Monish kawale said:
7 years ago
#Monisha
The bitfield method is to avoid structure padding.
Eg:
Struct monisha
{
Int a: 4
Char b: 3
};
Here the size of the structure is 4 byte as it takes max size data type as int is having 4 bytes which is more than char hence structure takes 4 bytes and in that 4 bytes int a will take 4 bits and char b will take 3 bits.
This is known as but fled to avoid memory leakage.
The bitfield method is to avoid structure padding.
Eg:
Struct monisha
{
Int a: 4
Char b: 3
};
Here the size of the structure is 4 byte as it takes max size data type as int is having 4 bytes which is more than char hence structure takes 4 bytes and in that 4 bytes int a will take 4 bits and char b will take 3 bits.
This is known as but fled to avoid memory leakage.
Anvi said:
9 years ago
1. The amount of memory required to store a structure variable is the sum of the size of all the members.
On the other hand, in the case of unions, the amount of memory required is always equal to that required by its largest member.
2. In the case of structure, each member has their own memory space but In union, one block is used by all the member of the union.
On the other hand, in the case of unions, the amount of memory required is always equal to that required by its largest member.
2. In the case of structure, each member has their own memory space but In union, one block is used by all the member of the union.
Vishalakshi said:
9 years ago
@Professor_X.
As you said, The minimum size of a structure variable will be the size of the biggest data type in the structure.
This sentence is valid only for union not structure because the structure will allocate different memory for different data types. But union allocates memory for the largest data type.
Please correct if I'm wrong. Thank you.
As you said, The minimum size of a structure variable will be the size of the biggest data type in the structure.
This sentence is valid only for union not structure because the structure will allocate different memory for different data types. But union allocates memory for the largest data type.
Please correct if I'm wrong. Thank you.
Professor_x said:
1 decade ago
You can test this.
The minimum size of a structure variable will be the size of the biggest data type in the structure.
i.e.,
In this example (*replace float with int).
sizeof (struct a) will be a minimum of 4 in 32-bit turboc.
Please correct me if I'm wrong.
The minimum size of a structure variable will be the size of the biggest data type in the structure.
i.e.,
In this example (*replace float with int).
sizeof (struct a) will be a minimum of 4 in 32-bit turboc.
Please correct me if I'm wrong.
Chaitu said:
1 decade ago
@Prema.
The main purpose of reserving bits in declaring them is the memory allocation for the variables can be used for what we want.
So by allocating required number of bits, we can save the memory.
The main purpose of reserving bits in declaring them is the memory allocation for the variables can be used for what we want.
So by allocating required number of bits, we can save the memory.
Prema said:
1 decade ago
What is the purpose of reserving the bits for a variable?
As the size of integer is 2 bytes or 4 bytes depending on the compiler why we should reserve 5 bits in this code?
As the size of integer is 2 bytes or 4 bytes depending on the compiler why we should reserve 5 bits in this code?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers