All Exams Test series for 1 year @ ₹349 only
Question

What is the output of the following C programming language code? 

#include<stdio.h>

 int main()

int x = 10;

 int *p = &x;

 *p = *p + 5;

 printf("%d %d", x, *p); 

return 0; 

}

The correct answer is

15 15

To determine the output of the given C code, let's go through it step by step:

    #include<stdio.h>

    int main() 
    {
        int x = 10;
        int *p = &x;
        *p = *p + 5;
        printf("%d %d", x, *p);
        return 0;
    }
    
  1. The code starts by including the standard input-output header file <stdio.h>, which is essential for using the printf function.
  2. In the main function, an integer variable x is initialized with the value 10.
  3. An integer pointer p is created and initialized with the address of x using the address-of operator &.
  4. The statement *p = *p + 5; is executed. Here, *p dereferences the pointer, accessing the value of x. Thus, *p + 5 results in 10 + 5 = 15. This value is then assigned back to x through the pointer.
  5. The printf("%d %d", x, *p); function call prints the values of x and *p. Since *p is still pointing to x, both x and *p print the value 15.

Therefore, the output of this code will be 15 15.

Conclusion: The correct answer is 15 15. This confirms that the pointer p successfully modified the value of x to 15, and both x and *p reflect this change.

Was this answer helpful?

Important Questions from Pointer

  1. What would be the equivalent pointer expression for referring the array element ar[m][n][o]
  2. Only legal pointer operations:
    A. pointer + number $\rightarrow$ pointer
    B. pointer - number $\rightarrow$ number
    C. pointer + pointer $\rightarrow$ pointer
    D. pointer - pointer $\rightarrow$ pointer
    E. pointer - pointer $\rightarrow$ number
    Choose the most appropriate answer from the options given below:
  3. Consider the following C program:
    #include <stdio.h>
    void stringcopy(char *, char *);
    int main(){
    char a[30] = "@#Hello World!";
    stringcopy(a, a + 2);
    printf("%s\n", a);
    return 0;
    }
    void stringcopy(char *s, char *t) {
    while(*t)
    *s++ = *t++;
    }
    Which ONE of the following will be the output of the program?

  4. Consider the following C program:
    #include <stdio.h>
    int main(){
    int a;
    int arr[5] = {30,50,10};
    int *ptr;
    ptr = & arr[0] + 1;
    a = *ptr;
    (*ptr)++;
    ptr++;
    printf("%d", a + (*ptr) + arr[1]);
    return 0;
    }
    The output of the above program is ____________ (Answer in integer)

  5. Consider the following ANSI-C program.

    #include <stdio.h>
    int main(){
     int *ptr, a, b, c;
     a=5; b=11; c=20;
     ptr=&a; *ptr=c; ptr=&c;
     a=*(&b); c=*ptr-a;
     printf("%d",c);
     return(0);
    }

    The output of this program is ____________. (answer in integer)

    Note: Assume that the program compiles and runs successfully.

Need Expert Advice?
Test Series
RRB JE img
Railways
RRB JE Prev. Yr. Paper (CBT 1 + CBT 2) Test Series
290 Tests 5 Tests Free
4474 Attempts
4.3(88)
English

Start Your Preparation with Prepp Mobile App

Download the app from Google Play & App Store
Download the app from Google Play & App Store
Prepp Mobile App