Wednesday, February 16, 2011

fun with printf...

Here are some interesting questions with printf ,have a look ....

(1)
#include<stdio.h>
#include<conio.h>
int main()
{
printf(5+"my name is milan kumar");
printf("\n");
printf(5+"my name is*milan kumar"+5);
printf("\n");
printf(5+"my name is milan kumar"-5);
printf("\n");
getch();
return 0;
}

output:
me is milan kumar
*milan kumar
my name is milan kumar

Explanation:
when we write "my name is milan kumar" that is a string ,it return the base address of the string and when we say 5+"some sting" then 5 get added to the base address and printf starts from the incremented address till the NULL is not found ....hence the ouptut


(2)
void main()
{
int a=30;
printf("%d"+1,a);
}

output:
d

Explanation:
As we have seen in the above example that whenever we write some string in the printf ,its base addres is taken ..so in this example when we write "%d"+1 base address of "%d" is taken and 1 is added to that and now printf will look as this :printf("d",a).now you can easily say why the ouput is d..

 (3)
void main()
{
int a=30;
printf("%d:%d"+1,a);
}

output:
d:30

Explanation:
In this example,the format string is evaluted and turns out to be "d:%d" after incrementing the address.and you better know that how printf("d:%d",a) will be evaluted .hence the output..


(4)
void main()
{
int a=4;
printf(&a["hi! my name is milan kumar"]);
getch();
}

output:
my name is milan kumar

Explanation:
This output can be expalined under the knowledge that array subscripting is commutative in C...means that a[i] and i[a] both are same and rather equivalent to *(a+i).OR we say "milan"[2] and 2["milan"] are same and both yields l as the output....
so ,coming at the question:
&a["hi! my name is milan kumar"] is same as (&("hi! my name is milan kumar")[a]) and evaluated as 
(&*("hi! my name is milan kumar"+a) ) ans as & and * are mutual cancelling operators this is same as saying 
("hi! my name is milan kumar"+a) i.e. ("hi! my name is milan kumar"+4)..
so now printf will look as;
printf("hi! my name is milan kumar"+4) produces the output as described in question(1)..........

(5)
lets take a look at tricky one...

void main()
{
int a=7,b=8;
printf(&a["c is a big headache:%s???"],&b["hello!! check out your concepts"]);
getch();
}

output:
big headache:check out your concepts???


Explanation:
For the explanation of this question ,have one thing in mind that is the Protype of  printf or more importantly the arguments printf takes ,as we know printf takes first argument as the format string which is further used to format the argument followed by a comma ..as we write printf("%d%d%d",a,b,c) in this "%d%d%d" is the format string which is use to format the arguments which are a,b,c.. so,1st %d for a ,2nd for b and 3rd for c...

coming back to the question..in our question the format string is:&a["c is a big headache:%s???"] and we can take it as "big headache:%s???" as explained in the above question..so with the new format string printf will look as:
printf("big headache:%s???",&b["hello!! check out your concepts"]);
so,we can see that there is a %s in the format string to print the 1st argument which can be evaluated as :
"check out your concepts" as explained in the above question...so now printf will look as this:
 printf("big headache:%s???","check out your concepts");
now we can evalute the printf easliy just by replacing the %s with the first argument...ehnce the result....

 (6)
#include <stdio.h>
  int main()
  {
    int a=3, b = 5;

    printf(&a["Ya!Hello! how is this? %s\n"], &b["junk/super"]);
    printf(&a["WHAT%c%c%c %c%c %c!\n"], 1["this"],
       2["beauty"],0["tool"],0["is"],3["sensitive"],4["CCCCCC"]);
getch();
    return 0;
  }


output:
Hello! how is this? super
That is C!


Explanation:
You can easily explain to yourself as explained in the above questions.......