-1
#include <wiringPi.h>
#include <stdio.h>
#define ledPin 0

main()
{
    wiringPiSetup()
    int x;
    for(x=0; x<4; x+1)
    {
        digitalWrite(ledPin, HIGH);
        delay(500);
        digitalWrite(ledPin, LOW);
        delay(500);
    }
}    

the error is on line 7 and i've been stuck on it for 2 days (i code in geany)

  • `x+1` doesn't do anything, did you mean `x += 1`? – user3386109 Sep 19 '22 at 19:40
  • This loop will never exit, use `x++`, `++x` or `x += 1` – Arkadiusz Drabczyk Sep 19 '22 at 19:40
  • I'm surprised it is showing an error at all – Eugene Sh. Sep 19 '22 at 19:43
  • 5
    No semicolon after `wiringPiSetup()` – dimich Sep 19 '22 at 19:46
  • @user3386109 I get: `warning: statement with no effect [-Wunused-value]` – Craig Estey Sep 19 '22 at 19:50
  • @CraigEstey Yup, but as dimich points out, the error on line 8 (which is the `int x;`) is due to the missing semicolon on line 7. So the alleged error message in the title seems a little suspect (there is no loop on line 8). – user3386109 Sep 19 '22 at 19:55
  • @user3386109 I'm on linux PC, so I had to remove any Pi specific lines (including the `wiringPiSetup()` _without_ the `;`) to get to the warning for the `x+1`. I'm guessing the omitted semicolon was a pasting error. OP didn't post the real/exact error as [AFAIK] there is no compiler error: "bad for loop" ;-) – Craig Estey Sep 19 '22 at 20:01
  • It is possible the error line is one off. I’ve seen it before for a variety of reasons, including the possibility that OP just skipped down the list of errors he understood to this one. (One of my first experiences with C was spending two days trying to understand a compiler error because _I forgot a semicolon_.) – Dúthomhas Sep 19 '22 at 20:03
  • @CraigEstey Agreed that "bad for loop" is not an error message that any sensible compiler would emit, which is the other reason the title is a little suspect. – user3386109 Sep 19 '22 at 20:09
  • @CraigEstey - you always need a Pi hanging around somewhere on the network -- for just such an occasion. (and if you haven't messed with one (from a programming/GPIO standpoint -- they are a lot of fun and incredibly capable)). And the Pi pico -- at $4 and the size of a stick of chewing gum -- can't be beat for a dual-core microcontroller `:)` – David C. Rankin Sep 19 '22 at 21:50
  • If you are going to write in C, stop by [The Definitive C Book Guide and List](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) and pick up a good book or reference. There is no guessing in C. Every character in every line must be valid. A good book removes the frustration from learning any language. (avoid coding challenge sites, they are not there for learning C and many promote code containing marginal or just plain bad coding practices) Using the Pi references and examples are fine -- but they only address Pi specific aspects of C. – David C. Rankin Sep 19 '22 at 21:55
  • @user3386109 yep i messed up the exact line (was line 8 but i trimmed down a useless line so is now line 7) – CactusCodesinC Sep 19 '22 at 22:07
  • @DavidC.Rankin I've used RPi at work. But, I need one for home. I want a maxed out RPi4. But, also a 4 port HDMI KVM. 8 port Ethernet switch. Micro SD card writer. And, nVidia Jetson [nano or newest equiv]. For here on SO, maybe an STM32*, beaglebone, but _never_ a PIC* (we _must_ bury the [walking] dead, you know). Just got a new 27" LCD monitor (to replace dead 24"). And, I've been nursing a 2010 era 8 CPU desktop with 22TB of disk. Oh, yeah: A new all-in-one color laser printer/scanner/fax. So, my list of toys for Santa to bring is extensive. I've been a good boy, so, maybe ... – Craig Estey Sep 19 '22 at 23:33
  • @CraigEstey Here's to hope! No knocking the 8 CPU 2010 error hardware. I have 2 super-micro rack mounts from that time, a 4U and 2U with only 1/2 your storage (Linux RAID). But Quad-Quad core boxes are fun to play with. Hopefully Santa can find an RPi4 or Beaglebone for his sack. (was easier when my little brother was an EE at TI). Until then, I'll make due with the 3B+, MSP432, and Pico. It takes me at least that long just to learn all the registers. Pico has an amazing hardware pio with 8 state machines (programmed in assembly) that can each function as a CPU. More to learn... – David C. Rankin Sep 19 '22 at 23:59

2 Answers2

1
  1. It sounds like you're getting a compile error on line 8:

    wiringPiSetup()  /* <-- You need to end the line with ";" */
    
  2. Your loop should look like this:

    for(x=0; x<4; x++) {...} /* "x+1" doesn't change the value of "x", so the loop will never terminate */
    
paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • Thanks but when i put a semicolon at the end of that it returns another error so i let it blank but i’ll try your fix asap – CactusCodesinC Sep 19 '22 at 21:56
0

The problem is in your for loop.

for(x=0; x<4; x+1)

The third parameter in brackets does not do anything. You probably wanted to increment x and you will do that with x++ or x+=1.

x+1 will return the value, but that will not be stored anywhere.

User19818359
  • 73
  • 1
  • 6