why does this print? (C language) - c

Why does the following code run the while loop? I thought "mid = term" is an assignment, not a condition? Please explain. Thanks.
#include <stdio.h>
main ()
{
int mid = 4, term = 4;
while ( mid = term)
printf("%d\n", --term);
}

The result of an assignment is the value. Therefore the expression evaluates to 4 or a non-zero and thus, in C, TRUE.

mid = term is an expression evaluating to term. So the while loop will run till term = 0.

Because the expression evaluates to true.
Basically, you are saying mid = 4
Since any int that isn't zero, returns true in a conditional statement - the while will loop.

The expression mid = term actually evaluates to the value of mid after assignment. So, what's being evaluated is while(4). Since all nonzero integers are interpreted as true (this is kind of a simplification), the while loop will run as long as term != 0.

Both the assignment and the test happen in the "while" loop, so the printf() executes four times in this case.

Assignment are also expressions which hold a value: the value they assign. mid=0 is an expressions that evals to 0 (thus false).

You assign term to mid and then test the truth of mid. mid is truthy whenever it is non-zero. The loop terminates when term (and hence mid) has been decremented to equal 0, which is falsy.

You are assigning the value of term to mid, then the while checks the value of mid which evaluates to true, until it reaches 0.
This should output:
3
2
1
0

Related

Why does this create an infinite list of negative odd numbers?

Why does the following instruction create an infinite list of negative numbers (-1, -3, -5, ...) if i=1?
while (i--)
printf("\n%i", --i);
i = 1 // 1
while (i--) // value of 'i--' is 1 is true; side-effect i = 0
printf("\n%i", --i); // print value of '--i' ie -1, side-effect i = -1
while (i--) // value of 'i--' is -1 is true; side-effect i = -2
printf("\n%i", --i); // print value of '--i' ie -3, side-effect i = -3
while (i--) // value of 'i--' is -3 is true; side-effect i = -4
printf("\n%i", --i); // print value of '--i' ie -5, side-effect i = -5
...
Note that side-effect above may occur before or after (or even during) the evaluation of rest of the expression.
There is nothing special. You have negative number of i, which is equal to 1 at the beginning (and then -1,-3,-5,...) because you decrement variable i two times. First, you are doing it in conditional expression: "while(i--)". After it, it happens in while block of code: "printf("\n%i", --i);". Finally, it continues again and again decreasing by 2 since you are using while loop.
The reason is i-- is a post decrement operator. Meaning, the value of i is used in expression followed by the evaluation of the expression itself. This causes the value of i in your comparison to different from the one present in the print statement which makes the infinite loop.
i-- Post decrement operation which tells that the value be used
first followed by the expression evaluation.
--i pre decrement operation which tells that the expression be
evaluated first followed by using the result of the expression.
On each iteration, i decrements 2 times, which is making series 1, -1, -3, and so on. Always positive values which lead to an infinite loop. If you choose i=2 then series will be 2, 0 and that's all. After getting zero while the loop will get exit.
Because you substract 2 from the odd number two it will never be zero on most implementations.
while (i--)
if(i & 1) printf("\n%i", i);
will stop at some point.
The while condition uses post-decrement. That means it tests the original value of i, which is 1, and then decrements the value to 0. Since 1 is truthy, it goes into the loop.
Inside the loop, you print --i. This is pre-decrement, so it sets i to -1, and prints that value.
On the next iteration the same thing happens. It tests -1, which is truthy, decrements it to -2, enters the loop body, decrements it to -3, and prints that.
Eventually your code will run into undefined behavior when i overflows below the smallest negative number. In most implementations it will wrap around and continue the same progression, but the actual behavior is not specific by the language.
i seems to be of type int and is with that a signed integer which also covers a negative range.
The standard mandates that an int needs to be capable to represent at least a range of −32767 to 32767.
If you decrement it, the value can go down to INT_MIN.
If you decrement the value after it has been reached the border of INT_MIN, the behavior is undefined.
i is decremented by 2 in each iteration, so the output shows a decrease in 2er steps.
If the loop is infinite is dependent upon how the implementation treats a decrementation after INT_MIN.
The while condition is always true as long as i is non-zero, which is the case here as i is decremented by 2 at the first iteration and there as well thereafter i is negative.

Confused with pre and postincrements inside IF

