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.
                
                        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?
                
                        Vijay said: 
                         
                        1 decade ago
                
                Step->1 char*str;
Here str is the char pointer.
Str has own address. Assume i.e. 500.
Step->2 str = "%d\n";
That mean str has at 500 = "%d\n".
Step->3 str++;
Address incremented by 1 because str is char pointer i.e new address will be 501(500+1).
Step->4 str++; Similarly again increment by 1.
Address will be 502 (501+1).
Step->5 printf(str-2, 300);
Here 1st will be evaluate str-2 then print.
Str-2 means decrements two times. Pointer will be at starting location, i.e 500.
Now printf will be like this printf ("%d\n", 300). Then finally print 300.
Step->6 return 0; means program execute successfully.
                Here str is the char pointer.
Str has own address. Assume i.e. 500.
Step->2 str = "%d\n";
That mean str has at 500 = "%d\n".
Step->3 str++;
Address incremented by 1 because str is char pointer i.e new address will be 501(500+1).
Step->4 str++; Similarly again increment by 1.
Address will be 502 (501+1).
Step->5 printf(str-2, 300);
Here 1st will be evaluate str-2 then print.
Str-2 means decrements two times. Pointer will be at starting location, i.e 500.
Now printf will be like this printf ("%d\n", 300). Then finally print 300.
Step->6 return 0; means program execute successfully.
                        Krishna mohan said: 
                         
                        1 decade ago
                
                Here str is char type & how we can assign it to integer type i.e: %d without any conversion.
                
                        Pankaj tilara said: 
                         
                        1 decade ago
                
                Thank you to all.
                
                        Nejat said: 
                         
                        10 years ago
                
                How do we know that the string is first initialized to 300?
                
                        Balaji said: 
                         
                        8 years ago
                
                Here, Str ++means address increment.
                
                        Amol said: 
                         
                        10 years ago
                
                Nice explanation.
                
                        Fanzy said: 
                         
                        1 decade ago
                
                Pointers are always incremented by 2 byte. Then how it is possible? can anyone explain this please?
                
                        Shubham said: 
                         
                        9 years ago
                
                At the starting str will points to the first character of the string which is %.
After executing two times increment operator on str pointer will skip the first two characters of the string which is %d and points to the \.
Inside the printf statement, str-2 make str point to the starting of the string .
So, the whole string will appear in the printf statement.
printf("%d\n",300).
Therefore, the output will be 300.
                After executing two times increment operator on str pointer will skip the first two characters of the string which is %d and points to the \.
Inside the printf statement, str-2 make str point to the starting of the string .
So, the whole string will appear in the printf statement.
printf("%d\n",300).
Therefore, the output will be 300.
                        Shami said: 
                         
                        9 years ago
                
                Nice @Shubham.
                Post your comments here:
 
            
        Quick links
                            Quantitative Aptitude
                                    
                                    Verbal (English)
                                    
                                    Reasoning
                                    
                                Programming
                                    
                                    Interview
                                    
                                     Placement Papers