Showing posts with label printf explored. Show all posts
Showing posts with label printf explored. Show all posts

Saturday, August 20, 2011

a tricky printf again...........

Here is a question which i am going to explore best to my concepts and knowledge,you can judge for yourself whether i am right or wrong..
int main()
{
int val=10;
printf(":%d",val+1,"-%d",val--);
return(0); 
}

Before evaluating this ques let us first evaluate the following printf statement
print(":%d",val+1,"-%d",val+2);


output for this printf:
:11
Explanation::As we know that printf first take the format string and according to the format specifiers in the format string it prints the arguments supplied to that..In this statement:
Format string is :%d
and the variable list id:val+a,"=%d",val+2
So this prints the val+1 that is 11 for the %d and leaves the another variables....

Now what if we want to print the remaining variables.......that so simple as we can see that the 2nd variable is a string and the third argument is an integer in the variable list so we should have the format string as given below:
format string----->:%d%s%d
and variables list is----->val+1, "-%d",val+2


so the output for this will be------>:11-%d12 
that is -%d is the string in this variable list so we have to use the %s format specifier to print this....


Now coming back to the real question,in the real question the printf statement is :
printf(":%d",val+1,"-%d",val--); 
If strictly speaking the result of this is undefined as you can read the sequence point post ,
No doubt the evaluation will be from right to left but we can't assure the result...but let us suppose everything goes according to our will or thinking that is smooth evaluation form right or left then first val-- will be evaluated that will decrement the val to 9 then val+1 which lead to print 10 as the output
so the output will be----->:10
To prove the evaluation is  from right to left let us print the value of val--;
printf(":%d%s%d",val+1,"-%d",val--);
output--->:10-%d10

You might not be satisfied with the prove of right to left evaluation so just change the val-- to --val because above example shows the postfix property that is printing before decrementing

printf(":%d%s%d",val+1,"-%d",--val);
output--->:10-%d9
Now i thing it will be cleared to you....

 May be i am wrong,so please do comment whether you agree with me or not????
if not give the explanation..... 

Friday, February 18, 2011

%n explored...

Yesterday one of my friends asked me about %n,i got shocked because i have nvr hear abt that...after searching about %n in printf i come up with the follwing info...

What is does::
1.)It Prints nothing,
2.)But write number of characters successfully written so far into an integer pointer parameter..

We will see the ouput by using printf,fprintf and sprintf ,the concept is same it nvr prints to anything whether it is :
-->standard ouput(console) in case of printf.
-->a file in case of fprintf.
-->a string in case of sprintf

With printf
#include<stdio.h>
#include<conio.h>
int main()
{
    int *i,*j;
    clrscr();
    printf("milan%nkumar%n",i,j);
    printf("\ni=%d",*i);
    printf("\nj=%d",*j);
    getch();
    return 0;
}

output:
milankumar
i=5
j=10

Explanation:
let us take the format string in the printf.it is "milan%nkumar%n" whenever printf find the %n it looks for the integer pointer argument and writes the numers of characters sucessfully written so far at its location,so,when printf find the first %n it sets i with  number of character written on the stdout that is 5(milan)...now finding the another %n ,printf sets the j with the number of characre written so far on the stdout that is 10(milankumar)..
here we can see that %n nvr prints anything to the stream associated with the function...

With fprintf
#include<stdio.h>
#include<conio.h>
int main()
{
FILE *fp;
int i=10,j=20,k=30;
int *p;
fp=fopen("c:\\test.txt", "w");
fprintf(fp, "Testing...%d%d%d%n",i,j,k,p);
printf("done....");
printf("\nnumber of characters written to the file are %d",*p);
getch();
return 0;
}

Output:
Testing...102030  /*data written to file:*/
done/*written to console*/
number of characters written to the file are 16 /*written to console*/

