Output of C programs | Set 31 (Pointers)

19 Apr, 2018
Prerequisite: Pointers in C/C++
Question 1What will be the output?#include<stdio.h>  int main(){     int a[] = { 1, 2, 3, 4, 5} ;    int *ptr;    ptr = a;    printf(" %d ", *( ptr + 1) );      return 0;}output  2
Description:It is possible to assign an array to a pointer. so, when ptr = a; is executed the address of element a[0] is assigned to ptr and *ptr gives the value of element a[0]. When *(ptr + n) is executed the value at the nth location in the array is accessed.Question 2What will be the output?#include<stdio.h>  int main(){     int a = 5;    int *ptr ;    ptr = &a;    *ptr = *ptr * 3;    printf("%d", a);      return 0;}Output: 15
Description:ptr = &a; copies the address of a in ptr making *ptr = a and the statement *ptr = *ptr * 3; can be written as a = a * 3; making a as 15.Question 2What will be the output?     #include<stdio.h>  int main(){    int i = 6, *j, k;    j = &i;    printf("%d\n", i * *j * i + *j);    return 0;}Output:222
    Description:According to BODMAS rule multiplication is given higher priority. In the expression i * *j * i + *j;, i * *j *i will be evaluated first and gives result 216 and then adding *j i.e., i = 6 the output becomes 222.Question 4What will be the output?#include<stdio.h>  int main(){    int x = 20, *y, *z;          // Assume address of x is 500 and     // integer is 4 byte size     y = &x;     z = y;    *y++;     *z++;    x++;    printf("x = %d, y = %d, z = %d \n", x, y, z);    return 0;}Output:x=21 y=504 z=504Description:In the beginning, the address of x is assigned to y and then y to z, it makes y and z similar. when the pointer variables are incremented there value is added with the size of the variable, in this case, y and z are incremented by 4.Question 5What will be the output?#include<stdio.h>  int main(){    int x = 10;    int *y, **z;    y = &x;     z = &y;    printf("x = %d, y = %d, z = %d\n", x, *y, **z);    return 0;}Output:x=10 y=10 z=10Description:*y is a pointer variable whereas **z is a pointer to a pointer variable. *y gives the value at the address it holds and **z searches twice i.e., it first takes the value at the address it holds and then gives the value at that address.
Question 1What will be the output?#include<stdio.h>  int main(){     int a[] = { 1, 2, 3, 4, 5} ;    int *ptr;    ptr = a;    printf(" %d ", *( ptr + 1) );      return 0;}output  2
Description:It is possible to assign an array to a pointer. so, when ptr = a; is executed the address of element a[0] is assigned to ptr and *ptr gives the value of element a[0]. When *(ptr + n) is executed the value at the nth location in the array is accessed.
#include<stdio.h>  int main(){     int a[] = { 1, 2, 3, 4, 5} ;    int *ptr;    ptr = a;    printf(" %d ", *( ptr + 1) );      return 0;}
output
  2

Description:It is possible to assign an array to a pointer. so, when ptr = a; is executed the address of element a[0] is assigned to ptr and *ptr gives the value of element a[0]. When *(ptr + n) is executed the value at the nth location in the array is accessed.
Question 2What will be the output?#include<stdio.h>  int main(){     int a = 5;    int *ptr ;    ptr = &a;    *ptr = *ptr * 3;    printf("%d", a);      return 0;}Output: 15
Description:ptr = &a; copies the address of a in ptr making *ptr = a and the statement *ptr = *ptr * 3; can be written as a = a * 3; making a as 15.
#include<stdio.h>  int main(){     int a = 5;    int *ptr ;    ptr = &a;    *ptr = *ptr * 3;    printf("%d", a);      return 0;}
Output:
 15

Description:ptr = &a; copies the address of a in ptr making *ptr = a and the statement *ptr = *ptr * 3; can be written as a = a * 3; making a as 15.
Question 2What will be the output?     #include<stdio.h>  int main(){    int i = 6, *j, k;    j = &i;    printf("%d\n", i * *j * i + *j);    return 0;}Output:222
    Description:According to BODMAS rule multiplication is given higher priority. In the expression i * *j * i + *j;, i * *j *i will be evaluated first and gives result 216 and then adding *j i.e., i = 6 the output becomes 222.
     #include<stdio.h>  int main(){    int i = 6, *j, k;    j = &i;    printf("%d\n", i * *j * i + *j);    return 0;}
Output:
222
    
Description:According to BODMAS rule multiplication is given higher priority. In the expression i * *j * i + *j;, i * *j *i will be evaluated first and gives result 216 and then adding *j i.e., i = 6 the output becomes 222.
Question 4What will be the output?#include<stdio.h>  int main(){    int x = 20, *y, *z;          // Assume address of x is 500 and     // integer is 4 byte size     y = &x;     z = y;    *y++;     *z++;    x++;    printf("x = %d, y = %d, z = %d \n", x, y, z);    return 0;}Output:x=21 y=504 z=504Description:In the beginning, the address of x is assigned to y and then y to z, it makes y and z similar. when the pointer variables are incremented there value is added with the size of the variable, in this case, y and z are incremented by 4.
#include<stdio.h>  int main(){    int x = 20, *y, *z;          // Assume address of x is 500 and     // integer is 4 byte size     y = &x;     z = y;    *y++;     *z++;    x++;    printf("x = %d, y = %d, z = %d \n", x, y, z);    return 0;}
Output:
x=21 y=504 z=504
Description:In the beginning, the address of x is assigned to y and then y to z, it makes y and z similar. when the pointer variables are incremented there value is added with the size of the variable, in this case, y and z are incremented by 4.
Question 5What will be the output?#include<stdio.h>  int main(){    int x = 10;    int *y, **z;    y = &x;     z = &y;    printf("x = %d, y = %d, z = %d\n", x, *y, **z);    return 0;}Output:x=10 y=10 z=10Description:*y is a pointer variable whereas **z is a pointer to a pointer variable. *y gives the value at the address it holds and **z searches twice i.e., it first takes the value at the address it holds and then gives the value at that address.
#include<stdio.h>  int main(){    int x = 10;    int *y, **z;    y = &x;     z = &y;    printf("x = %d, y = %d, z = %d\n", x, *y, **z);    return 0;}
Output:
x=10 y=10 z=10
Description:*y is a pointer variable whereas **z is a pointer to a pointer variable. *y gives the value at the address it holds and **z searches twice i.e., it first takes the value at the address it holds and then gives the value at that address.
This article is contributed by I.HARISH KUMARs 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
mohit rao 1
C-Output
Program Output
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Runtime Errors
Different ways to copy a string in C/C++
Output of C++ Program | Set 1
Output of Java Program | Set 3
Output of Java Programs | Set 12
Output of C++ programs | Set 47 (Pointers)
Output of C programs | Set 59 (Loops and Control Statements)
Output of Java Program | Set 7
Output of C Programs | Set 3
Output of Java program | Set 15 (Inner Classes)