Expected primary-expression before '/' token

I haven't done any programming for a while and I cannot understand this.....

I get the compile error expected primary-expression before '/' token at the line with HERE in the comment in this srtn, yet the similar commands above it are OK.

void getDigits()
{
float result = hh/10;   //=2.1           THIS IS
digit0 = result;        // it's int so 2      H
int remain = hh%/10;    // it's int so 1      H
digit1 = remain;

float result = mm/10;   // = 1.6
digit2 = result;        // it's int so 1      M
int remain = mm%/10;    // it's int so 6      M
digit3 = remain;

float result = ss/10;   // = 3.2
digit4 = result;        // it's int so 3      M
int remain = ss%/10;    //<<<< HERE
digit5 = remain;

/*
float result = ss/10;    // = 3.2
digit4 = result;         // 3                 S
int remain = ss%/10;     //<<<<< and HERE
digit5 = remain;      */
}

In my futile testing, I duplicated the commands above the problem area and retested line by line. The new lines were OK but, when I deleted the old stuff, the problem returned. In the code above, if I uncomment the original commands, the error is removed from the line "HERE" and returns at the line "and HERE". The commands look identical to me and I don't understand why the error only turns up at the last command of that type.

Either % or / :wink:

And you have a stack of re-declarations of variables.

assuming everything is a global variable, you could do

void getDigits()
{
  digit0 = hh / 10;
  digit1 = hh % 10;

  digit2 = mm / 10;
  digit3 = mm % 10;

  digit4 = ss / 10;
  digit5 = ss % 10;
}

1 Like

Thank you, both. That section is now fine. What I couldn't understand is why only the last command was in error. I can only assume that, for this part of the programme, the verifier starts at the bottom and works up - which explains what happened when I commented stuff out.

The compiler starts from the top.
It reports the point where it finds something it cannot handle.
It can be rather cryptic. But the error is always at or before the given point.
Especially missing {} and ; will give cryptic messages. Almost never: hey, I suspect a missing } here.

Note you can check if you have a missing number of opening braces or closing braces, by clicking your cursor on the opening brace and the matching closing brace (if any) will be outlined.

It will help you track down problems like this.

Also it helps if you post all your code, because it is very likely your errors are in the bit you didn't post.

No it is the other way round, it starts at the top and works down.

OK, but this does not explain why the compiler passed over two identical bad commands and only declared an error on the third. Also, when I commented out the last instance, the compiler declared the second instance in error, when it had previously declared it kosher.

I compiled the below based on your example; ignore the fact that the variables are not initialised.

void setup()
{
  // put your setup code here, to run once:
}

void loop()
{
  // put your main code here, to run repeatedly:
}

void getDigits()
{
int digit0, digit1, digit2, digit3, digit4;
int hh, mm, ss, s;


  float result = hh / 10;  //=2.1           THIS IS
  digit0 = result;         // it's int so 2      H
  int remain = hh % / 10;  // it's int so 1      H
  digit1 = remain;

  float result = mm / 10;  // = 1.6
  digit2 = result;         // it's int so 1      M
  int remain = mm % / 10;  // it's int so 6      M
  digit3 = remain;

  float result = ss / 10;  // = 3.2
  digit4 = result;         // it's int so 3      M
  int remain = ss % / 10;  //<<<< HERE
  digit5 = remain;

  /*
float result = ss/10;    // = 3.2
digit4 = result;         // 3                 S
int remain = ss%/10;     //<<<<< and HERE
digit5 = remain;      */
}

The result:

C:\Users\bugge\AppData\Local\Temp\.arduinoIDE-unsaved2025529-16556-5yud42.l7p0b\sketch_jun29b\sketch_jun29b.ino: In function 'void getDigits()':
C:\Users\bugge\AppData\Local\Temp\.arduinoIDE-unsaved2025529-16556-5yud42.l7p0b\sketch_jun29b\sketch_jun29b.ino:19:21: error: expected primary-expression before '/' token
   int remain = hh % / 10;  // it's int so 1      H
                     ^
C:\Users\bugge\AppData\Local\Temp\.arduinoIDE-unsaved2025529-16556-5yud42.l7p0b\sketch_jun29b\sketch_jun29b.ino:22:9: error: redeclaration of 'float result'
   float result = mm / 10;  // = 1.6
         ^~~~~~
C:\Users\bugge\AppData\Local\Temp\.arduinoIDE-unsaved2025529-16556-5yud42.l7p0b\sketch_jun29b\sketch_jun29b.ino:17:9: note: 'float result' previously declared here
   float result = hh / 10;  //=2.1           THIS IS
         ^~~~~~
C:\Users\bugge\AppData\Local\Temp\.arduinoIDE-unsaved2025529-16556-5yud42.l7p0b\sketch_jun29b\sketch_jun29b.ino:24:7: error: redeclaration of 'int remain'
   int remain = mm % / 10;  // it's int so 6      M
       ^~~~~~
C:\Users\bugge\AppData\Local\Temp\.arduinoIDE-unsaved2025529-16556-5yud42.l7p0b\sketch_jun29b\sketch_jun29b.ino:19:7: note: 'int remain' previously declared here
   int remain = hh % / 10;  // it's int so 1      H
       ^~~~~~
C:\Users\bugge\AppData\Local\Temp\.arduinoIDE-unsaved2025529-16556-5yud42.l7p0b\sketch_jun29b\sketch_jun29b.ino:24:21: error: expected primary-expression before '/' token
   int remain = mm % / 10;  // it's int so 6      M
                     ^
