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.
Ashwani said:
1 decade ago
Can anyone explain this please ?
Jayaram said:
1 decade ago
Explain the concept and how it can be useful in progrm and how the value will get signed or unsigned.
Jyoti said:
1 decade ago
Why char scheme:4; is valid ?
Since it is char of 1 byte field.
Since it is char of 1 byte field.
Santosh said:
1 decade ago
Please someone explain this.
K.maheswari said:
1 decade ago
Any one explain this concept ?
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]
Amit said:
1 decade ago
What do you mean by un/reserved bits?
Rashmi said:
1 decade ago
prashant
very nice explaination...!!
very nice explaination...!!
Swati said:
1 decade ago
How float category: 5 is wrong? can anyone explain 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; */
}
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers