3.4k views

Consider the following program in C language:

#include <stdio.h>

main()
{
int i;
int*pi = &i;

scanf("%d",pi);
printf("%d\n", i+5);
}

Which one of the following statements is TRUE?

1. Compilation fails.
2. Execution results in a run-time error.
3. On execution, the value printed is $5$ more than the address of variable $i$.
4. On execution, the value printed is $5$ more than the integer value entered.
edited | 3.4k views
0
yes here memory assigned at int i;
+2
Beautiful Question Indeed !!

int i; //i is declared
int*pi = &i;  //pi is a pointer variable
//and is assigned the address of i

scanf("%d",pi); //i is overwritten with the value
//we provided because pi is pointing to i earlier

printf("%d\n", i+5) //it will print the value stored in i+5

input=3; output=8

Option D is answer.

answered by Boss (14.4k points)
selected by
+47
scanf just puts the read value in the address passed to it. Usually we give this address using '&' operator. Here, we use a pointer variable and do this. Result is the same.
0
So, option D is correct :)
0

Ans is not D
tried running on codechef...

https://www.codechef.com/ide

0
why not D)?
+3

@Lokesh (D) is correct. You may see this

+2
Ok (D) is correct but i m confused here

suppose i is at memory location 100

I think *pi has address of i...i.e, 100

and in scanf this 100 will be overwritten and not value of i
0

Lokesh Dafale even codechef gives same ans...D is true...

+9
@Lokesh

U sopposed memory location of i=100 ok

after int *pi=&i;// content of pointer p=100 .

Now

scanf("%d",&i);

&:- is address of operator

&i:-Reads as address of i which is equals to content of pi so we can write them interchangeably it wont effect the result.

Means &i is 100% same as writing only pi and here both returns address of i which is 100

so->>scanf("%d",&i); is same as scanf("%d",pi);

I can move one level more by taking int **ptr=&pi;

and write scanf("%d",*ptr); which is same as above 2 defination of scanf.
+8

one more thing

scanf("%d",____);

this ___ may represent any valid address and here scanf will read an integer variable(due to %d) from user (i.e 10) and make that value(10) as the content of address____

//so as per ur doubt this value 10 will not overwrite with address 100 itself but it will make as the content of the provided address 100...which indirectly happens i=10

Now this ___ address u may write in any allowable way in c be it using address of operator(&) or using pointer.

0

https://gateoverflow.in/user/Rajesh+Pradhan, in your 1 level up question- please explain how correct answer is coming. I'm attaching screenshot of my understanding.

+1
@swati in your image, &ptr = 3000 and ptr = 2000 and hence *ptr = value at 2000 = 1000, so value will be entered at address 1000
0
@Swati Rauniyar, your $scanf("\%d",*ptr)$ is wrong. The question has $scanf("\%d",ptr)$.
0
I don't understand when their is no memory allocated to a variable at declaration time how it is possible to allocate it's address in a pointer
0

sajal singh actually in the 1st line itself i defined.For automatic and register variables, there is no difference between definition and declaration. The process of declaring an automatic or a register variable defines the variable name and allocates appropriate memory. However, for external variables, these two operations may occur independently.

main()
{
int i;//here i stores GARBAGE VALUE
int*pi = &i; //p stores address of i

scanf("%d",pi);// think like that scanf("%d",&i); because pi=&i; so we give value store at i(value at                  address)
printf("%d\n", i+5);//if we give n output n+5
}
answered by Active (3.1k points)
+1
0
There is no problem in the program as pi points to a valid location. Also, in scanf() we pass address of a variable and pi is an address.

So answer is D
answered by Loyal (9.3k points)
#include <stdio.h>
main()
{
int i;
int *pi = &i;  // pi pointing to address of i

scanf("%d", pi); /*  means store the inputted value by scanf at location/address  pointed by pi. i.e at variable int i. earlier int i was holding garbage value. now it will store input value given by you. let say you input the  value of i=10;  */

printf("%d\n", i+5);  //  add 5 in int i.  i.e 10+5 =15
}

so final output will be 5 more than entered value by you.
answered by (301 points)

1
2