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 2 of 3.
Rahul said:
1 decade ago
What is signed int or unsigned int?
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?
Ashwani said:
1 decade ago
Can anyone explain this please ?
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.
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.
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; */
}
Swati said:
1 decade ago
How float category: 5 is wrong? can anyone explain this?
Rashmi said:
1 decade ago
prashant
very nice explaination...!!
very nice explaination...!!
Amit said:
1 decade ago
What do you mean by un/reserved bits?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers