Friday, December 4, 2009

Return values of some functions in c

scanf

scanf("%d" , & i); //10 is given as input

Scanf returns number of items successfully read and not 1/0. Here 10 is given as input which should have been scanned successfully. So number of items read is 1.

printf
Upon a successful return, the printf() function returns the number of characters printed (not including the trailing '\0' used to end output to strings). If the output was truncated due to this limit then the return value is the number of characters (not including the trailing '\0') which would have been written to the final string if enough space had been available. Thus, a return value of size or more means that the output was truncated. If an output error is encountered, a negative value is returned.
eg.
#include
int main()
{
int i=448;
printf("%d\n",printf("%d",printf("%d",i))); return 0;
}

Output: 44831
We proceed from right to left ... 1st 448 is printed, that has 3 characters, than 3 is printed and finally as 3 is 1 digit no. we print 1
alternatively if we use the string :
printf("%d\n",printf("%d a ",printf("%d b ",i)));
Output: 448 b 6 a 4
Now we have to count 448+1 space + b+1 space = 6 characters 
6 +1 space + a + 1 space = 4

0 comments:

Post a Comment