C Programming - Pointers - Discussion

Discussion Forum : Pointers - Find Output of Program (Q.No. 14)
14.
What will be the output of the program ?
#include<stdio.h>

int main()
{
    char *str;
    str = "%d\n";
    str++;
    str++;
    printf(str-2, 300);
    return 0;
}
No output
30
3
300
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
43 comments Page 4 of 5.

Srbh said:   1 decade ago
char *ch;
ch="%d";
printf(ch,300);
printf("ch\n",300);
printf(ch,"300");

Anyone please explain me how these 3 printf statements differs ?

HarshaN said:   1 decade ago
@Vallabh

Why only one argument in the printf gets printed instead of two arguments in your second example?

Some one pls explain

Mitali said:   1 decade ago
But here the value of str is not replaced by the value enclosed in double quote. So how is this possible?

Vinay said:   1 decade ago
We all know that we cannot substract any integer value in pointer then how can we substract 2 in it please. Any one who know answer this ?

Vallabh said:   1 decade ago
Because it will take it as :

printf("%d",300); and print 300.

But in case like

char *str;
str="abc";
printf(str,"mno\n");

The output will be "abc".

Jasmin said:   1 decade ago
Can any body explain what happened to printf (str-2, 300) why only 300 is answer ? Why the value of str-2 is not printed. ?

Sujith said:   1 decade ago
str-2 one's incremented then it becomes str-1
then again incremented then it becomes str
then str="%d\n"; then 300 will print

Pavan said:   1 decade ago
Thank you sohan and archana.

Suresh said:   1 decade ago
Thank you sohan lal mits. There is clearance in your explanation. Thanks a lot.

Sohan lal mits said:   1 decade ago
We know that when we increament pointer wihh n values like then pointer point to nth value address and whereas pointer decrease with nth value so pointer locate same address as previus so according question
char *str;
str = "%d\n";
str++;
str++;
printf(str-2, 300);
output:300


Post your comments here:

Your comments will be displayed after verification.