The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
+23 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 (96.1k points)
edited by | 3.4k views
yes here memory assigned at int i;
Beautiful Question Indeed !!

4 Answers

+32 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.4k 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

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.

+9 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.1k points)
best answer
indeed best answer.
+2 votes
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)
0 votes
#include <stdio.h>
    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)

Related questions

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
49,540 questions
54,099 answers
71,007 users