569 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$ ?

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.

by

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.

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);

Output 75

The output will be 75 if input given is 25
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.