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 2 of 5.

RAJ KISHOR YADAV said:   1 decade ago
#include <stdio.h>
int main()
{

char *str="**********";

int i;
for(i=0;i<=3;i++)
printf("%*..*s\n",30,i,str);
return 0;

}

Hi guys,

Can any explain it?

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 ?

Aditya Pandey said:   8 years ago
Let adress of str =5000.
When it increased twice = 500+(1*4)+1*4)=5008.
But in the next line, there is str-2, it comes back on it's previous location.
So it prints value 300.

Ashlesha said:   1 decade ago
Are you want to say in last step that if str-1 it will pt to 102 and again to 100. That means here compiler will take str-2 is for twice time instead of 2 bytes. , right?

Sahana said:   1 decade ago
printf("%d",300); and print 300.

But in case like:

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

The output will be "abc".

Siya said:   9 years ago
Can we declare print statement like printf(str-2, 300);?

MY question is where is "%c" or " "%s"in printf statement. If it is true then how it works?
(1)

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 ?

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

Santhisuresh said:   1 decade ago
We gave str++ two times it gives str=str+2. In printing we are subtract 2 from str+2(i.e str+2-2). Therefore it prints the same value.

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


Post your comments here:

Your comments will be displayed after verification.