C Programming - Input / Output - Discussion
Discussion Forum : Input / Output - Find Output of Program (Q.No. 8)
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;
}
Discussion:
25 comments Page 1 of 3.
Priyanka said:
1 decade ago
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.
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.
Ashu said:
6 years ago
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.
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.
(4)
Sharath said:
2 decades ago
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.
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.
Anubhav singh said:
1 decade ago
@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.
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.
Chaudhary paresh said:
1 decade ago
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'
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'
Rohit Bachhav said:
2 years ago
@All.
Here is my coding part. Please refer it.
p++ goes to 'd'
2nd p++ goes to '\'
and p-2 goes to '% ' at the starting of the pointer
So,
printf becomes printf("%d\n",23)
it treats %d= format specifier
and \n = newline character
So that's why it prints 23 (int).
Here is my coding part. Please refer it.
p++ goes to 'd'
2nd p++ goes to '\'
and p-2 goes to '% ' at the starting of the pointer
So,
printf becomes printf("%d\n",23)
it treats %d= format specifier
and \n = newline character
So that's why it prints 23 (int).
(3)
Mehul said:
1 decade ago
@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
#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
Balu said:
1 decade ago
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.
Hritik singh said:
8 years ago
Can anyone explain me what exactly happening inside printf statement's braces? Please.
Vishal said:
10 years ago
What about the char type man? How can it change from character to integer?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers