This question already has answers here:
What does the comma operator , do?
(8 answers)
Closed 3 years ago.
One of my friends ask me about this code and I and he are unable to find out, what is happening in the if condition. Can you guys explain how this condition works?
int main()
{
int i;
if (i = (1, 2, 0))
printf("Mehrose");
else
printf("Hello ");
printf("%d\n", i);
return 0;
}
The output for this code is Hello 0
First, formatting as the compiler sees the code we get:
int main(void)
{
int i;
if(i=(1,2,0))
printf("Mehrose");
else
printf("Hello");
printf("%d\n",i);
return 0;
}
The if statement can be broken down:
Comma operator , is evaluated first, left-hand side of the operator is discarded. This repeats for each operator:
if(i=(1,2,0))
if(i=(2,0))
if(i=0)
The assignment operator = assigns a value of 0 to i, and returns the right-hand side of the expression:
if(0)
Recall that 0 is evaluated as false (is "falsy") and 1 is evaluated as true (is "truthy"). Thus the first condition fails and the second block is executed. "Hello" is printed to the standard output stream, followed by "0".
In the expression,
i=(1,2,0)
you're using the comma operator, which evaluates all its operands and yields the result of its rightmost operand - which is 0 here.
So 0 is assigned to i.
So it's equivalent to if (i = 0), which assigns 0 to i and yields the value of i which is false and thus it prints the string in the else branch.
Related
Studying for a computer science final.
I really cannot figure this question out.
What will be the output of this C program?
#include<stdio.h>
int main()
{
int i = 0;
while(i < 4, 5)
{
printf("Loop ");
i++;
}
return 0;
}
A. Infinite Loop
B. Loop Loop Loop Loop Loop
C. Loop Loop Loop Loop
D. Prints Nothing
Upon execution it prints loop for infinite times. Why is that happening? Why is there a comma inside the arguments of While loop? What does it do?
It will loop forever, because the condition of the while loop i < 4, 5 evaluates to 5, which is different than 0, therefore is considered true in C.
To learn more about that, read about the comma operator: https://en.wikipedia.org/wiki/Comma_operator
Briefly, when the comma operator is used, all of its operands are evaluated but the whole expression takes the value of the last one. For example:
int val = (1, 2, 3);
printf("%d\n", val);
Will print 3.
What you have in the while loop's condition is the comma operator, which evaluates its operands and yields the value of its right most operand.
In your case, it evaluates i < 4 condition and discards it and then evaluates the condition to just 5. So it's essentially equivalent to:
while(5)
{
printf("Loop ");
i++;
}
Which obviously results in infinite loop as the condition is always true. (remember that any non-zero value is always "true" in C). There's also a possible integer overflow due to i getting incremented in the infinite loop.
This question already has answers here:
How does the Comma Operator work
(9 answers)
Closed 3 years ago.
Program code :
int main()
{
int i;
for (i = 0; i < 0, 5; i++)
printf("%d ", i);
return 0;
}
The loop above is executed infinite times.
What does i<0,5 mean, and how it is evaluated?
Based on operator precedence, this is interpreted as (i < 0), 5. The comma operator evaluates all statements, but discards their values except the last. So for practical purposes the loop reads as
for (int i = 0; 5; ++i) {...}
Because a non-zero value is interpreted as true in C/C++, this is equivalent to:
for (int i = 0; true; ++i) {...}
which is an infinite loop.
why i<0,5 how it is evaluated?
Because the value of a comma expression is its last value. The values in this comma expression arei < 0 and 5. This first is evaluated to false and thrown away! The second (and last) is 5 which is true. That is used as the for loop condition expression.
A for loop runs until its condition expression is false, so this loop run forever.
In the condition of the loop there is used the comma operator
for (i = 0; i < 0, 5; i++)
printf("%d ", i);
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
So this expression with the comma operator
i < 0, 5
has two operands and can be rewritten for clarity like
( i < 0 ), ( 5 )
So the value of the expression according to the quote from the C Standard is the value of the second operand that is 5.
As 5 is not equal to 0 then the condition always evaluates to logical true and you have an infinite loop.
From the C Standard (6.8.5 Iteration statements)
4 An iteration statement causes a statement called the loop body to
be executed repeatedly until the controlling expression compares equal
to 0. The repetition occurs regardless of whether the loop body is
entered from the iteration statement or by a jump.155
It is very easy to check yourself. Try:
int main()
{
int i, y;
for (i = 0; i < 0, 5; i++)
{
y = (i < 0, 5);
printf("%d ", y);
}
return 0;
}
And you know that second expression in your list is the value of the comma expression.
Studying for a computer science final.
I really cannot figure this question out.
What will be the output of this C program?
#include<stdio.h>
int main()
{
int i = 0;
while(i < 4, 5)
{
printf("Loop ");
i++;
}
return 0;
}
A. Infinite Loop
B. Loop Loop Loop Loop Loop
C. Loop Loop Loop Loop
D. Prints Nothing
Upon execution it prints loop for infinite times. Why is that happening? Why is there a comma inside the arguments of While loop? What does it do?
It will loop forever, because the condition of the while loop i < 4, 5 evaluates to 5, which is different than 0, therefore is considered true in C.
To learn more about that, read about the comma operator: https://en.wikipedia.org/wiki/Comma_operator
Briefly, when the comma operator is used, all of its operands are evaluated but the whole expression takes the value of the last one. For example:
int val = (1, 2, 3);
printf("%d\n", val);
Will print 3.
What you have in the while loop's condition is the comma operator, which evaluates its operands and yields the value of its right most operand.
In your case, it evaluates i < 4 condition and discards it and then evaluates the condition to just 5. So it's essentially equivalent to:
while(5)
{
printf("Loop ");
i++;
}
Which obviously results in infinite loop as the condition is always true. (remember that any non-zero value is always "true" in C). There's also a possible integer overflow due to i getting incremented in the infinite loop.
This question already has answers here:
C comma operator
(4 answers)
Closed 5 years ago.
Whats the difference between:
int i;
for( i=0;i<5 && i<3;i++)
{
printf("stackoverflow.com");
}
and
int i;
for( i=0;i<5 , i<3;i++)
{
printf("stackoverflow.com");
}
I mean use of && instead of ','
Regards
In the second code block, only i < 3 is actually used to evaluate whether the loop should exit. The i < 5 expression is evaluated, but its value is discarded. See the comma operator for more info.
There is functionally no difference between your examples because i < 3 is the limiting expression and appears second. Both loops will exit when i reaches 3. However, if you switched the terms so that you have i < 3, i < 5, then the second kicks out when it reaches 5 because only the value of i < 5 is considered.
In this case they will do exactly the same.
The thing that differs is that in the first case i<3 will not be evaluated every time. A statement on the form A && B will only execute B if the return value of A is true. The reason is that the logical value of && is true if both operands are true. If one of them are false, the whole expression is false, so if the left operand evaluates to false, there's no need to evaluate the right operand.
The comma operator on the other hand evaluates both operands, but the result of the left operand is discarded.
When using the comma operator , in a statement like the:
( <expression1>, <expression2> )
the <expression1> is evaluated and discarded, then the <expression2> is evaluated and its value is returned. In summary the right most expression is evaluated and its value returned. Other expressions are evaluated and discarded. So your statement of:
for(i=0; i<5 , i<3; i++)
is equivalent to:
for(i=0; i<3; i++)
As for the first statement, this expression:
(i<5 && i<3)
is a simple AND boolean evaluation. A funny one because it is enough to say:
(i<3)
Long story short, who ever made this example probably expects you to say "both conditions evaluate to second expression".
This question already has answers here:
What is the difference between ++i and i++?
(20 answers)
Closed 7 years ago.
I was toying with the concept of array pointers. I wrote this simple program:
#include <stdio.h>
int main (int argc, char **argv){
char s[] = "Hello world!\n";
char *i;
for (i = s; *i; ++i){
printf(i);
}
return 0;
}
which gives the very amusing output:
Hello world!
ello world!
llo world!
lo world!
o world!
world!
world!
orld!
rld!
ld!
d!
!
When I wrote this, however, I was under the impression that the output would start from the second row. Reason being, in the for loop, I use a pre-increment notation. i is set to the beginning of s, the Boolean condition is checked and it holds true, then i gets incremented and the block executes.
That was my impression, but obviously it is erroneous as the block executes before i gets incremented. I rewrote the program using a post-increment notation and got exactly the same result which confirms my hypothesis. If that is the case, then how are they treated differently in this program?
for (initialization; condition; increase) statement;
This for works in the following way:
initialization is executed. Generally, this declares a counter variable, and sets it to some initial value. This is executed a single time, at the beginning of the loop.
condition is checked. If it is true, the loop continues; otherwise, the loop ends, and statement is skipped, going directly to step 5.
statement is executed. As usual, it can be either a single statement or a block enclosed in curly braces { }.
increase is executed, and the loop gets back to step 2.
the loop ends: execution continues by the next statement after it.
There is no difference between postincrement and preincrement because increase is executed separately anyway.
The increment expression of the for loop is executed after the body. For your case
for (i = s; *i; ++i){
printf(i);
}
is similar to
i = s; // loop init
while (*i) // loop condition
{
printf(i);
++i; // loop increment
}
i is set to the beginning of s, the Boolean condition is checked and it holds true, then i gets incremented and the block executes.
Not quite. The actual syntax is
i is set to the beginning of s
the Boolean condition is checked
2.1. if it holds true the block executes,
2.2. come out of loop otherwise.
then i gets incremented and continue to step 2.
Note: In this particular scenario, pre and post increment to i are not going to make any difference.
the equivalent of
for( a ; b ; c )
{
statements
}
is
a ;
while( b )
{
statement
c ;
}
and
++i ;
i++ ;
are the same things, because i is only evaluated and the evaluation isn't used
Reason being, in the for loop, I use a pre-increment notation. i is set to the beginning of s, the Boolean condition is checked and it holds true, then i gets incremented and the block executes.
No. i is incremented after the block executes.
I rewrote the program using a post-increment notation and got exactly the same result which confirms my hypothesis. If that is the case, then how are they treated differently in this program?
They're not. Post-increment or pre-increment isn't going to make a blind bit of difference. The difference between those two is in the result of the expression i++, ++i (namely, will it evaluate to the previous value or to the new value?). It does not magically alter the entire flow of an encapsulating control structure. The evaluated result of the increment expression is just thrown away so it doesn't matter what you do as long as you provide an expression that results in incrementing i.
It's like how this:
int main()
{
int x = 5;
int y = 5;
int a = x++;
int b = ++y;
}
will result in different values for a and b because the result of the increment expression is used, whereas the following programs:
int main()
{
int x = 5;
x++;
}
int main()
{
int x = 5;
++x;
}
are identical.
i gets incremented after the block get executed.
Try this:
for(int i = 0; i < 5; ++i )
{
printf("\n %d",i);
}