See the below c program(why the program is ending at 1 and not going upto -infinity?what is the meaning of if(--i)??? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

See the below c program(why the program is ending at 1 and not going upto -infinity?what is the meaning of if(--i)???

#include <stdio.h> int main() { static int i=5; if(--i){ printf("%d\n",i); main(); } } https://code.sololearn.com/cYd6UzlumKli/?ref=app

27th Apr 2021, 1:39 AM
Vatsav
8 Answers
0
Hey Vatsav prefix (--i) decrement 1 from " i " , and any non-zero value is true value for conditional expression , so it print 5,4,3,2,1 and at 0 it becomes false so terminates !
27th Apr 2021, 2:04 AM
Giriraj Yalpalwar
Giriraj Yalpalwar - avatar
+ 2
If it was i++ or ++i then it can make upto infinity You can see the above explanation for your understanding.
27th Apr 2021, 1:58 AM
Aditya
Aditya - avatar
+ 1
its a prefix expression. examples: i++ i-- --i ++i the first couple, increments/decrements the variable after loop completes the second couple increments/decrements before the variable is read
27th Apr 2021, 1:56 AM
Slick
Slick - avatar
+ 1
Hey Thirt13n If should have a condition right? But here there only an decrement function how can we say that condition is true for I>0
27th Apr 2021, 2:09 AM
Vatsav
+ 1
Why is it not true for zero please explain
27th Apr 2021, 2:15 AM
Vatsav
0
Hey Vatsav Any non-zero value is true for conditional expression !
27th Apr 2021, 2:11 AM
Giriraj Yalpalwar
Giriraj Yalpalwar - avatar
0
Vatsav Zero(0) mean false !
27th Apr 2021, 2:16 AM
Giriraj Yalpalwar
Giriraj Yalpalwar - avatar
0
Ok ok got it thanks ☺️☺️☺️
27th Apr 2021, 2:17 AM
Vatsav