C Programming - Structures, Unions, Enums - Discussion
Discussion Forum : Structures, Unions, Enums - Point Out Errors (Q.No. 7)
7.
Point out the error in the program in 16-bit platform?
#include<stdio.h>
int main()
{
struct bits
{
int i:40;
}bit;
printf("%d\n", sizeof(bit));
return 0;
}
Discussion:
19 comments Page 1 of 2.
Puja Bharti said:
10 years ago
i = 40 means 40 is being assigned to i. Whenever i will be used, it will be 40. But i:40 means, we are trying to define i which will store memory of 40 bit which is equal to 5 byte.
So, it will generate an error because maximum size of int datatype is of 4 byte.
So, it will generate an error because maximum size of int datatype is of 4 byte.
(1)
FlameNfire said:
1 decade ago
When we write "int i=40" the compiler does it "int i=0; i=40;" (compiler does it in 2 steps) ,
When we write "int i:40" the compiler just declare an integer and assign 40 to it, which saves time.
When we write "int i:40" the compiler just declare an integer and assign 40 to it, which saves time.
Srikanth atthota said:
8 years ago
We can specify bits up to 16 bits in turbo c and 32 bits in a 32-bit machine. However, 40 bits is not compatible size number of bits with either 16 bit or 32-bit machine it results in an error called BIT FIELD TOO LARGE.
Shalivahana nandish said:
9 years ago
Yes, @Kavi it is possible to assign i value througth structure see given below example.
#include <stdio.h>
int main()
{
struct value
{
int bit1:4;
}bit;
struct value bit2={13};
printf("%d\n", bit2.bit1);
return 0;
}
its given op:-3
Sol: Binary of 13 is 1101(left most bit is 1 considered negative value)
do the 2's comple ment and get the op:
1101
0010 1's complement
+1
0011
-3
#include <stdio.h>
int main()
{
struct value
{
int bit1:4;
}bit;
struct value bit2={13};
printf("%d\n", bit2.bit1);
return 0;
}
its given op:-3
Sol: Binary of 13 is 1101(left most bit is 1 considered negative value)
do the 2's comple ment and get the op:
1101
0010 1's complement
+1
0011
-3
Kavi said:
10 years ago
Suppose i value is assigned in this program.
Is it possible to print I value? If yes how can I write the code? Please anyone explain it.
Is it possible to print I value? If yes how can I write the code? Please anyone explain it.
Varsha Shinde said:
1 decade ago
What is the difference between i = 40 and i:40? Can you just explain it?
Gowtham said:
1 decade ago
int i:40;
40 bit field is required which means 5 bytes of memory.
In a 16-bit machine max int datatype can take 2 bytes of memory.
So is the Error: Bit field too large.
@Sugandh:
int i:2; should work fine.
40 bit field is required which means 5 bytes of memory.
In a 16-bit machine max int datatype can take 2 bytes of memory.
So is the Error: Bit field too large.
@Sugandh:
int i:2; should work fine.
Deepak said:
1 decade ago
What is its mean? please explain it clearly.
Sugandh said:
1 decade ago
I reduced is size to "2".
int i:2, still I am getting the same error.
int i:2, still I am getting the same error.
Prakash said:
1 decade ago
@Flamenfire is wrong here.
int i:40;
is not the same as
int i=40;
On a 16-bit machine, one can declare
int i:16
int i:15
...
int i:1
But
int i:17
int i:18
...
Is not supported on 16-bit machines.
int i:40;
is not the same as
int i=40;
On a 16-bit machine, one can declare
int i:16
int i:15
...
int i:1
But
int i:17
int i:18
...
Is not supported on 16-bit machines.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers