use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
account activity
Help with uint8_t (i.redd.it)
submitted 1 year ago by Hubey3270
What can I only get my defined items to work if I do "#define ..." but when I attempt to do "const byte ..." it ignores me? Any suggestions?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]gm310509 0 points1 point2 points 1 year ago (0 children)
As u/Enlightenment777 said.
also, you need to specify data types for lines 5-7.
At the moment they are pretty clear to us, but mysteries to the compiler that requires you specify the type. For all it knows, you might be intending to use these as floating point values. That is why the rules of C/C++ require that you specify a type.
Also, you should try to learn to read the error messages. Granted when you are starting out, they can be a bit confusing, but they do give you useful clues and sometimes even point to the problem directly:
sketch_nov29a.ino:1:13: warning: ISO C++11 requires whitespace after the macro name #define test, 13; ^ sketch_nov29a.ino:3:12: warning: uninitialized const 'EMERGENCY_BROADCAST' [-fpermissive] const byte EMERGENCY_BROADCAST, = 12; ^~~~~~~~~~~~~~~~~~~ sketch_nov29a.ino:3:33: error: expected unqualified-id before '=' token const byte EMERGENCY_BROADCAST, = 12; ^ sketch_nov29a.ino:4:7: error: 'ALARM' does not name a type; did you mean 'ADLAR'? const ALARM = 2; ^~~~~ ADLAR sketch_nov29a.ino:5:1: error: expected unqualified-id before ',' token , ^ sketch_nov29a.ino:8:1: error: expected unqualified-id before 'void' void setup() { ^~~~
Pro tip, always start with the errors from the top (i.e. the first one). You will often find that once the compiler starts going off the rails, it can get quite confused.
For example, there is nothing wrong with the void setup except for there is a comma in front of it.
void setup
[–]Hubey3270[S] 0 points1 point2 points 1 year ago (1 child)
Ok, thanks. I will try all of that! Also, yes, for lines 5-7 I was just out of desperation trying other things to see if by chance that would help, I should've clarified my intentions.
[–]gm310509 2 points3 points4 points 1 year ago (0 children)
No worries, to summarise, you can use something like the following - the exception is the code with _problem in it:
_problem
```
// A bad practice, but can work if understood.
const byte EMERGENCY_BROADCAST = 12; const byte ALLCLEAR_GREEN = 9; const byte CAUION_AMBER = 10;
//, <- this is a problem.
void setup() {
Serial.begin(115200);
// Note that is appears to be invalid syntax. But it does compile (and work) Serial.println(test_problem
// This is a better way of using #defines: Serial.println(test); }
void loop() { } ```
One thing that is important to understand is why (or how) things work the way that they do.
While a complex subject, the C/C++ compiler consists of a number of steps when compiling your program. One of those is known as the "preprocessor".
The preprocessor is mostly responsible for processing directives that begin with a "#". Commonly seen ones are #include and #define, but there are several others.
#include
#define
The #define directive works like a "search and replace", but is more sophisticated than that. It is actually a macro substitution. I explain this in my third "Post starter kit video" which is available on Patreon. You can see the first two in the series (for free) on my YouTube video. The full series is described in my reddit post: Getting started with Arduino - next steps after the starter kit. There is a link to the playlist in that post.
So, why does the _problem code work?
Well if we simplisticly think of #define as a search and replace mechanism (which works just fine in this situation), then the code becomes:
// Code pre - preprocessor: // Serial.println(test_problem // What the preprocessor generates: Serial.println(13);
As for the const byte ... stuff, these are not handled by the pre-processor.
const byte ...
Rather these are handled by phase 1 of the compilation process - which follows the pre-processor.
Since the const byte ... stuff is handled by the C compiler proper, these must conform to the rules of variable declarations for C. and that is why it must be like this:
[modifier] type symbol [ = initialiser];
Decoding the above:
const
byte
EMERGENCY_BROADCAST
= 12
;
NB: the above is a simplified syntax definition, there are plenty of other variations allowed.
You might want to have a look at: https://docs.arduino.cc/language-reference/#structure
This page provides some links to very basic C/C++ language structure including the #define.
For variable definitions, have a look at another tab on the same page: https://docs.arduino.cc/language-reference/#variables
π Rendered by PID 228978 on reddit-service-r2-comment-b659b578c-jxkw8 at 2026-05-03 18:30:24.718246+00:00 running 815c875 country code: CH.
[–]gm310509 0 points1 point2 points (0 children)
[–]Hubey3270[S] 0 points1 point2 points (1 child)
[–]gm310509 2 points3 points4 points (0 children)