New to C - I am struggling to figure this out [duplicate] - c

This question already has answers here:
What is the difference between prefix and postfix operators?
(13 answers)
Closed 7 years ago.
I don't understand how the results of this is :
2
2
2
Here is my code :
#include <stdio.h>
int main()
{
int a = 1, b = 1, x = 0, y = 0;
double w;
x = 1 + a++;
printf("x = %d\n", x);
printf("a = %d\n", a);
y = ++b;
printf("y = %d\n", y);
printf("b = %d\n", b);
}
Ok i understood the postfix and prefix but i still don't understand why a and b are 2 and not 1 .
They are not being saved anywhere
so when you say x=1+a++ and y=++b,
b becomes 2 and is saved in y . How does b keeps being 2 when not saved anywhere such as b=++b .
Sorry i am not sure if you guys follow what i am thinking.

You have to understand how the increment operator works.
You have two operations :
X++ => Return the value of X, then increment it by 1.
++X => Increment X by 1 then return it.
In your situation, the line with a problem is here : x = 1 + a++;
This translates into :
Return the value of a (1) and increment it (a becomes 2).
Set the value of x equal to the 1 + the value returned by a (1) (x becomes 2)
Hope this helps.

C Language Pre-increment and Post-increment Operators
#include <stdio.h>
int main()
{
int a = 1, b = 1, x = 0, y = 0;
double w;
x = 1 + a++;
printf("x = %d\n", x);
printf("a = %d\n", a);
y = ++b;
printf("y = %d\n", y);
printf("b = %d\n", b);
}
Pre-increment means increment variable before its value is used. Post-increment means increment variable after its value has been used. To understand how these operators work, let's look at the first case:
a = 1;
x = 1 + a++; // ++ on right means post-increment
First you're setting the value of a to 1. Then you're saying add a (value is 1) to x (value is 1). The result is x has a value of 2. And then, after the statement is done executing, as a side-effect, a is incremented, because you used the post-increment form of ++. So after x has been set to a value of 2, the value of a will become 2.
Instead, if you used a a pre-increment operator:
a = 1;
x = 1 + (++a); // ++ on left means pre-increment
Again, you start with a = 1, but this time, a is incremented before its value is used in the statement, because ++ on the left-side means pre-increment. In other words, first, a is incremented to the value of 2. Then a is added to x (whose value is 1), setting the value of x to 3.
In both the above cases, a starts out with a value of 1 and becomes 2. The difference is whether that happens before or after the value of a is used in the expression.

Related

How does this logic work: k+=(x=5,y=x+2,z=x+y) ;

How does this logic work: k+=(x=5,y=x+2,z=x+y);
How it will give result k == 22. When I initialize the value of k = 10
#include <stdio.h>
int main()
{
int x,y,z,k=10;
k+=(x=5,y=x+2,z=x+y);
printf("value of k %d ",k); //why it will show value of k =22
return 0;
}
In the right side of the assignment statement there is used the comma operator (sequentially two times from left to right). Its value is the value of the last operand. So this statement
k+=(x=5,y=x+2,z=x+y);
that can be for clarity rewritten like
k+=( ( x = 5, y = x + 2 ), z = x + y );
in fact is equivalent to the following set of statements
x = 5; // x is set to 5
y = x + 2; // y is set to 7
z = x + y; // z is set to 12
k += z; // k is set to 22
From the C Standard (6.5.17 Comma operator)
2 The left operand of a comma operator is evaluated as a void
expression; there is a sequence point between its evaluation and that
of the right operand. Then the right operand is evaluated; the result
has its type and value.

I'm confused about the output

#include <stdio.h>
#include <stdlib.h>
int main()
{
int a , b ,c ;
printf("Enter values for a and b: ");
scanf("%d%d",&a,&b);
a = a + b-- ;
if (a<b){
c = -1;
printf("\n\t%d %d %d\n\n",a,b,c);
}
else {
c = 0;
printf("\n\t%d %d %d\n\n",a,b,c);
}
}
Lets assume the value of the input for a and b are 2 (for both of them).
I studied the above program, but when it comes to the output it will be 4 1 0, a=4,b=1,c=0.
But, the calculation part above said that a=a+b-1 which will be the value of a is 3, now the new value of a is 3. But for b the value is still 2 because we didn't assign a new value to it.
I am very confused about the output.
There is a difference between a+1, a++ and ++a. Details here.
Therefore, when you say
a = a + b--;
You are actually saying
a = a + b;
b = b - 1;
If you say
a = a + --b;
It becomes
b = b - 1;
a = a + b;
And if you say
a = a + (b-1)
It does what you think: a = a + b - 1. The value of b doesn't change afterwards.
At the beginning, a and b are both 2
Then, you execute a = a + b--;.
The decrement operator is located after the b, so it evaluates to:
a=a+b;
b=b-1;
After this, a will be 4 and b will be 1.
a is not smaller than b, so c will be 0.
​
Note:
If it would be a = a + --b, it would evaluate to
b=b-1;
a=a+b;
Because the -- is executed at the beginning of the evaluation.

