How can you create a structure that can hold either an integer or a floating-point number, but not both simultaneously?
Answer: B
A union is the appropriate choice when you need to store different data types but only one value at a time.
Q.322Medium
What is the difference between 'struct' and 'typedef struct' in C?
Answer: B
typedef struct creates a type alias, allowing you to declare variables without using the 'struct' keyword each time.
Q.323Medium
What will be the output if you read a union member that was not last written to?
Answer: A
Reading a union member that wasn't last written gives unpredictable results (garbage value or leftover bits from previous member).
Q.324Medium
In a nested structure scenario, how do you access a member of the inner structure?
Answer: C
Use dot operator (.) for direct structure variables and arrow operator (->) for pointers, or combinations for nested access.
Q.325Medium
What is the size of the following structure on a 32-bit system?
struct Point {
char c;
int x;
double y;
}
Answer: D
char(1) + padding(3) + int(4) + double(8) = 16 bytes minimum, but alignment to double boundary makes it 24 bytes.
Advertisement
Q.326Medium
What will be the output of this code?
union Data {
int a;
char b;
};
union Data d;
d.a = 257;
printf("%d %c", d.a, d.b);
Answer: C
Union shares memory. 257 in int format overwrites the char. On a little-endian system, only the lower byte (1) is stored in the char portion.
Q.327Medium
What is the size of the following union?
union Test {
int a[5];
char b[10];
double c;
}
Answer: B
Union size equals the largest member. int[5] = 20 bytes, char[10] = 10 bytes, double = 8 bytes. Maximum is 20 bytes.
Q.328Medium
Consider a structure with bit fields:
struct Flags {
unsigned int flag1 : 1;
unsigned int flag2 : 3;
unsigned int flag3 : 4;
};
What is the minimum size of this structure?
Answer: C
Bit fields must fit within an underlying integer type. Total bits = 8, which requires at least 4 bytes (one int on most systems).
Q.329Medium
What will happen if you try to compare two structures using the == operator directly?
Answer: B
C does not support direct comparison of structures using ==. You must compare members individually or use memcmp().
Q.330Medium
Which of the following is a valid way to pass a structure to a function?
Answer: C
Structures can be passed by value (copying the entire structure) or by pointer (passing address) for efficiency.
Q.331Medium
In a nested structure scenario like struct A contains struct B, how do you access a member of B from A's variable?
Answer: C
If B is embedded in A: a_var.b_var.member. If B is a pointer in A: a_var->b_var->member or a_var.b_ptr->member.
Q.332Medium
What is the purpose of using anonymous structures/unions?
Answer: B
Anonymous structures allow direct member access without needing to reference the nested structure name.
Q.333Medium
Consider this structure: struct Complex { int real; int imag; }; If you want to create an array of 100 such structures, which declaration is correct?
Answer: A
Option A correctly creates a static array of 100 structures. Option B creates a pointer (and allocates incorrect size).
Q.334Medium
Consider the following code:
struct Data {
int x;
char y;
double z;
};
Assuming int=4 bytes, char=1 byte, double=8 bytes, what is the size of struct Data due to padding?
Answer: D
Memory alignment causes padding. x(4) + padding(4) + y(1) + padding(7) + z(8) = 24 bytes. The compiler aligns to the largest member (double = 8 bytes).
Q.335Medium
Which feature in C allows you to define a structure without a tag name for use within another structure?
Answer: B
Anonymous structures allow defining structures without a tag name directly inside another structure, making members accessible as if they belong to the outer structure.
Q.336Medium
In the context of bit fields in structures, what is the maximum number of bits that can be allocated to a single member in standard C?
Answer: C
The maximum number of bits for a bit field member cannot exceed the size of the underlying type. For int (typically 32 bits), you can allocate up to 32 bits to a single member.
Q.337Medium
Which of the following statements about structure initialization in C is INCORRECT?
Answer: B
Members do not need to be explicitly initialized. Uninitialized members in automatic structures have indeterminate values, while static structures have zero-initialized members. Designated initializers (C99+) allow flexible initialization order.
Q.338Medium
What does fgets() function do?
Answer: A
fgets() reads a string from a file until a newline character or end of file is encountered. It takes three arguments: buffer, size, and file pointer.
Q.339Medium
Which function writes formatted data to a file?
Answer: A
fprintf() writes formatted data to a file, similar to printf() but with a file pointer as the first argument.
Q.340Medium
What is the purpose of ftell() function?
Answer: A
ftell() returns the current position of the file pointer in bytes from the beginning of the file.