int main() {
int x=1;
int a=2;
int b=3;
if(a++>2 && --b<3)
x=5;
printf("%d %d %d",a,b,x);
}
Shouldn't this code return 3 2 5 respectively, instead of 3 3 1?
a++>2
will return false, since 2 is not greater than 2 (remember, a++ means the old value of a will be returned, not the incremented value). Also, ais now worth 3.
Since the && operator is short-circuiting, the --b<3 part will not be evaluated, so b is still worth 3.
The variable x remains unchanged because the conditional was false.
So you do get "3 3 1"
a++>2 is false, and because of short-circuiting, --b<3 is never evaluated. a++>2 is false because a is initially 2, and 2 is not greater than 2. As a side effect, though, a has become 3. Because the condition was false, x is unchanged. Thus the final values of b and x are the same as their initial values, while a has been incremented.
No. Your code explained:
if(a++>2) // a = 2, so 2 > 2? No.
// So the other condition WILL NOT be checked (--b>3), because it's an && condition.
But, a++ increments the 'a', AFTER the expression. So now:
a = 3
b = 3
The code doesn't enter into the if block. So 'x' will never be changed. Then:
a = 3
b = 3
x = 1
If you did:
if(++a>2 ...
The ++a increments BEFORE the expression. So 'a' would be 3 and the if would check '3 > 2'.
The post-increment will be resolved first, then the comparation. That is because of the Operator Precedence Rules.
If you have two operator with different priority, the one with the greater priority will be invoked first, then the other one. If two operator have the same priority, they will be solved according to the corresponding associativity rule.
You can check operators priority and associativity rules in the next link:
http://en.cppreference.com/w/c/language/operator_precedence

Logical operator with Arithmetic operator in C

I came across a question and I wanted to verify whether my assumptions were correct.
Given the following codes:
1. int i = -2, j = 1, ans;
2. ans = i++ || ++j;
3. printf("%d %d %d", i,j, ans);
The output is: -1 1 1
In C language, it seems that only 0 will be treated as false, any other values will be treated as true when used with a logical operator. So I am not doubtful why ans derives to 1 (true || true gives us true(1) )
What I wanted to ask here is: Why is the value of j still 1 and not 2 despite ++j?
Can safely assume that any arithmetic operations after the logical operators || && will only be effective at the line it is used(in this case, line 2), and after which the variable still retain its original value?
|| operator wont do any operation on second operand when first operand is nonzero.
Because, any one of the operand is non-zero then, output will be true in || operator operation. In your code 1st operand is non-zero. Thats why operation on second operand is not performed.
As in Second Line of your Code i.e
ans = i++ || ++j;
first it will check i++ as it is not zero that means it is true.
And in OR Condition if first condition is true it will not check second condition i.e ++j.
Because if first condition is true is doesn't matter, Second condition is TRUE or FALSE it will return a true value. So if First Condition is true it will not check the other condition.
Logical operators short circuit. That is, in the following:
ans = i++ || ++j;
++j will never be evaluated if i++ evaluates to true (non-zero).

Understanding the exit condition of a 'for' loop

I had this question after reading the Stack Overflow quesion Print an int in binary representation using C.
In a user's comment, they posted this for loop, which assigns either a 1 or a 0 to the bit position in order to convert from an int decimal to char * binary.
for(; bits--; u >>= 1)
str[bits] = u & 1 ? '1' : '0';
I understand why there doesn't need to be an initialized value. This is the syntax for a for loop that I've always known:
for ( variable initialization; condition; variable update )
I don't understand how 'bit--' can be an exit condition. Please help me understand how this code works (I tested it, and it is valid).
In C, a value of zero evaluates to "false" in a Boolean context. So when bits-- evaluates to 0, in the context of the loop it evaluates to "false" and terminates the loop.
If you say, for example:
int x = 1;
if (--x)
{
printf("True!\n");
}
else
{
printf("False!\n");
}
It will output "False", because --x evaluates to 0, which is "false" in a Boolean context.
All conditions basically boil down to checking whether something is 0 or not. 0 means false, everything else means true. So that loop will break when bits is 0.
You will sometimes see while or if conditions written
if (variable) // or while(variable)
That is just shorthand for
if (variable != 0) // or while (variable != 0)
So
for (; bits--; u >>= 1)
is short for
for (; bits-- != 0; u >>= 1)
bits-- is an assignment expression of type int (since it will return the value of b, which is int). To match the for loop syntax, it gets converted to a Boolean expression, which means it is true if bits != 0.
In fact, the condition is identical to bits!=0, but by using bits--, it changes the value of bits at the same time, making the code more compact. That's all.
As others said, in C, you can use integers as a condition - 0 or false, and anything else for true. (Actually, you almost always do it - even an expression like a<b is an int.)
So, the loop will end when bits-- will be 0.
When the -- operator comes after the variable, it decreases the variable, and gets the previous value of it. For example, if you have int a=3,b; b=a--;, then b will be 3, and a will be 2.
So, the loop will exit after that bits will been decreased from 0 to -1.
That means that, if in the beginning, bits==8 (for example), the loop will iterate 8 times, when in the first, bits will be 7 (because the condition had checked), and in the last, bits will be 0. It is a nice way to loop through an array (Since in C, an array of bits variables is being indexed from 0 to bits-1).

I am getting a strange output in C?

Here is a c program.I am getting a strange output.
When num1=10 and num2=20->
#include<stdio.h>
void main()
{
int num1=10,num2=20;
clrscr();
if(num1,num2)
{
printf("TRUE");
}
else
{
printf("FALSE");
}
getch();
}
Output:
TRUE
when num1=0 and num2=220
Output:
TRUE
But when num1=0 and num2=0:
Output:
FALSE
Why does this happen?
also,what does this given below code mean:
if(num1,num2)
Thanks in advance!
You're using the comma operator. That operator first evaluates its first operand, then drops the result on the floor and proceeds to evaluate and return its second operand.
That's why your program only prints FALSE if num2 evaluates to false in a boolean context (like e.g. 0, 0.0 or NULL).
In:
if(num1,num2)
the last expression overrides all preceeding ones so it's the same as:
if(num2)
since, num2 is 0, you get FALSE.
If you check this out,
http://msdn.microsoft.com/en-us/library/2bxt6kc4(v=vs.71).aspx
the , stands for sequential evaluation, meaning the expressions are evaluated one after another, the last being your num2.
Learn about comma operator in c http://en.wikipedia.org/wiki/Comma_operator.
i=(a,b) means store b in i.
Everything else than 0 in c is true.
so if(3) if (-3) all are true
only if(0) is false
if(num1,num2)
Is a use of the comma operator. The Comma operator calculates the first operand and discards the result then the second operand and returns the result. Thus (a, b) calculates a, calculates b and then returns b.
This should clear up your confusion for the logical cases, in each of them the statement has the effect of looking at the value of b.
if(num1,num2)
is not a syntax error, but it is a logic error. Basically, this will resolve to being only
if(num2)
Only the last variable is evaluated.
I assume what you want is 'if a and b are true'. The comma like you are using means to evaluate just the last variable.
What I think you want is:
if(num1 && num2) /* num1 AND num2 */
You need to use && (logical AND) not a single & (Which is bitwise AND)

Resources