Increment operator not showing expected result

I am not getting the result I expected.
void main(){
int x = 5;
int y = x++;
printf("%d, %d", x, y);
}
I am getting 6, 5 as output, but I expected 6, 6.
You're using the post-increment operator.
The line:
int y = x++;
is equivalent to:
int y = x;
x += 1;
The increment of x occurs after the value of x is copied to y.
The pre-increment operator is probably what you're looking for, which would be used like:
int y = ++x;
This is equivalent to:
int y;
x += 1;
y = x;
which appears to be what you expect.
The ++ operator behaves differently depending where it is.
y = x++; will get the value of x and then increment it. (Post-increment)
y = ++x; will increment x first, and then give that new value to y. (Pre-increment)
You're probably looking for pre-increment.
You are using the postincrement operator. It evaluates to the value of the variable before it is incremented, which is why y contains 5.
For your case you want the preincrement operator.
y = ++x:
You have used the post-increment operator (i.e x++) and assigned it to y, so y will be 6 and x will be 5.
For your expected output, use the pre-increment operator, like so:
int main() {
int x = 5;
int y = ++x;
printf("%d, %d", x, y);
}

C: help me understanding the output

can any one help me understanding the output of this code?
#include <stdio.h>
int main()
{
int x = 1, y = 1;
for(; y; printf("%d %d \n", x, y))
{
y = x++ <= 5;
}
return 0;
}
the output is:
2 1
3 1
4 1
5 1
6 1
7 0
A for loop in the form of
for (a; b; c)
{
d;
}
is equivalent to
{
a;
while (b)
{
d;
c;
}
}
Now if we take your loop
for(; y; printf("%d %d \n", x, y))
{
y = x++ <= 5;
}
It is equivalent to
{
// Nothing
// Loop while y is non-zero
while (y)
{
// Check if x is less than or equal to 5, assign that result to y
// Then increase x by one
y = x++ <= 5;
printf("%d %d \n", x, y);
}
}
Now it should hopefully be easier to understand what's going on.
Also: Remember that for boolean results (like what you get as result from a comparison), true is equal to 1, and false is equal to 0.
The code is an obfuscated, ugly version of this:
#include <stdio.h>
#include <stdbool.h>
int main()
{
int x = 1;
bool y = true;
while(y == true)
{
y = (x++ <= 5);
printf("%d %d \n", x, (int)y);
}
return 0;
}
y in the original code serves as a boolean. Back in the ancient days, there were no boolean type in C so it was common to use int instead. The expression y = x++ <= 5; evaluates to 0 or 1, which is equivalent to false or true.
Note:
While the C language allows all manner of crazy stuff, you should never write for loops as in the original code. De facto standard is to write for loops like this:
The first clause of a for loop shall only contain iterator initialization.
The second clause should only contain the loop condition.
The third clause should only contain a change of the loop iterator, such as for example an increment (like i++).
A for loop that doesn't follow the above industry standard rules is badly written, no excuses.
The statement y = x++ <= 5; sets y to 1 if x is less than or equal to 5, or to 0 if x is greater than 5. It then increments x. The loop stops when y is equal to 0, that is, on the iteration after x was incremented above 5. Then it runs the printf() statement on each iteration.
You wouldn’t normally see a loop switch the loop condition and the loop body that way. This seems to be a pathological example of how it’s technically legal. You shouldn’t imitate it.
y is 1 (true) as long as x <= 5 and x increases every iteration.
Rewrite your code:
for (int x = 1, y = 1; y; y = x <= 5, x++) {
printf("%d %d\n", x, y);
}
or using while
int x = 1;
int y = 1;
while (y) {
printf("%d %d\n", x, y);
x++;
y = x <= 5;
}
The first component of the for loop is empty; which means values of variables haven't changed. The condition in the for loop is y. Which means that the loop will run till the condition y is true; which means till the value of y is anything other than 0.
When the value of y becomes equal to 0; the condition will become false and the loop will stop iterating.
Then comes the block execution...
y = x++ <= 5;
Here first the condition x <= 5 is checked. Note that x++ is post increment and so the value of x will be incremented after the execution of the statement. So, if the value of x was 1 before increment; it will check 1 <= 5 and not 2 <= 5 and after the execution of the statement; the value will become 2.
After that comes the third component of for loop which is the increment part. Here,
printf("%d %d \n", x, y)
simply prints x and y.
So, what happens is; when the loop starts, both x and y are 1.
The statement
y = x++ <= 5;
is encountered, thus the condition x++ <= 5 is checked, where x is 1. Since the condition is true; the value of y will be 1 and x will be incremented to 2. The same thing continues.... when x will be equal to 5, x++ <= 5 will check 5 <= 5 and the condition will be true and x will be incremented to 6. Now when it goes in the loop the next time; the condition will be false and y will instead be equal to 0. Thus, now when the loop checks the condition y, the condition will be false and thus the control will flow out of the loop and thus you see the result.
I built and dumped this code in IDA since I understand assembly better :D
This code is a bit obfuscated so here will be the normal version of it:
#include <stdio.h>
int x = 1;
int y = 1;
while(y != 0) {
if(x++ <= 5)
{
y = 1;
} else {
y = 0;
}
printf("%d %d\n",x,y);
}
that for loop in the source:
for(; y; printf("%d %d \n", x, y))
checks if y is a non-zero value and if it is, it will print the string you want. In the body of the loop, the result of the comparison (0 or 1) will be copied to y and the check continues.
Precedence of operator <= is higher than the operator =.
So, the statement:
y = x++ <= 5;
is equivalent to:
y = (x++ <= 5)
Because of post-increment ++ operator, the value of x returned first and then x is incremented.
The initial value of both x and yis 1.
Since y is 1, the for loop condition evaluates to true and the statement in the for loop block executed. But in the for loop of your code, in place of loop iterator update statement you have printf() statement which is printing the value of x and y after executing the loop body statements. The flow goes like this:
1 <= 5 evaluate to 1 which is assigned to y and after this x is 2,
Output : 2 1
2 <= 5 evaluate to 1 which is assigned to y and after this x is 3,
Output : 3 1
3 <= 5 evaluate to 1 which is assigned to y and after this x is 4,
Output : 4 1
4 <= 5 evaluate to 1 which is assigned to y and after this x is 5,
Output : 5 1
5 <= 5 evaluate to 1 which is assigned to y and after this x is 6,
Output : 6 1
6 <= 5 evaluate to 0 which is assigned to y and after this x is 7,
Output : 7 0
Now the value y is 0 and in your code, the for loop condition is y so the loop condition evaluates as false and loop exits.

