#include <stdio.h>
void main()
{
int x;
for(x=10;x;x--)
printf("%d\n",x);
}
In the above program, the output is printing from 10 to 1. But I didn't give any lower limit. How is it printing like that?
Regards
Kokila
#include <stdio.h>
void main()
{
int x;
for(x=10;x;x--)
printf("%d\n",x);
}
In the above program, the output is printing from 10 to 1. But I didn't give any lower limit. How is it printing like that?
Regards
Kokila
You did give a lower limit. The loop stops when x is false, which reached as x is 0.
Let's compare it with a typical condition and let's see why they both work:
for(x=10;x;x--)
versus
for(x=10; x > 0; x--)
Notice the for loop will run when it is TRUE or a non-zero value.Code:x x > 0 10 1 9 1 8 1 7 1 6 1 5 1 4 1 3 1 2 1 1 1 0 0
Try not.
Do or do not.
There is no try.
- Master Yoda
>>void main()
main() returns an int, so make it
>int main(void)
and return 0; at the end.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
Thanks for all the responses.