Why does (I==1; I<=3; I++) printf ("hello") print hello four times instead of three times?

1/22/2020 4:07:17 PM


8 Answers

New Answer


Assumming l = 0 the expression l == 1 has no effect. It is comparison operation not assignment. The loop runs from 0 to 3 (inclusive because of l<=3) now count how many numbers are there from 0 to 3 (inclusive) There are four no. hence loop runs 4 times


Avinesh The person probably didn't realized that she has written l==1 instead of l = 1


~ swim ~ that seems to obvious if that is what the learner is looking for. ASIYA SHAIKH make sure that the question you posted is right and let the community know if you have received the answer you are looking for by acknowledging it.


~ swim ~ But then it has to be I = 0 for it to print it 4 times, another mistake.


~ swim ~ Agreed as well, but she has some searching and sorting algorithms in her code bits which made me think that this should have been simple for her but the question itself seems wrong.


It is assumed that I has been declared and is equal to 0 when this first executes. If I is a stack variable though, this can not be guaranteed and can be any value. The code I==1 simply returns 0 (false) because it is not an assignment, but rather a test. A compiler should warn that I==1 has no effect in this case.it’s also missing the for. The line should be: for (I=1; I<=3; I++) printf ("hello");


Avinesh Agreed and in my original comment i have mentioned "assuming l = 0" She didn't replied back so i assume that was the case


Maybe i == 0 ; = false = 0 ? And i<=3 is 3 inclusive and 0123