0

C programming(logical operator)

how is this being executed?? please explain.... #include<stdio.h> int main() { int i = -1, j = 1, k ,l ; k = !i && j ; l = !i || j ; printf ( "%d %d", i, j ) ; return 0; }

5th May 2018, 8:37 AM
Saurabh Thakur
Saurabh Thakur - avatar
5 odpowiedzi
+ 3
@ifl in c actually 0 is false and anything else is true generally a boolean is true if its value is 1 and false if its value is 0. Also your && or || operators will return 0 or 1 depending on the result if it's true or not Just made a small code to show: https://code.sololearn.com/c7y8sE0bktk0/?ref=app
5th May 2018, 9:39 AM
Aaron Eberhardt
Aaron Eberhardt - avatar
+ 1
@Aaron ... duh! Yes correct I meant non zero values are true. Changed my sentence mid way and did not check before posting. Thanks for the correction
5th May 2018, 9:44 AM
ifl
ifl - avatar
0
the last line prints i and j which stay unchanged . so -1 and 1. in C, non zero values are true, so k is zero and l is 1 (edited to correct typo)
5th May 2018, 9:31 AM
ifl
ifl - avatar
0
if there's k=!5 &&j??
5th May 2018, 9:36 AM
Saurabh Thakur
Saurabh Thakur - avatar
0
in : k=!5&&j !5 is false and then j is true false && true is false So as an integer, k is then 0
7th May 2018, 10:18 AM
ifl
ifl - avatar