+ 3

What are errors in this code segment?

for(p=10;p>0;) p=p-1; printf("%f",p);

17th Feb 2018, 4:27 PM
Anshul Sharma
Anshul Sharma - avatar
2 Respuestas
+ 2
1. If you want more than one statement of code to run in the for loop (or any loop, if, else etc) you need to enclose the code between curly braces { and }. 2. It looks like you're trying to print out an integer using %f which is meant for a float. Use %d instead. You could also just add your decrementing as the last statement in the parentheses. #include <stdio.h> int main() { int p; for(p = 10; p > 0; p--) { printf("%d\n", p); } return 0; }
17th Feb 2018, 4:45 PM
ChaoticDawg
ChaoticDawg - avatar
+ 1
for (int p = 10; p < 0;) p -= 1; printf(“%d”, p); The first problem is that you didn’t declare p, unless that was earlier in the program. The second line works fine, but I replaced it because it is easier to write it this way and does the same thing. For the third line, %f is for float or double, not int which I assumed p was. %d is for int.
17th Feb 2018, 4:40 PM
Jacob Pembleton
Jacob Pembleton - avatar