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 3 of 5.
                
                        Gunjan said: 
                         
                        1 decade ago
                
                What will be the output char ch=300;?
As it is unsigned it exceeds the range, what is the answer and why please explain.
                As it is unsigned it exceeds the range, what is the answer and why please explain.
                        Fanzy said: 
                         
                        1 decade ago
                
                Pointers are always incremented by 2 byte. Then how it is possible? can anyone explain this please?
                
                        Abi said: 
                         
                        1 decade ago
                
                Hi everyone!
Please explain, how come char ptr gives an integer as result without typecasting?
                Please explain, how come char ptr gives an integer as result without typecasting?
                        Ram said: 
                         
                        1 decade ago
                
                printf is right?
                
                        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?
                        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".
                        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.
                
                        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.
                        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.
                        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.
Post your comments here:
 
            
        Quick links
                            Quantitative Aptitude
                                    
                                    Verbal (English)
                                    
                                    Reasoning
                                    
                                Programming
                                    
                                    Interview
                                    
                                     Placement Papers