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 2 of 3.
Vishal said:
10 years ago
What about the char type man? How can it change from character to integer?
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.
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.
Yuvaraj said:
2 decades ago
Can anyone explain this problem?
Dakshya said:
1 decade ago
But this program doesn't say that pointer is now pointing on 23.
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
Sagar said:
1 decade ago
Paresh has given a good explanation for beginers like me.
Frustru birdy said:
1 decade ago
Thanks chaudhary.
Mukesh kumar said:
1 decade ago
Chaudhary paresh answer is simple and correct.
Vijji said:
1 decade ago
Chaudhary Paresh answer is simple.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers