struct demo {
unsigned int a : 5;
unsigned int b : 3;
unsigned int c : 4;
unsigned int d : 7;
};
Bit fields in C are packed into the smallest integral type that can accommodate them.
Here, a(5) + b(3) + c(4) + d(7) = 19 bits total.
Since 19 bits exceed 16 bits but fit within 32 bits, the compiler allocates 4 bytes (32 bits) for this structure, following standard packing rules.
The volatile keyword tells the compiler that a variable's value may change at any time (due to external factors like hardware registers, signal handlers, or multi-threading) and should be read from memory every time it's accessed, rather than being optimized by the compiler or cached in a register.
A B-tree of order m has a maximum of m children per node, which means it can have a maximum of (m-1) keys.
Each node can have between ⌈m/2⌉ and m children (for non-leaf nodes), ensuring balance. B-trees are commonly used in database indexing and file systems because they minimize disk I/O operations through their multi-level structure and balanced properties.
#include
int main() {
int a = 5;
printf("%d %d %d", a++, ++a, a);
return 0;
}
This code contains undefined behavior because variable 'a' is modified multiple times (a++, ++a) without intervening sequence points in the same expression. The order of evaluation is unspecified, making the result compiler-dependent.
Both declarations are equivalent. 'const int * const ptr' and 'int const * const ptr' declare a constant pointer to a constant integer. The first const makes the integer constant, the second const makes the pointer constant.
int arr[3][3] = {{1,2,3}, {4,5,6}, {7,8,9}};
int *ptr = (int *)arr;
printf("%d", *(ptr + 5));
A 2D array is stored in row-major order in memory: 1,2,3,4,5,6,7,8,9. When ptr is cast to int*, ptr+5 points to the 6th element (0-indexed), which is 6.
#define MAX 5
int main() {
int arr[MAX];
printf("%d", sizeof(arr)/sizeof(arr[0]));
return 0;
}
In a struct, each member has its own memory allocation, so the total size is the sum of all members. In a union, all members share the same memory location, so the size equals the largest member. Only one member can hold a value at a time in a union.
The pre-increment operator (++x) increments x from 5 to 6 before using its value in printf(). So the output is 6.
ptr[2] is equivalent to *(ptr+2), which dereferences the pointer and returns the value at the third element (index 2).