C:\Users\bugge\AppData\Local\Temp\.arduinoIDE-unsaved2025529-16556-5yud42.l7p0b\sketch_jun29b\sketch_jun29b.ino:27:9: error: redeclaration of 'float result'
   float result = ss / 10;  // = 3.2
         ^~~~~~
C:\Users\bugge\AppData\Local\Temp\.arduinoIDE-unsaved2025529-16556-5yud42.l7p0b\sketch_jun29b\sketch_jun29b.ino:17:9: note: 'float result' previously declared here
   float result = hh / 10;  //=2.1           THIS IS
         ^~~~~~
C:\Users\bugge\AppData\Local\Temp\.arduinoIDE-unsaved2025529-16556-5yud42.l7p0b\sketch_jun29b\sketch_jun29b.ino:29:7: error: redeclaration of 'int remain'
   int remain = ss % / 10;  //<<<< HERE
       ^~~~~~
C:\Users\bugge\AppData\Local\Temp\.arduinoIDE-unsaved2025529-16556-5yud42.l7p0b\sketch_jun29b\sketch_jun29b.ino:19:7: note: 'int remain' previously declared here
   int remain = hh % / 10;  // it's int so 1      H
       ^~~~~~
C:\Users\bugge\AppData\Local\Temp\.arduinoIDE-unsaved2025529-16556-5yud42.l7p0b\sketch_jun29b\sketch_jun29b.ino:29:21: error: expected primary-expression before '/' token
   int remain = ss % / 10;  //<<<< HERE
                     ^
C:\Users\bugge\AppData\Local\Temp\.arduinoIDE-unsaved2025529-16556-5yud42.l7p0b\sketch_jun29b\sketch_jun29b.ino:30:3: error: 'digit5' was not declared in this scope
   digit5 = remain;
   ^~~~~~
C:\Users\bugge\AppData\Local\Temp\.arduinoIDE-unsaved2025529-16556-5yud42.l7p0b\sketch_jun29b\sketch_jun29b.ino:30:3: note: suggested alternative: 'digit4'
   digit5 = remain;
   ^~~~~~
   digit4
C:\Users\bugge\AppData\Local\Temp\.arduinoIDE-unsaved2025529-16556-5yud42.l7p0b\sketch_jun29b\sketch_jun29b.ino:14:17: warning: unused variable 's' [-Wunused-variable]
 int hh, mm, ss, s;
                 ^
exit status 1

Compilation error: expected primary-expression before '/' token

The first error is clearly on the first problematic line (19) int remain = hh % / 10; // it's int so 1.

But the red line was on the third instance (line HERE) and if I uncomment the last lines, the red line is on "and HERE" i.e. later

The only way I can get the red line on the first instance is by commenting out everything after it.

Perhaps I need a new IDE. This is 1.8.3, but I have never had cause to complain before...

Ignore the red line, look at the line numbers in the output.

IDE 2.3.6 seems better from that perspective

But I would not consider that a reason to upgrade.

The compiler only complains when it reaches something that it can't understand. It just shows you that the previous statements could be resolved in some odd way, but not enough to say I can't go on because there are too many anomalies or down right contradictions.

That is why it gives up when it does. Not when you think it should give up.

If this is not good enough for you then I suggest you take a course on compiler writing, then you will see exactly why this happens.

1.8.19 is the same, so don’t upgrade on that account. I think the problem lies more in the way the user sees the compiler error messages - the window at the bottom of the IDE is small, and focused on the end of the compilation process, so that’s what the user addresses. I learned long ago to enable all compiler warnings, and enable verbose compiler output; then, when compilation fails, stretch the window and consider all the information presented. Usually, that’s far more effective than fixing a line of code.

One of the features in 2.3.6 is the auto filling of of sets of brackets and comments.

You type /* and before you know it, you've got /**/foo*/
It can be useful, or sometimes lead to a bit of a search.
Caught me more than once.

You can disable that if you want; see Arduino IDE 2.3.6 is now available - #16 by ptillisch.

1 Like

Thanks

Useful information.

Thanks

Your solution is not inherently wrong (apart from %/ instead of %), but improvement is possible.
Division in float is very slow (30µs in UNO R3), and you call it 6 times. Division in int works in your case too (15 µs in UNO R3). As the 'high' digit in each number is always low, it is possible to speed it up further:

// given hh, mm, ss, as int
int digit0 = 0;
while (hh > 9) {
  digit0++;
  hh -= 10;
}
int digit1 = hh;

int digit2 = 0;
while (mm > 9) {
  digit2++;
  mm -= 10;
}
int digit3 = mm;

int digit4 = 0;
while (ss > 9) {
  digit4++;
  ss -= 10;
}
int digit5 = ss;

Thank you!
This is just a countdown timer so, if it can do the job in one second, that's OK. However, I have been concerned about getting a quick start, and this could be important there.

At 'quick start' I think of:

if (readTime > presetTime) {wakeUp()}

You could convert readTime (from a clock) before each comparison, but you could also convert presetTime to the format of the reading from the clock (once).

I haven't got anything running yet, I'm looking at

1 set event time. Epoch midnight yesterday. No hurry

2 NTP request current epoch time. Hurry, once

3 subtract current from event. Hurry

4 countdown in hhmmss

There is no clock per se. The DS3231 is just there for its 1Hz pulse. Essential rig is 5v ProMini, ESP-01, 6x7 leds on 7221. I guess, if time was pressing, I could transfer the maths to the ESP.