Question

From error I think code in print() is not seeing the counter in for().n.b. code is for Propeller microcontroller. for() worked without problems in past. Much thanks

#include "simpletools.h"  // for print on my microcontroller platform
void main(void) {
    for(int x = 1; x <= 20; x++); {
        // *** error next line> error: 'x' undeclared (first use in this function)
        print("x = %d\n", x);
        high(14);
        pause(500);
        low(14);
        pause(500);
    }
}
Was it helpful?

Solution

Try removing the semicolon after for but before the {.

Change this:

for(int x = 1; x <= 20; x++); {

to this:

for(int x = 1; x <= 20; x++) {

The ; ends the for loop. What you have done in your code is have a complete for loop followed by an unrelated block of code. The x is only visible within the scope of the for loop, which means up to that errant semicolon.

OTHER TIPS

You can also try this:

void main(void) {
    int x = 1;
    for(; x <= 20; x++) {
        print("x = %d\n", x);
        high(14);
        pause(500);
        low(14);
        pause(500);
    }
}

Your question said "C Language" right? So I think you should declare the value "int x" on top first.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top