Why does this program run the way it does?

#include<stdio.h>
int main()
{
int a[5] = {5, 1, 15, 20, 25};
int x, y, z;
x = ++a[1];
y = a[1]++;
z = a[x++];
printf("%d, %d, %d", x, y, z);
return 0;
}
"x" is printed as 3, but I would have expected it to return 2? In fact if I remove the "++" and set x equal to a[1], it returns as 2. It adds 1 to any value that is actually there. Am I missing something?
"x" is printed as 3, but I would have expected it to return 2?
x = ++a[1];
Here x = 2 because of pre-increment
The you have
z = a[x++];
x ++ = x + 1 = 2+1 = 3
Hence x=3
x = ++a[1];//Increment a[1] and then assign it to x
y = a[1]++;//assign a[1] to y and then increment a[1]
z = a[x++];//assign a[2] to z and then increment x
The ++ is called an increment operator. It increases the value by 1.
In you code you have
x = ++a[1];
++ before a variable is the Pre Increment Operator. It increments the value of a[1] before assigning it to x. So the value of a[1] and x becomes 3.
The other one
y = a[1]++;
++ after the variable is the Post Increment Operator. It assigns a[1] ( which has already become 3 ) to y and then increments the value of a[1] to 4.
This link will help you
http://www.c4learn.com/c-programming/c-increment-operator/#A_Pre_Increment_Operator
x=++a[1]; // Now x got the value 2.
In this line,
z = a[x++]; // x++ will be happen after the assigning of a[2] to z. So the value of x is incremented. So the x value is became 3.
It is post increment so the z got the value as 15. Refer this link.

Resources