The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
+21 votes

Consider the following program in C language:

#include <stdio.h>

   int i;
   int*pi = &i; 
   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.
asked in Programming by Veteran (99.8k points)
edited by | 2.5k views
yes here memory assigned at int i;
Beautiful Question Indeed !!

3 Answers

+30 votes
Best answer
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.2k points)
selected by
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.
So, option D is correct :)

Ans is not D 
tried running on codechef...

why not D)?

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

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

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


U sopposed memory location of i=100 ok

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



&:- 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.

one more thing


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, in your 1 level up question- please explain how correct answer is coming. I'm attaching screenshot of my understanding.

@swati in your image, &ptr = 3000 and ptr = 2000 and hence *ptr = value at 2000 = 1000, so value will be entered at address 1000
@Swati Rauniyar, your $scanf("\%d",*ptr)$ is wrong. The question has $scanf("\%d",ptr)$.
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
+4 votes
   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.5k points)
+1 vote
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 (8.3k points)

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

38,079 questions
45,572 answers
49,043 users