- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference between #define and const in Arduino
If you've done sufficient Arduino programming, you'd have seen that there are two ways of defining constants.
One way is to use #define, like
#define const_name 3
The other way is to use the const keyword, like
const int var_name = 3;
Difference between #define and const
#define is like a placeholder. The Arduino compiler replaces all mentions of this constant with its value at the compile time. This means that the values defined using #define don't take up any program space.
Variables defined using const, on the other hand, are just normal variables, whose values can't be changed. They take up program memory space, and have a type (which is advantageous in many situations).
In general, it is preferred to use const over #define, for defining constants.
- Difference between const int*, const int * const, and int const * in C
- Difference between const int*, const int * const, and int const * in C/C++?
- What is the difference between const int*, const int * const, and int const *?
- Difference between const char* p, char * const p, and const char * const p in C
- Difference between #define and const in C
- Difference between readonly and const keyword in C#
- Difference between float and double in Arduino
- Explain the difference between const and readonly keywords in C#
- Difference between signed and unsigned integer in Arduino
- What is the difference between keywords const and readonly in C#?
- What is the difference between #define and const Keyword in C++?
- Difference between hardware serial and software serial in Arduino
- What is difference between int and const int& in C/C++?