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 3 of 3.
Sumanta Roy said:
1 decade ago
Sharaths answer is right.
Piyush said:
1 decade ago
Sharaths answer is simple.
Nita said:
1 decade ago
Chaudhary Paresh is right.
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'
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers