This question already has answers here:
How do I explain the output of this simple C code?
(4 answers)
Return value of printf() function in C
(13 answers)
Closed 6 years ago.
int main (void){
const int y = 99;
printf("%d\n", printf("y = %d", y));
}
This program prints "y = 996", I would like help in understanding.
Firstly,
printf("y = %d", y) prints out y = 99
So the rest of the final expression is:
printf("%d\n", "y = 99");
But this should be invalid as %d expects a number, not a charArray?
The printf function returns the number of bytes printed. That value is what is being passed to the second call.
The inside call prints y = 99 which is 6 characters. So the outer call receives 6 as the second parameter:
printf("%d\n", 6);
So the output is y = 996.
Let me take a simple example to clear your doubt:
sample :
void main(void)
{
int b=printf("hellow");
printf("%d",b);
}
output:
6
printf("message") is a predefined method which returns "no. of characters present in the message" as the above example.
you code:
int main (void){
const int y = 99;
printf("%d\n", printf("y = %d", y));
}
note: After printing the message printf() always returns int value which is no of characters because return type of printf() is int type.
step 1: Inner printf() function prints 99 first.
step 2: After execution it returns 6 which is no. of characters in the message.
message is "y = %d"
step 3: 6 is hold by outside printf() function as :-
printf("%d",6);
So, it gives output combinely :
996
While printf will print out a string to stdout, what it returns is an integer that represents the number of characters it printed out.
The nested call to printf, printf("y = %d", y), prints out 99 and returns 6 because the string "y = 99" is 6 characters long, which makes the outer call to printf, which takes the return value of the nested call to printf as an argument, produce the output 6, giving you the final output of 996.
Related
The values which are output from inside the called function and from inside the calling function are not identical.
Why are values of y and value of m not the same?
screenshot of code and output, showing "Value of m 6" and "value of y 13"
#include<stdio.h>
int pass(a);
int main()
{
int x,y;
printf("Hello world\n");
printf("Enter the x ");
scanf("%d",&x);
y = pass(x);
printf("value of y %d",y);
return 9;
}
pass(m)
{
m = m + 5;
printf("Value of m %d\n",m);
// return 5;
}
Output:
Hello World
Enter the x 1
Value of m 6
value of y 13
Strictly, this should not even attempted to be explained, because of undefined behaviour, compare Reaching end of function without return statement
but ...
Assuming that my guess on the pattern of returned values (a constant plus the number of digits in "input + 5") is correct:
By chance, the default assumptions of the compiler when seeing the incomplete prototype int pass(a); (should be int pass(int a);), allow it to associate the later provided implementation of pass(m) (should be int pass(int m);, or more consistently int pass(int a)).
So when you call y = pass(x);, y gets the value returned by that implementation.
The implementation then lacks a clean return statement (it has one, but inactive by being a comment).
So at the end of executing that function, the most recently determined result is returned, another default of compilers which you should better not rely on for clarity and readability of code.
The most recent result is the return value of the call to printf().
That return value is the number of successfully printed characters.
You might want to read up on printf() and its return value in its specification, and about the concept of return values, prototypes and data types of parameters and return values in general.
For the output you show in your picture of text,
Value of m 6\n (thanks for making me type that...) that is, let me count
^ ^ ^ ^
1 5 10 13,
13, including the newline at the end of the output from inside the function.
Obviously, this is completely unrelated to the value of the local variable m, which is seen in the picture of text.
For more details on how to achieve what you might try to do see the comment David C. Rankin.
I'm trying to print sum of numbers in macro as well in main, but don't know why i'm getting the different output with printing it in main.
#include<stdio.h>
#define sum(a,b)\
printf("\n%d",a+b)
int main() {
sum(2,4);
printf("\nsum in main = %d",sum(2,4));
return 0;
}
output:
6
6
sum in main = 2
looks like it is printing the number of char present in marco.
If i comment the printf in macro, then printf in main works fine.
#include<stdio.h>
#define sum(a,b) a+b
//printf("\n%d",a+b)
int main() {
// sum(2,4);
printf("\nsum in main = %d",sum(2,4));
return 0;
}
output:
sum in main = 6
printf("\nsum in main = %d",sum(2,4));
In the first case the printf is printing the return value of printf which is the number of characters printed.
printf is expecting the value for format specifier %d , the value is the return value of printf which is from the macro.
The other case %d is getting value 6 from the macro sum(2,4)
first, you should declare your macro as:
#define sum(a,b) ((a)+(b))
second, it works perfectly fine, the issue is, that in first example you defined sum as
#define sum(a,b) printf("\n%d", a+b)
so this macro will just print sum of a+b and then return printf value, which is count of characters printed, in this case 2 because you print newline (\n) and result of sum (6), so in your case after unwrapping macro your code looks like:
printf("sum in main = %d\n", printf("\n%d", a+b));
that's why it's printing 2 at the end - you just pass result of function printf to another printf
This question already has answers here:
what does the - operator do with char *?
(4 answers)
Closed 9 years ago.
#include <stdio.h>
int main()
{
short int a = 5;
printf("%d" + 1, a);
return 0;
}
The code prints the alphabet enclosed in quotes in printf irrespective of the value and type of variable a. If any other number is added except 1 nothing gets printed.
Why is it so?
Not sure, I would expect it to print just d, of course. That's what happened when I tested it.
If you add more than 1 (or 2) all bets are off and you're getting undefined behavior for passing a random pointer instead of a valid formatting string.
On compiling the above code, you should get a warning like:
[Warning] too many arguments for format [-Wformat-extra-args]
Now remove the printfs argument a.
printf("%d" + 1);
This will print d.
100 101
% d
^
|
Here is the starting address of the string.
%d is a string and its starting address is 100. "%d" + 1 will give you the address 101.
Why you want to do this?
if you want you can do like
do like
printf("%d", a+1);
Try this and you'll understand what unwind is trying to make you understand
#include <stdio.h>
int main()
{
short int a = 5,b = 4;
printf("%d %d" + 4, a,b);
return 0;
}
OUTPUT: d
Since it takes the 4th character inside the double quotes in printf() statement..
If number is 3
OUTPUT: 5
If number is 2
OUTPUT: 5
If number is 1
OUTPUT: d
int main()
{
int a, b, c;
a = 10;
b = 20;
c = printf("%d", a) + ++b;
printf("\n%d", c);
}
The output of the above program is 23 it seems but i dont know how it is obtained. Can anyone have an idea about it?
printf has a return value, which is the total number of characters it prints.
The statement printf("%d",a) will print 10, which means the return value of printf here is 2.
The rest is easy:
c=printf("%d",a)+ ++b;
c will have a value of 2 + 20 + 1, which is 23.
Here the output will be two different integers,for two different printf statements . For the first printf statement the code prints 10, then when this printf statement participate in some assignment statement , it is treated as the number of characters it is printing i.e. 2 here. Then it is added to ++b i.e. 21 (PRE-INCREMENTED) . So the output is 23(2 + 21) .
The whole output looks like this :
10
23
printf returns the number of characters printed as an integer. So as you are printing 10 it will return 2.
So now
c=printf("%d",a)+ ++b;
will become
c=2+ ++b;
since b with a value of 20 is pre-incremented this will become
c=2+21
Therefore c=23
I am trying to understand the output of the program printed below. When I look at it, I see that when printnum() is called with an argument of 1, "1" will be printed and then since 1<7, the function will call itself. This process will continue until "6" is printed and then printnum(7) is called. So, now "7" is printed and the if condition is not satisfied, so that code is skipped and we move to the second printf("%d", x) function where "7" is printed out again. There is nothing after the second printf("%d", x), so why doesn't everything end there? What makes the program keep going to print the numbers again in descending order?
#include <stdio.h>
int printnum ( int x )
{
printf("%d", x);
if ( x < 7 )
{
printnum ( x + 1 );
}
printf("%d",x);
}
int main()
{
printnum(1);
}
Output:
12345677654321
printnum(8) is never called, because it isn't true that 7 < 7.
The reason that you get the numbers printed in descending order once x = 7 is reached is, that each recursive call ends, leaving the previous call to continue.
Consider what it does for x = 1:
Print 1
Call recursively with x = 2
Print 1
If we expand this one level more:
Print 1
Print 2
Call recursively with x = 3
Print 2
Print 1
And one more:
Print 1
Print 2
Print 3
Call recursively with x = 4
Print 3
Print 2
Print 1
If you continue this expansion, you can see you get numbers in ascending order before the recursive call, and in descending order after the recursive call.
This happens because the second printf is called after your recursion exits at each level.
As your final recursive call printfs and ends, control transitions to the function that called it - the second-to-last recursive call. This call then exits the scope of the if statement, and calls printf, and then ends - upon which control transitions to the function that called it - the third-to-last recursive call. Repeat until you are inside the call to printnum(1), whose return takes you back to main.
It's recursion. When you enter the function you call printf, then you enter another level in the recursion, then you enter the printnum, so you call the printf with x+1 and so on.
When you arrive at stop condition (x==7), the function goes till the second printf (so 7 will be displayed again).
The function printnum terminates, so the program returns at the level above, then printnum at level 6 can printf again, terminate and return and so on.
Comments in-line!
int printnum ( int x ) x = 1 x = 2 x=6 x=7
{
printf("%d", x); prints 1 prints 2 prints 6 prints 7
if ( x < 7 ) Yes Yes Yes No
printnum ( x + 1 ); calls printnum(2) calls printnum(3) .... calls printnum(7) N/A
printf("%d",x); print 2 and return to print 6 and return prints 7 and returns to the
the caller which is the previous caller to the previous previous caller which is
main() which is printnum(1) caller which is printnum(x=6)
printnum(5)
}
Please check the following to print in ascending and descending order passing the starting value and the limit.. (please note that the error checking is not done!)
#include <stdio.h>
int printnum_123(int x, int limit)
{
printf("%d ", x);
if (x<limit)
printnum_123(x+1, limit);
return;
}
int printnum_321(int x, int limit)
{
if (x<limit)
printnum_321(x+1, limit);
printf("%d ", x);
return;
}
int main(void)
{
printnum_123(1, 10); printf("\n");
printnum_321(1, 10); printf("\n");
return 0;
}
$ ./a.out
1 2 3 4 5 6 7 8 9 10
10 9 8 7 6 5 4 3 2 1
$