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;
}
Discussion:
43 comments Page 2 of 5.
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?
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
Why only one argument in the printf gets printed instead of two arguments in your second example?
Some one pls explain
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 ?
ch="%d";
printf(ch,300);
printf("ch\n",300);
printf(ch,"300");
Anyone please explain me how these 3 printf statements differs ?
Alok Kumar said:
1 decade ago
char *ch;
ch="%s\n";
ch++;
ch++;
printf(ch-2,300);
Please explain it.
ch="%s\n";
ch++;
ch++;
printf(ch-2,300);
Please explain it.
PAVIforlove said:
1 decade ago
Assume ch address is 100.
The ch pointer variable store the %d.
In memory location 100.
After increment it would be 101 then 102.
While printing ch-2 means 102-2 then it again pointing 100th location in 100 location it contains "%d" then it will print 300.
The ch pointer variable store the %d.
In memory location 100.
After increment it would be 101 then 102.
While printing ch-2 means 102-2 then it again pointing 100th location in 100 location it contains "%d" then it will print 300.
Srk said:
1 decade ago
Consider str is pointing to 1002.
If we increment it by one 1002+sizeof(char)*1 (size of chat is 2 bytes so it will Point to next location (1004) that is explicitly casted to store char,
( str = (char *)(str+1*sizeof(char)) )
To 1004 contains a garbage value and similarly 1006.
but in printf statement we will make the pointer to point 1002.
If we increment it by one 1002+sizeof(char)*1 (size of chat is 2 bytes so it will Point to next location (1004) that is explicitly casted to store char,
( str = (char *)(str+1*sizeof(char)) )
To 1004 contains a garbage value and similarly 1006.
but in printf statement we will make the pointer to point 1002.
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.
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".
But in case like:
char *str;
str="abc";
printf(str,"mno\n");
The output will be "abc".
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?
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?
Ram said:
1 decade ago
printf is right?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers