Advanced C Interview Questions and Answers for Experienced
1) What will be the output of printf(“%d”)?The ideal form of printf is printf(“%d”,x); where x is an integer variable. Executing this statement will print the value of x. But here, there is no variable is provided after %d so compiler will show garbage value. The reason is a bit tricky.When access specifiers are used in printf function, the compiler internally uses those specifiers to access the arguments in the argument stack. In ideal scenario compiler determines the variable offset based on the format specifiers provided. If we write printf(“%d”, x) then compiler first accesses the first specifier which is %d and depending on the that the offset of variable x in the memory is calculated. But the printf function takes variable arguments.The first argument which contains strings to be printed or format specifiers is mandatory. Other than that, the arguments are optional.So, in case of only %d used without any variable in printf, the compiler may generate warning but will not cause any error. In this case, the correct offset based on %d is calculated by compiler but as the actual data variable is not present in that calculated location of memory, the printf will fetch integer size value and print whatever is there (which is garbage value to us).
2) What is the return values of printf and scanf?The printf function upon successful return, returns the number of characters printed in output device. So, printf(“A”) will return 1. The scanf function returns the number of input items successfully matched and assigned, which can be fewer than the format specifiers provided. It can also return zero in case of early matching failure.3) How to free a block of memory previously allocated without using free?If the pointer holding that memory address is passed to realloc with size argument as zero (like realloc(ptr, 0)) the the memory will be released.4) How can you print a string containing ‘%’ in printf?There are no escape sequence provided for ‘%’ in C. To print ‘%’ one should use ‘%%’, like -printf(“He got 90%% marks in math”);5) What is use of %n in printf()?Ans: According to man page “the number of characters written so far is stored into the integer. indicated by the int * (or variant) pointer argument.“. Meaning if we use it in printf, it will get the number of characters already written until %n is encountered and this number will stored in the variable provided. The variable must be an integer pointer.#include<stdio.h>
main()
{
int c;
printf(“Hello%n world “,&c);
printf(“%d”, c);
}Above program will print ‘Hello world 5 “ as Hello is 5 letter.6) Swap two variables without using any control statement ?We can swap variable using 2 methods. First method is as given below#include <stdio.h>
main()
{
int a = 6;
int b = 10;
a = a + b;
b = a – b;
a = a – b;
printf(“a: %d, b: %d\n”, a, b);
}Second method to swap variables is given below#include <stdio.h>
main()
{
int a = 6;
int b = 10;
a ^= b;
b ^= a;
a ^= b;
printf(“a: %d, b: %d\n”, a, b);
}7) Consider the two structures Struct A and Struct B given below. What will be size of these structures?struct A
{
unsigned char c1 : 3;
unsigned char c2 : 4;
unsigned char c3 : 1;
}a;struct A
{
unsigned char c1 : 3;
unsigned char : 0;
unsigned char c2 : 4;
unsigned char c3 : 1;
}a;The size of the structures will be 1 and 2. In case of first structure, the members will be assigned a byte as follows –
7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
c3 | c2 | c2 | c2 | c2 | c1 | c1 | c1 |
But in case of second structure –
8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
c3 | c2 | c2 | c2 | c2 | c1 | c1 | c1 |
The :0 field (0 width bit field) forces the next bit width member assignment to start from the next nibble. By doing so, the c3 variable will be assigned a bit in the next byte, resulting the size of the structure to 2.8) How to call a function before main()?To call a function pragma startup directive should be used. Pragma startup can be used like this -#pragma startup fun
void fun()
{
printf(“In fun\n”);
}
main()
{
printf(“In main\n”);
}The output of the above program will be -In fun
In mainBut this pragma directive is compiler dependent. Gcc does not support this. So, it will ignore the startup directive and will produce no error. But the output in that case will be -In main9) What is rvalue and lvalue?You can think lvalue as a left side operant in an assignment and rvalue is the right. Also, you can remember lavlue as location. So, the lvalue means a location where you can store any value. Say, for statement i = 20, the value 20 is to be stored in the location or address of the variable i. 20 here is rvalue. Then the 20 = I, statement is not valid. It will result in compilation error “lvalue required” as 20 does not represent any location.10)How to pack a structure?We can pack any structure using __attribute__((__packed__)) in gcc. Example -typdef struct A __attribute __((__packed__))
{
char c;
int i;
}B;
We can also use, pragma pack like this -#pragma pack(1)
typedef struct A
{
char c;
int I;
}B;
In both cases the size of the structure will be 5. But remember, the pragma pack and the other method mentioned, both are compiler dependent.