Explanation:
This example clears that %n is only associated with the printf in which it is used,it is not hardley attched with the stdout or printf ,it will always set the integer pointer with the number of the characters written to the stream that is used by the function..stream many be file,stdout,string etc..
so number of characters written to the file are 16(Testing...102030)..hence the output...


With sprintf
#include <stdio.h>
#include<conio.h>
int main()
{
char buff[50];
int a=34,b=50,*p;
clrscr();
sprintf(buff, "%d minus %d equals %d%n",a,b,a-b,p);
printf ("(%s) is the result of our sprintf",buff);
printf("\nNumber of characters written to the buff are:%d",*p);
getch();
return 0;
}

output:
(34 minus 50 equals -16) is the result of our sprintf
Number of characters written to the buff are:22

Explanation:
Now i think you can easily explain the output similarly in th eabove examples....
other wise feel free to mail me at milankmr@gmail.com

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.......

Tuesday, February 15, 2011

Question on printf

What is the output of the following program?????????
void main()
{
int i=60,j=80;
printf("%d:%d");
}

Output:
80:60

Explanation:
As we have seen that All automatic variables and constants are stored into stack area in the data segment ..
so when we initialize the variables i and j they goes int the stack's initialized area...and printf pops the value from the stack if no 2nd argument is specified....and all we know that stack is LIFO so value 80 is printed before the 60.....

Now take a look at this:
void main()
{
int i=60,k,j=80;
printf("%d:%d:%d");
}

Output:
80:60:garbage value

Explanation:
This is again a bit tricky...we expect the result to be 80:garbage:60 which is not ..........
as we have seen that there are two portions in the stack area in the data segment one for intialized vaiables and other for uninitialized vaiables,and initialized variables lie above the uninitialized variable in the stack....as in the example k is not initialized it goes into the uninitialized area and comes out at the end..........


Monday, February 14, 2011

WAP to calculate and print 2 raise to the power 5 without using any digit in ur prgm

This is nothing just a trick,as we have looked that printf returns the number of charaters printed ....
and we know there is a pow function in the math.h library which returns the result of a number raised to other number.....

so the solution is........


#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
clrscr();
printf("power is %f",pow(printf("hi"),printf("hello")));
getch();
return 0;
}

output:
hellohipower is 32.000000

Explanation:
printf("hi") returns 2 ,printf("hello") return 5 and pow function returns the 2 raise to the power 5..........

Note....pow(arg1,arg2) evalutes from right to left thats why it prints hello first and then hi............


what printf returns???????

Coming straightly to the point :the prototype of printf is:
int printf( const char *format ,…)
so the return type of printf is an int i.e. an interger value,
which is equal to the number of characters printed........
let us prove this by some examples:
(1)
void main()
{
int n;
n=printf("milan");
printf("the returned value from printf is %d:",n);
}

output:
the returned value from printf is:5

Explanation:
 as the number of character in the world "milan" is 5,so the value returned is 5.....

(2)
void main()
{
int a=10;
printf("value returned is:%d",printf("%d",a);
}

output:
10value returned is:2

Explanation:
firstly the inner printf executes and printf the value of a i.e. 10,then it returns the number of chararters printed to the outer printf i.e.2(1 and 0 in 10)...and the outer printf prints the "value returned is:2",where 2 is the value retured by the inner printf.........

(3)
void main()
{
int a=2222;
printf("%d",printf("%d",printf("%d",a)));
}

output:
222241

Explanation:
In this example ,we will really come to that printf returns the actual number of characters printed by it,,firstly the innermost printf prints the value of a i.e 2222 and returns the number of characters printed that is 4,now coming to the second printf ,second printf got the value 4 returned by innermost printf and prints it and return 1(yes,1 not 5 because the value printed by second printf is just 4 not 22224),so the first printf gets the returned value from the second printf which is 1 and print the same...so the output is 222241...in which 2222 is printed by innermost printf,4 is printed by second printf and 1 is printed by the first printf.......