Output of C programs | Set 64 (Pointers)

Prerequisite : Pointers in C
Question 1 : What will be the output of following program?
[sourcecode language=”C”]
#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
}
[/sourcecode]
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?

[sourcecode language=”C”]
#include <stdio.h>
int main()
{
int* ptr;
*ptr = 5;
printf("%d", *ptr);
return 0;
}
[/sourcecode]
OPTIONS:
a) compilation error
b) Runtime error
c) 5
d) linker error

OUTPUT: (b) Runtime error

Explanation: Pointer variable (*ptr) cannot be initialized.

Question 3 : What will be the output of following program?

[sourcecode language=”C”]
#include <stdio.h>
int main()
{
int a = 36;
int* ptr;
ptr = &a;
printf("%u %u", *&ptr, &*ptr);
return 0;
}
[/sourcecode]
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?

[sourcecode language=”C”]
#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;
}
[/sourcecode]
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?

[sourcecode language=”C”]
#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;
}
[/sourcecode]
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.

This article is contributed by Abhishek kurmi.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : BhanuSashankReddy



Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.