Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I am trying to get the value for the last equation
int a = 0, b = 0, c = 0, x = 0, y = 0, z = 0;
a = b++ + ++c;
printf("a=%d\n", a);
x = y + 1 + ++z;
printf("x=%d\t", x);
printf("b=%d\t", --b);
printf("b=%d\t", b++);
printf("c=%d\t", c+1);
printf("c=%d\t", 2-c);
whats the value of the last equation and why? how do I calculate it
These statements
printf("c=%d\t", c+1);
printf("c=%d\t", 2-c);
do not change the value of the variable c.
The variable was changed only in this statement
a = b++ + ++c;
^^^
If you want to change the variable c in the calls of printf then you should write at least
printf("c=%d\t", c = c+1);
printf("c=%d\t", c = 2-c);
Then the output will look like
a=1
x=2 b=0 b=0 c=2 c=0
^^^
Without these changes the output is
a=1
x=2 b=0 b=0 c=2 c=1
^^^
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
In this piece of code:
int main()
{
int i=0;
while(i<10)
printf("%d",i++)
return 0;
}
If I am not wrong, the printf will always print the value +1 of i, so the first print will not be 0 but actually 1, am I right?
You are wrong. The post-increment i++ is evaluated to the value of i before incrementing, so what is printed in the first iteration is 0.
The expression x++ evaluates to x before incrementing.
int x = 5;
printf("%d\n", x++);
// Output: 5
printf("%d", x);
// Output: 6
On the other hand, ++x evaluates to x + 1, or x after incrementing:
int x = 5;
printf("%d\n", ++x);
// Output: 6
printf("%d", x);
// Output: 6
When you compile code it is read from left to right like text,
so for example your line 6
printf("%d",i++)
would first print the decimal and then add to variable.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I executed a code in the C language, However I am unable to understand its output.
#include <stdio.h>
int main()
{
int a=5;
int b= ++a + 0!=0;
printf("%d %d",++a, b);
return 0;
}
The output for the above program is
7 1
I am unable to understand why it is so.
Order of operations causes this to be treated as:
int b = (((++a) + 0) != 0);
Therefore:
int b = (6 != 0);
6 isn't 0, so that has a value of true aka 1.
int b = 1;
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
#include <stdio.h>
int main()
{
int x=3;
float y=3.0;
if(x==y)
printf("\n x and y are equal");
else
printf("\n x and y are not equal")
return 0;
}
The code prints "x and y are equal".
Please explain how did this happen.
When comparing an int variable to a float variable using ==, the int is converted to a float implicitly first, and then the comparison is made.
Hence, float(x) == y means 3.0f == 3.0f, which is true, that's why you it executes:
printf("\n x and y are equal");
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want to print two strings in console
int k = 3;
for (int i = 0; i < k; i++)
{
printf("\ra: %d\n\rb: %d", i*2, i*3);
}
I want to get this result:
a: 4
b: 6
But I get:
a: 0
a: 2
a: 4
b: 6
The reason why you are getting that output is that you have just one \r before a, but \n\r between a and b in the:
printf("\ra: %d\n\rb: %d", i*2, i*3);
Every cycle, the cursor returns to the beginning of line where b is and it writes OVER it, with new value for a :
1st 2nd 3rd run
a=0 a=0 a=0
b=0 a=2 a=2
b=3 a=4
b=6
since that is quick, you can see only the last one. It's really not clear what you wanna do, but if you need only the output of the last run, you can write the printf after the cycle:
for (int i = 0; i < k; i++)
{
...
}
printf("\r\na: %d\n\rb: %d", i*2, i*3);
If you change the loop to
for (int i = 2; i < k; i++)
this will print the result you asked for.
a: 4
b: 6
You could do a if inside the for like:
if (i * 2 == 4)
printf ...
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
#include <stdio.h>
int main()
{
int x = 1, y = 1;
for(;y;printf("%d%d\n",x,y))
y = x++ <= 5;
return 0;
}
I'm confused about the structure of the for construct in the code snippet above. It's taken form a book on programming in C.
The correct output is the following according to the book.
2 1
3 1
4 1
5 1
6 1
7 0
Can someone explain how and why this is the output?
To understand what the for loop does, here is the equivalent while loop:
while (y) {
y = x++ <= 5;
printf("%d%d\n",x,y);
}
The transformation goes like this:
for (INIT; COND; INC) {
BODY;
}
->
INIT;
while (COND) {
BODY;
INC;
}
Note that in C the expression y is equivalent to y != 0 if y is an int.
The construct becomes easy to understand if you expand it.
I reckon the trick here is in understanding that the suffix incrementation of x happens after the comparison.
Another concept shown in your example is the fact that in C all comparisons return a boolean value (1 for true and 0 for false).
#include <stdio.h>
int main()
{
int x = 1, y = 1;
printf("first version:\n");
for(;y;printf("%d%d\n",x,y))
y = x++ <= 5;
printf("second version:\n");
x = 1;
y = 1;
while (y != 0)
{
if (x <= 5)
y = 1;
else
y = 0;
x = x + 1;
printf ("%d%d\n",x,y);
}
return 0;
}
I hope this answers your question.
To summarize how for loop works in C:
for(initialization; condition; expression) {
//statements
}
1. initialization is executed before the first evaluation of the condition
2. expression is evaluated after every iteration of the loop. 3. The loop will only be entered when the condition is true.
In your code, please note that in the line,
y = x++ <= 5;
' x ' is compared with the number 5 before its value is incremented. For example, on the 5th iteration x=5 is checked against (<=5), then it is incremented to 6. After that the statement
printf("%d%d\n",x,y)
is executed printing "61".