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 1 of 5.
                
                        Vijay said: 
                         
                        2 decades ago
                
                Hi guys,
 
Can anyone give the explanation for this problem?
                Can anyone give the explanation for this problem?
                        Archana said: 
                         
                        2 decades ago
                
                Twice str is incremented n then when we do str-2, it is again pointing to starting position. So str contains "%d\n".
So printf statement would be like printf("%d\n",300);
Hence output will be 300.
                So printf statement would be like printf("%d\n",300);
Hence output will be 300.
                        Sunitha said: 
                         
                        1 decade ago
                
                Thank you very much archana.
                
                        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
                char *str;
str = "%d\n";
str++;
str++;
printf(str-2, 300);
output:300
                        Suresh said: 
                         
                        1 decade ago
                
                Thank you sohan lal mits. There is clearance in your explanation. Thanks a lot.
                
                        Pavan said: 
                         
                        1 decade ago
                
                Thank you sohan and archana.
                
                        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
                then again incremented then it becomes str
then str="%d\n"; then 300 will print
                        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. ?
                
                        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 ?
                Post your comments here:
 
            
        Quick links
                            Quantitative Aptitude
                                    
                                    Verbal (English)
                                    
                                    Reasoning
                                    
                                Programming
                                    
                                    Interview
                                    
                                     Placement Papers