search
Log In
3 votes
259 views

https://gateoverflow.in/?qa=blob&qa_blobid=14433986388826671915

int main()
{
    int a = 10;
    int *b = &a;
    scanf("%d",b);
    printf("%d",a+50);
}

What will be the Output of the following code if input given is $25$ ?

in Programming
edited by
259 views

4 Answers

1 vote
int main() {
 int a=10;            // value of a =10;
 int *b=&a;          // pointer b stores the address of a i.e. points to a
 scanf("%d",b);     // b gets a value from user(25) and stores it in a since b points to a
 printf("%d",a+50);  // here a = (value which user gave + 50)= 25+50 = 75
 return 0;
}

$\therefore$ here value of $a$ = (value which user gave + 50) = 25+50 = $75$ will be printed as output.

0
I'm having a doubt that b contains address of a than how can we update the value of a as=25 rather than not updating any value
Plz help with an approach for my doubt.
1

scanf reads data from stdin(here it is 25) and stores them according to the parameter format (here it is %d) into the locations pointed by the additional arguments.(i.e stores in location pointed by b)

we generally write scanf as scanf("%d" , &a); and here b = &a;

So we can write scanf("%d",b);

0 votes

Output 75

 

0 votes
The output will be 75 if input given is 25
0 votes
Here the answer will be 75 because here you are putting the address of a in scanf. so, when scanf is called it will read the input value to the memory location a. so, in a instead of 10 25 will be stored and when 25 is added to 50 we will get answer as 75 which is printed as given in the code.

Related questions

1 vote
1 answer
1
210 views
$1)$ How to access array element with array of pointers? By pointer to an array we can access like this $(*a)[0]$,$(*a)[22]$, .. like this right? but how with array of pointer? $2)$Check these three codes , one of them use array and one of them use array of pointer, and 3rd one ... **ptr[12]; int main(void) { if(*(ptr+5)==*(ptr+3)){ printf("Equal"); } else{ printf("Not Equal"); } return 0; }
asked May 5, 2019 in Programming srestha 210 views
0 votes
1 answer
2
270 views
what is wrong with this code? It shows segmentation fault #include<stdio.h> #include<stdlib.h> void main() { int i,j,count=1; int **a=(int**)malloc(3*sizeof(int*)); for(i=0; i<3; i++) { a[i]=(int*)malloc(4*sizeof(int)); } for(i=0; i<3; i++) { for(j=0; j<4; j++) { a[i][j]=count; count++; } } printf("\n"); for(i=0; i<3; i++) { for(j=0; j<4; j++) { printf("%d ",a[i][j]); } printf("\n"); } }
asked Sep 13, 2018 in Programming aditi19 270 views
0 votes
1 answer
3
188 views
give the complete solution with explanation int main() { int arr [2] [2] [2] = {10,2,3,4,5,6,7,8}; int *p, *q; p = &arr[1] [1] [1]; q = (int*) arr; printf("%d ,%d \n",*p ,*q); return 0; }
asked Aug 28, 2018 in Programming shivam sharma 5 188 views
0 votes
1 answer
4
73 views
What is a variable pointer? please note the question is not asking about pointer variables, but the specific term.
asked Aug 7, 2018 in Programming Prakhar Yadav 1 73 views
...