C Programming - Input / Output - Discussion

8. 

What will be the output of the program ?

#include<stdio.h>

int main()
{
    char *p;
    p="%d\n";
    p++;
    p++;
    printf(p-2, 23);
    return 0;
}

[A]. 21
[B]. 23
[C]. Error
[D]. No output

Answer: Option B

Explanation:

No answer description available for this question.

Yuvaraj said: (Sep 2, 2010)  
Can anyone explain this problem?

Sharath said: (Sep 25, 2010)  
Hi, I think we have to know the pointers first.

In the above program p points to the string "%d\n" and so when the pointer is incremented twice so the pointer is pointed to null position(means it moves to the right of "%d\n").

So in the printf statement the pointer is again moved to the beginning of the string "%d\n"so that the printf statement is similar to printf("%d\n",23);

So the output will be 23.

If any one can explain more detail please do it. Thanks.

Chaudhary Paresh said: (Dec 19, 2010)  
As per my understanding

p="%d\n";
if u print p display one address assume 1000
print *p display =%
p++
address increment 1001x
print *p display =d
p++
address increment 1002
print *p display =\n
after that we print p-2 == means display 1000
but here we write printf(p-2,23) == 23
because printf display last value in argument list
if we write like that print(p-2,23,'C') then display 'C'

Nita said: (Feb 15, 2011)  
Chaudhary Paresh is right.

Piyush said: (Jul 5, 2011)  
Sharaths answer is simple.

Sumanta Roy said: (Jul 12, 2011)  
Sharaths answer is right.

Vijji said: (Nov 12, 2011)  
Chaudhary Paresh answer is simple.

Mukesh Kumar said: (Dec 5, 2011)  
Chaudhary paresh answer is simple and correct.

Frustru Birdy said: (Dec 25, 2011)  
Thanks chaudhary.

Sagar said: (Feb 8, 2012)  
Paresh has given a good explanation for beginers like me.

Mehul said: (Sep 7, 2012)  
@Paresh is wrong

#include<stdio.h>

int main()
{
char *p;
p="%d\n";
p++;
p++;
int i=1;
int j=3;
printf("%d\n", i,j);
return 0;
}
OUTPUT:
1

Dakshya said: (Jul 27, 2013)  
But this program doesn't say that pointer is now pointing on 23.

Balu said: (Aug 2, 2013)  
We must assign some address value to p, but here we are assigning a string. Can anyone explain why it won't report an error and display 23.

Priyanka said: (Aug 3, 2013)  
Hey I don't understand any of your explanations. I guess you guys are complicating it.

As far as I know its just based on pointers concept. Here "p" is a pointer which points to an address.

An address will be stored in the variable p. Let us assume that address to be 1000. in the above program.. the value "%d\n" is assigned to "p".. which means that value is stored in the address 1000.. when we give p++ twice.

The value stored in "p" will get incremented twice.. which means.. p++=1000+1=1001, again p++=1001+1=1002.. so the current value(address) stored in p=1002.."p" is now pointing to the address 1002.

But the value which we assigned "%d\n" is stored in the address 1000.. inorder to make p point to that adress(1000).. we give p-2... where p=1002-2=1000.

Now p will again point to 1000.. so the printf(p-2,23); should be seen like this printf("%d\n",23);... hence it prints the value 23.

Correct me if I'm wrong.

Anubhav Singh said: (Jun 22, 2015)  
@Priyanka.

Your overall answer is correct but there is an error in your explanation, if 1000 is the base address to which p is pointing to i.e., "%" here then p++ will point to "d", and not to the different array, but here for the purpose we need to do p-2,

Consider this program that shows the pointer arithmetic:-

#include<stdio.h>

int main()
{
char *p;
p="%d\n";
p++;
printf("%c",*p);
return 0;
}

Output:- d.

Vishal said: (Jan 13, 2016)  
What about the char type man? How can it change from character to integer?

SALAMUDDIN said: (Jul 25, 2016)  
You're absolutely right @Priyanka.

Ravi Rathore said: (Aug 25, 2016)  
Nice explanation @Priyanka.

Nidipa said: (Nov 3, 2016)  
Please can any one explain clearly? I didn't understand this program.

Vishalakshi said: (Dec 5, 2016)  
Thank you @Abhinav Singh.

Arihant said: (Aug 23, 2017)  
Nice @Priyanka.

Basha said: (Aug 30, 2017)  
Please explain me clearly.

Hritik Singh said: (May 16, 2018)  
Can anyone explain me what exactly happening inside printf statement's braces? Please.

Ashu said: (Jul 26, 2019)  
As far as I know, it's just based on pointers concept. Here "p" is a pointer which points to an address.

An address will be stored in the variable p. Let us assume that address to be 1000. in the above program. The value "%d\n" is assigned to "p" which means that value is stored in the address 1000.. when we give p++ twice.

The value stored in "p" will get incremented twice which means p++=1000+1=1001, again p++=1001+1=1002. So the current value(address) stored in p=1002.."p" is now pointing to the address 1002.

But the value which we assigned "%d\n" is stored in the address 1000 inorder to make p point to that address(1000) we give p-2, where p=1002-2=1000.

Now, p will again point to 1000 so the printf(p-2,23); should be seen like this printf("%d\n",23);. Hence it prints the value 23.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.