Advanced C Interview Questions and Answers for Experienced

    1 Votes

16) What is the difference between const char* p and char const* p?

In const char* p, the character pointed by pointer variable p is constant. This value can not be changed but we can initialize p with other memory location. It means the character pointed by p is constant but not p. In char const* p, the pointer p is constant not the character referenced by it. So we can't assign p with other location but we can change the value of the character pointed by p.

17) What is the point of using malloc(0)?

According to C standard, “ If the size of the space requested is zero, the behavior is implementation defined: either a null pointer is returned, or the behavior is as if the size were some nonzero value, except that the returned pointer shall not be used to access an object”. But there is a benefit of this. The pointer return after malloc(0) will be valid pointer and can be deallocated using free() and it will not crash the program.

18) What is function pointer?

Function pointer, as the name suggests, points to a function. We can declare a function pointer and point to a function. After that using that function pointer we can call that function. Let's say, we have a function Hello which has definition like this -

void Hello(int)

The pointer to that function will look like -

void (*ptr)(int);

Here, ptr is a function pointer that can point to function with no return type and one integer argument. After declaration, we can point to function Hello like this -

void (*ptr)(int) = NULL;
ptr = Hello;

After that we can call that function like this -

(*ptr)(30);

19) Declare a function pointer that points to a function which returns a function pointer ?

To understand this concept, let us look at the following example.

#include<stdio.h>
void Hello();
typedef void (*FP)();

FP fun(int);

main()
{
FP (*ptr)(int) = NULL;
FP p;
ptr = fun;
p = (*fun)(30);
(*p)();
}

void Hello()
{
printf("Hello\n");
}

FP fun(int a)
{
FP p = Hello;
printf("Number is : %d\n", a);
return p;
}

In this program, we have a function Hello with no return type and no argument. The function fun takes an integer argument and returns a function pointer that can point to Hello(). First we have typdef the function pointer which can point to a function with no return type and argument with identifier FP. That way it will be easier to define the required function pointer. If we avoid typedef the required function pointer (ptr) will look like this -

void (*(*ptr)())(int)

20) What is indirection?

In C when we use variable name to access the value it is direct access. If we use pointer to get the variable value, it is indirection.

21) Write a C program to check your system endianness?

#include<stdio.h>
main ()
{
union Test
{
unsigned int i;
unsigned char c[2];
};

union Test a = {300};
if((a.c [0] == 1) &&
(a.c [1] == 44))
{
 printf ("BIG ENDIAN\n");
}
else
{
 printf ("LITTLE ENDIAN\n");
}
}

22) Write a program to get the higher and lower nibble of a byte without using shift operator?

#include<stdio.h>
struct full_byte
{
char first : 4;
char second : 4;
};

union A
{
char x;
struct full_byte by;
};

main()
{
char c = 100;
union A a;
a.x = c;
printf(“the two nibbles are: %d and %d\n”, a.by.first, a.by.second);
}

23) How can you determine the size of an allocated portion of memory?

Ans: We can’t. During malloc or calloc system maintains a list for the pointer allocated and sizeof memory which is used during free(). This is not accessible to user.

24) Guess the output or error:

#include<stdio.h>
#define SQUARE(x) (x)*(x)
main()
{
int i = 5;
printf('%d\n”, SQUARE(++i));
}

Answer will be 49. The macro will be expanded like (++i) * (++i). But the ++ operator has higher precedence. So, the output will be 49.

25) How do you override a defined macro?

We can use #ifdef and #undef preprocessors. It can be done like this -

#ifdef A
#undef A
#endif
#define A 10

Here if macro A is defined it will be undefined using undef and again defined using define.

26) Can math operations be performed on a void pointer?

No. Pointer addition and subtraction means advancing the pointer by a number of elements. But in case of a void pointer, we don't know fpr sure what it's pointing to, so we don't know the size of what it's pointing to. That is why pointer arithmetic can not be used on void pointers.

27) What does the macro #line do?

The macro #line has the capability to change the current line number and optionally the file name returned by the predefined macros __LINE__ and __FILE__. Here also the line number will be incremented automatically after the value is changed using #line. For example, consider the following program.

#include<stdio.h>
main()
{
printf("First: %d\n", __LINE__);
#line 0
printf("Second: %d\n", __LINE__);
printf("Third: %d\n", __LINE__);
}

The output will be -

First: 5 
Second: 0
Third: 1

28) Guess the output or error of the C program given below?

#include<stdio.h>
#define DO_SOMETHING(x) 
int i; 
for(i = 0; i < 10; i++) 
x += i; 

main()
{
int i = 0;
DO_SOMETHING(i);
printf("\n%d", i + printf("1"));
}

This program will generate an compilation error, as the integer variable i is declared twice. The macro DO_SOMETHING will be replaced before the compilation and the result code will have “int i” declaration twice in the program which will cause compilation error.

29) Print the output of this array pointer program ?

#include <stdio.h>
#include <stdio.h> int main(void) { int a[5] = { 1, 2, 3, 4, 5 }; int *ptr = (int*)(&a + 1); printf("%d %d\n", *(a + 1), *(ptr - 1)); return 0; }

Output of this program - 2 5

Here a has type array[5] of int, and &a has type pointer to array[5] of int. So, ptr will yield a pointer to the array[5] of int that comes after a. Subtracting 1 from ptr will point to the last element of a which is 5. Remember, here &a and &a[0] both will point to starting address of the array or the address of the first element. But (&a + 1) points block of memory of size 5 times the sizeof int that comes after the memory block of a, while (&a[0] + 1) return the address of the second element.

30) Print the output of this C program on operator precedence?

#include <stdio.h>
int main(void)
{
int a, b, c, d;
a = 3;
b = 5;
c = a, b;
d = (a, b);
printf("c=%d ", c);
printf("d=%d\n", d);
return 0;
}

Output - 3 5

The comma in C is both a separator as well as an operator. Also, comma operator has the least precedence. In the assignment operation of c, the comma has less priority that assignment operator. So, c = a, b actually evaluates to (c = a), b. But in case of assignment of variable d, the comma operator evaluates both of its operands and returns the value of the second. So, d = (a, b) is equivalent to d = b.

Page 2 of 4

Popular Videos

communication

How to improve your Interview, Salary Negotiation, Communication & Presentation Skills.

Got a tip or Question?
Let us know

Related Articles

C++ Interview Questions and Answers for Freshers, Experienced
Embedded C Interview Questions and Answers
C Interview Questions and Answers