Prerequisite : Pointers in C Question 1 : What will be the output of following program?
#include "stdio.h" int main()
{ char a[] = { 'A' , 'B' , 'C' , 'D' };
char * ppp = &a[0];
*ppp++; // Line 1
printf ("%c %c ", *++ppp, --*ppp); // Line 2
} |
OPTIONS: a)C B b)B A c)B C d)C A
OUTPUT: (d) C A
Explanation: Line 1 : Now, ppp points to next memory location i.e., index 1 of the character array. Line 2 : Firstly, –*ppp= –(*ppp) is executed and hence the value ‘B’ (which is in the index 1 position of the char[] array) gets decremented by 1(i.e., it becomes ‘A’)and it is sent for printing. Then *++ppp= *(++ppp) is executed which initially increments the pointer to the next element of the array and prints the value in that index number 2 which is ‘C’. Although –*ppp is executed first compared to *++ppp, the display will be shown in the order as we mentioned in the printf() function in line 2. Hence we get output as C A. Question 2 : What will be the output of following program?
#include <stdio.h> int main()
{ int * ptr;
*ptr = 5;
printf ("%d", *ptr);
return 0;
} |
OPTIONS: a) compilation error b) Runtime error c) 5 d) linker error
OUTPUT: c 5
Explanation: Pointer variable (*ptr) cannot be initialized. Question 3 : What will be the output of following program?
#include <stdio.h> int main()
{ int a = 36;
int * ptr;
ptr = &a;
printf ("%u %u", *&ptr, &*ptr);
return 0;
} |
OPTIONS: a) Address Value b) Value Address c) Address Address d) Compilation error
OUTPUT: (c)Address Address
Explanation: & and * cancelled each other and display the address stored in a pointer variable ptr i.e) the address of a. Question 4 : What will be the output of following program?
#include <stdio.h> int main()
{ int num = 10;
printf ("num = %d addresss of num = %u", num, &num);
num++;
printf ("\n num = %d addresss of num = %u", num, &num);
return 0;
} |
OPTIONS: a) Compilation error b) num = 10 address of num = 2293436 num = 11 address of num = 2293438 c) num = 10 address of num = 2293436 num = 11 address of num = 2293440 d) num = 10 address of num = 2293436 num = 11 address of num = 2293436
OUTPUT: (d)
Explanation: Here a variable num holds the value 10 and get its address as 2293436, then increment is done as num++ which uses the same address space to store the incremented value. Question 5 : What will be the output of following program?
#include <stdio.h> int main()
{ int i = 25;
int * j;
int ** k;
j = &i;
k = &j;
printf ("%u %u %u ", k, *k, **k);
return 0;
} |
OPTIONS: a) address address value b) address value value c) address address address d) compilation error
OUTPUT : (a)address address value
Explanation : Here a pointer variable *j hold the address of i and then another pointer variable *k hold the address of j. now k = address of j *k = address of i **k = value of i.