Output of C programs | Set 64 (Pointers) - GeeksforGeeks

25 Apr, 2018
Prerequisite : Pointers in CQuestion 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 Bb)B Ac)B Cd)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 errorb) Runtime errorc) 5d) linker error
OUTPUT: (b) Runtime error

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 Valueb) Value Addressc) Address Addressd) 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 errorb) num = 10 address of num = 2293436num = 11 address of num = 2293438c) num = 10 address of num = 2293436num = 11 address of num = 2293440d) num = 10 address of num = 2293436num = 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 valueb) address value valuec) address address addressd) 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.nowk = address of j*k = address of i**k = value of i.
This article is contributed by Abhishek kurmi.
BhanuSashankReddy
C-Output
C-Pointers
C Quiz
MCQ
Program Output
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
C | Structure & Union | Question 10
C | Pointer Basics | Question 15
C | Advanced Pointer | Question 1
C | Structure & Union | Question 4
C | Pointer Basics | Question 1
Practice questions on Height balanced/AVL Tree
Operating Systems | Set 1
Computer Networks | Set 1
Computer Networks | Set 2
Compiler Theory | Set 1