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 4 of 5.
                
                        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".
                printf("%d",300); and print 300.
But in case like
char *str;
str="abc";
printf(str,"mno\n");
The output will be "abc".
                        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 ?
                
                        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?
                
                        Vijay said: 
                         
                        2 decades ago
                
                Hi guys,
 
Can anyone give the explanation for this problem?
                Can anyone give the explanation for this problem?
                        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".
Post your comments here:
 
            
        Quick links
                            Quantitative Aptitude
                                    
                                    Verbal (English)
                                    
                                    Reasoning
                                    
                                Programming
                                    
                                    Interview
                                    
                                     Placement Papers