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
Related
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
Newb here.
I'm probably missing something trivial but:
Here's the thing: http://i.imgur.com/8BmPci5.png
#include <stdio.h>
#include <stdlib.h>
int main()
{
//Sort Numbers (zuerst)
int numbers [10];
int i,j;
j = 0;
int read;
printf("Input numbers: \n");
for (i=0;i<10;i++) {
scanf("%d",&read);
if (read == 27) {
break;
} else {
numbers[i] = read;
j++;
}
}
printf("j is: %d, i is: %d\n", j, i);
for (i=0;i<j;i++) {
printf("numbers[%d] is: %d\n", i, numbers[i]);
}
return 0;
}
Output:
Input numbers:
1
2
3
^[
j is: 10, i is: 10
numbers[0] is: 1
numbers[1] is: 2
numbers[2] is: 3
numbers[3] is: 3
numbers[4] is: 3
numbers[5] is: 3
numbers[6] is: 3
numbers[7] is: 3
numbers[8] is: 3
numbers[9] is: 3
I have a for loop (goes from 0 to <10). I also have a scanf inside wich scans for an int. If it ain't ESC (ASCII 27), then it puts it into an array and it increments the value of j. If it's an ESC, it ('s supposed to) break (exit the loop). Later, only j (or j-1) number of array items would be printed.
Issue: j (and i too) increments to 10, even if break is called at ~ i = 3 or 4.
Break supposed to exit the for loop without doing anything after it's called, right? Quote from BeginnersBook.com:
It is used to come out of the loop instantly. When a break statement is encountered inside a loop, the control directly comes out of loop and the loop gets terminated. It is used with if statement, whenever used inside loop.
What's wrong with my code? Thanks in advance.
You're being naughty in that you're not checking the return value of scanf, which will tell you the number of variables that were successfully populated. In your case, you want that to be 1 to signal that something was read into your variable read.
If you try to pass \033 (ASCII encoded ESC), then scanf will fail to parse that to an int. Then the previous value of read will be retained (which could give you undefined effects if it hasn't yet been set to anything), and read == 27 will almost certainly be 0. That behaviour accounts for the output you are observing.
If scanf does fail, you could try reading a char from the stream, and check if that is equal to \033.
Please explain me why the last printf gives value 11?
I really don't understand why it happened.
When a = 10 the condition is not fulfilled so why this value has changed to 11?
Incrementation goes as soon as the condition is checked?
Code:
int main(void) {
int a = 0;
while(a++ < 10){
printf("%d ", a);
}
printf("\n%d ", a);
return 0;
}
Output:
1 2 3 4 5 6 7 8 9 10
11
Let's look at a++ < 10 when a is equal to 10.
The first thing that will happen is 10 < 10 will be evaluated (to false), and then a will be incremented to 11. Then your printf statement outside the while loop executes.
When the ++ comes on the right hand side of the variable, it's the last thing evaluated on the line.
Try changing a++ < 10 to ++a < 10, rerunning your code, and comparing the results.
The post increment operator increments the value of the variable before it after the execution of the statement.
Let's take an example,
int k = 5 ;
printf("%d\n", k++ );
printf("%d", k );
will output
5
6
because in the first printf(), the output is shown and only after that, the value is incremented.
So, lets look at your code
while(a++ < 10)
it checks a < 10 and then after that, it increments a.
Lets move to a few iterations in your loop.
When a is 9, the while loop checks 9 < 10 and then increments a to 10, so you will get output for that iteration as 10, and similarly, for the next iteration, it will check 10 < 10 but the while loop does not execute, but the value of a is incremented to 11 and thus, in your next printf() , you get output as 11.
Let's look at a simpler piece of code to show what a++ does.
int a = 0;
int b = a++;
printf("%d %d\n", a, b);
I think that you'd expect this to output 1 1. In reality, it will output 1 0!
This is because of what a++ does. It increments the value of a, but the value of the expression a++ is the initial pre-incremented value of a.
If we wanted to write that initial code at the top of my answer as multiple statements, it would actually be translated to:
int a = 0;
int b = a;
a = a + 1;
printf("%d %d\n", a, b);
The other increment that we have access to is pre-increment. The difference there is that the value of the expression ++a is the value of a after it was incremented.
Because it's post-increment. The compiler will first evaluate a<10 and THEN increment a by 1.
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
This question already has answers here:
sequence points in c
(4 answers)
Closed 9 years ago.
My code is :
#include<stdio.h>
int main()
{
char c='8';
int d=8;
printf("%d %d %d",d,d+=c>='0'&&c<='9',c++);
return(0);
}
The output of this question is : 9 9 56.
I'm unable to understand this.
Please somebody help me and please explain me the output.
Thanks.
You are observing undefined behaviour. d is passed as an argument twice, and once with side effects. If done in sequence your code should be equivalent to
char c='8';
int d=8;
printf("%d",d);
d+= (c>='0)' && (c<='9');
printf(" %d", d);
printf(" %d",c);
c++;
But since it is undefined in what order the arguments are computed when passing them with side effects to a function you are seeing here:
char c='8';
int d=8;
d+= (c>='0)' && (c<='9');
printf("%d",d);
printf(" %d", d);
printf(" %d",c);
c++;
Basically printf reads the argument from right to left so the first print would be of
" printf("%d %d %d",d,d+=c>='0'&&c<='9',c++); "
- 1st output = c++ -> which is nothing but ascii value of '8' = 56
and then c is incremented by 1 .
- next is the d -> it boils down to d = d + if(c>='0'&&c<='9') ...
so d = d+1 so 9 here
- next d again so it is again Hope it is clear !
Ok, the first thing you have to know is, a char is stored in the memory as Ascii Table. So in the memory the char c will have a integer value of '8'. From the table we know that the integer value of '8' is 56. According to the table we'll get:
'8': 56
'0': 48
'9': 57
So let's get started.
c++: It is a statement which adds c by one and return the value of current c.
Example:
int a,c;
c=1;
a=c++;
printf("a=%d,c=%d",a,c);
The result of this code is "a=1,c=2"
so %d of c++ is still 56.
d+=c>'0'&&c<='9': According to C's priority this statement will be like:
d+=(c>'0'&&c<='9')
So let's start with c>'0'&&c<='9' first. It is a condition statement. Is c's Ascii value great or equal to 0's Ascii value AND less or equal to 9's Ascii value?( Notice c's Ascii is 56 or 57 now because the evaluation order of printf is undefined. So it will be 56 if this statement is evaluated before c++ or 57 if after c++. But both way, c<='9' is true ) YES. So the statement is true. In C the TRUE is 1.
So d+=c>'0'&&c<='9' will be d+=1 which means d=d+1. So %d of d is 9.
So the result is "9 9 56"