Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why does this print? (C language)

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);
}
like image 417
wadafarfar Avatar asked Jan 24 '26 07:01

wadafarfar


1 Answers

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

like image 88
linuxuser27 Avatar answered Jan 26 '26 21:01

linuxuser27