What a " , " does in a while loop? [duplicate] - c

This question already has answers here:
What does the comma operator , do?
(8 answers)
Closed 5 years ago.
I accidentally typed while(x,0) instead of while(x<0). The code of course didn't work as planned nor got compiler errors so i took a damn hour to find the mistake.
Why it didn't get a compiler error? And what does the , do in the while loop?

while(x,0)
comma is treated as binary operator and it will return 0, so your loop condition will go false.

In C, a comma is "a binary operator that evaluates its first operand and discards the result, and then evaluates the second operand and returns this value (and type)."
Since 0 is false then your code was exiting the loop.
https://en.m.wikipedia.org/wiki/Comma_operator

Related

Assignment operator works fine as a condition in if Statement in C programs [duplicate]

This question already has answers here:
How the assignment statement in an if statement serves as a condition?
(4 answers)
Closed 2 years ago.
I am aware that when a non zero value is provided as a condition in an if statement, the condition is evaluated to be true.
Yet, I'm wondering how an assignment(=) is evaluated to be true here, instead of a comparison(==), and runs without any errors in the below C program.
int main(){
int a, b =5;
if(a=10) {
("This works");
}
if(b=1){
("This works too");
} }
The assignment operator always returns the value that was assigned. So in your case a=10 has the value 10 and the if statement is essentially if(10). Which is true.
a = 10 is an expression that assigns 10 to a and whose value is the result of the assignment (that is 10).
any value different from 0 is considered to be true
Try this:
if (b = 0) {
("This is never displayed");
}

Using an Int in the if condition [duplicate]

This question already has answers here:
why negative number is taken as true in condition statement? [duplicate]
(5 answers)
Closed 2 years ago.
In my current class I am seeing a lot of times where my teacher puts a number as the condition of an if statement then asks for the output. I'm confused as to how these statements are being evaluated.
Example:
if(-1) {
x = 35;
}
or
if(0) {
x = 35;
}
Why does it go inside the if statement instead of just ignoring it? What is making if(-1) evaluate true? And why does if(0) evaluate false?
This is an online class so I can't get help directly from the teacher usually. I have searched for a few hours on various websites about C for loops, but I haven't found anything relevant yet to my question.
C doesn't have a "real" boolean type. When evaluating integers, 0 is a "false", and anything else is a "true" value.

How do I calculate the output [duplicate]

This question already has answers here:
Is short-circuiting logical operators mandated? And evaluation order?
(7 answers)
Closed 4 years ago.
#include<stdio.h>
void main()
{
int x=3,y=2,z=0,m;
m=++x || ++y && ++z;
printf("\n %d %d %d %d\n",x,y,z,m); // 4 2 0 1
}
The output of the following code is mentioned as comment in program and I am trying to evaluate how this answer came but I am not able to understand.
I just wanted to know how the program calculates the relative value.
thanks to pmg, i have corrected my original answer (i had an error)
Because the left side of the OR operator (||) is not zero, it doesn't evaluate anything else on that line. This is called a "short circuit operator". In this example you gave, the programmer basically is tricking the compiler. If the argument on the right of the operator does not affect the outcome, it will not execute that code. However in this case, there are increments going on there and they won't be evaluated either.
This will assign "1" to m.
your output should be 3, 2, 0, 1.

What does the expression (i,j) in c signifies? [duplicate]

This question already has answers here:
What does the comma operator , do?
(8 answers)
Closed 6 years ago.
I tried executing the following c code:
#include <stdio.h>
int main()
{
int i=2000,j=100;
printf("%d",(j,i));
}
I gave different values to i and j and found the output. I always get the output as the value contained in the second variable. Does the expression always give the last variable as a result or does it have any other meaning?
You are using the comma operator.
The output of your code is 2000, because the comma operator evaluates the two expressions and returns the value of the second operand. More details can be found in this SO answer.

C: Ternary operator giving error when leaving one expression empty [duplicate]

This question already has answers here:
Why would you use the ternary operator without assigning a value for the "true" condition (x = x ?: 1)
(7 answers)
Closed 10 years ago.
I am attempting to use some code utilising a ternary statement in order to run a specific part, however, when I do this, I get the warning:
expression result unused
And the code in that particular section does not run.
The code in this case is:
i != a ?: printf("|%*s\\\n", i, "");
Why would that be? According to here, this form of the ternary operator, in which there is no alternative for the case, should function, however, it simple skips over it here. Any help is appreciated.
Your code is equivalent to
(i != a) ? (i != a) : printf(...);
Note that you won't end up using the i != a result, hence the warning. Best to write this as an if statement:
if(i==a) printf(...);

Resources