element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum When to use int, const int, const byte and Define
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • Replies 41 replies
  • Subscribers 384 subscribers
  • Views 9112 views
  • Users 0 members are here
  • programming
  • arduino_code
  • development
Related

When to use int, const int, const byte and Define

colporteur
colporteur over 2 years ago

I'm not a programmer. I am more of a ressurectionist. I find Arduino code pieces and join them together to make a program like Dr. Frankenstein joined body parts to create his monster. 

I see the statements listed in the question and wondered when do they apply. In collecting code parts from a number of programs, I can develop a program that has all four before the voids.  If I was to create a program from scratch I'm not confident I know what one to select for the variable type I am using.

Are there some best practices a novice could use in applying these to get the most benefit from their programming?

  • Sign in to reply
  • Cancel

Top Replies

  • baldengineer
    baldengineer over 2 years ago +6
    Personally, I used to only use #define for text-replacement. So, things like #define NOT_PRESSED 0x1, and #define PRESSED 0x0 for buttons. It makes if-statements more readable. For example, (if digitalRead…
  • Andrew J
    Andrew J over 2 years ago +4
    colporteur said: Are there some best practices a novice could use in applying these to get the most benefit from their programming? Please take this the right way: the best thing you could do is sit…
  • shabaz
    shabaz over 2 years ago in reply to beacon_dave +3
    Foo and bar are the worst! It never made sense. Reminded me of the stereotype of the nerd who deliberately uses the most complex sed/awk syntax he/she can think of. Especially since it doesn't take a genius…
Parents
  • obones
    obones over 2 years ago

    When compiling, you have multiple steps, three of them being preprocessor, compiler and linker in that order

    The preprocessor takes your source files, modifies them "in memory" and passes the result to the compiler

    The compiler takes its input and generates machine code in the form of object files (.o)

    The linker takes all object files and creates the final application file (.exe under Windows, .bin for firmware...)

    #define NAME value tells the preprocessor to replace NAME by value wherever it sees it. It is just like copy/paste but done for you by the preprocessor, which means the compiler will never see the NAME item, only a repetition of value in lots of places

    const int and int only differ in semantics, the "const" specifier telling the compiler that it should not allow any code to change the value. As this is seen by the compiler, you can "take" the address of the constant by using the ampersand (&) operator, which might be required by some libraries and is even more convenient when dealing with structures because all you deal with is a pointer (32 bits on 32 bits architecture) and not the entire structure itself (that can range in the dozen of bytes)

    Finally, the linker can do some serious optimizations with constants when they have the same value by "merging" them together, meaning that 10 constants with the same value only occupy their size once in the final firmware. While this does not mean much under desktop OSes, it can be very important on embedded architectures.
    One other benefit of "const" is that on architectures like the ESP32, the value is not placed in RAM but in Program memory. As RAM is very limited (120k contiguous at most), it is of the utmost importance to place as little things as possible in there.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
Reply
  • obones
    obones over 2 years ago

    When compiling, you have multiple steps, three of them being preprocessor, compiler and linker in that order

    The preprocessor takes your source files, modifies them "in memory" and passes the result to the compiler

    The compiler takes its input and generates machine code in the form of object files (.o)

    The linker takes all object files and creates the final application file (.exe under Windows, .bin for firmware...)

    #define NAME value tells the preprocessor to replace NAME by value wherever it sees it. It is just like copy/paste but done for you by the preprocessor, which means the compiler will never see the NAME item, only a repetition of value in lots of places

    const int and int only differ in semantics, the "const" specifier telling the compiler that it should not allow any code to change the value. As this is seen by the compiler, you can "take" the address of the constant by using the ampersand (&) operator, which might be required by some libraries and is even more convenient when dealing with structures because all you deal with is a pointer (32 bits on 32 bits architecture) and not the entire structure itself (that can range in the dozen of bytes)

    Finally, the linker can do some serious optimizations with constants when they have the same value by "merging" them together, meaning that 10 constants with the same value only occupy their size once in the final firmware. While this does not mean much under desktop OSes, it can be very important on embedded architectures.
    One other benefit of "const" is that on architectures like the ESP32, the value is not placed in RAM but in Program memory. As RAM is very limited (120k contiguous at most), it is of the utmost importance to place as little things as possible in there.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
Children
  • phoenixcomm
    phoenixcomm over 2 years ago in reply to obones

     obones 

    obones said:
    #define NAME value tells the preprocessor to replace NAME by value wherever it sees it. It is just like copy/paste but done for you by the preprocessor, which means the compiler will never see the NAME item, only a repetition of value in lots of places

     

    Sorry you don't get the star for this, you the  Penalty Box. "the #define" is a not copy and paste! It is a MACRO de-expansion tool. please see this example which I posted just before you did. " square #define sqr(a)  ((a)*(a)",  this is not copy and paste! just for giggles you can also do pi which is NOT in the Math lib you can do this:  #define pi 3.1416  kind of this and then nothing is correct you could use this: π=atan2(0,−1) now turn this into a 

    #define  pi  atan2(0, -1)  

    Cris.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • obones
    obones over 2 years ago in reply to phoenixcomm

    Yeah, you can do all kind of crazy stuff with the preprocessor, it's turing complete after all.

    But for a "beginner's" explanation, seeing it a as copy/paste tool that replaces a given text by another one before sending it to the compiler is a much simpler way to grasp what it does.

    And that also helps a lot understanding why the compiler will emit error messages that never mention the preprocessor symbol, which can be off putting when starting to work with this kind of concepts.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • phoenixcomm
    phoenixcomm over 2 years ago in reply to obones

    I don't agree, I taught K&R C on the side to students that had one semester. of Pascal. The two languages are very similar in construction ie semantically, even though one is a compiler, and the other is an interpreter. After getting to un-learn Pascals over the use of English to describe variables ( like fred_ponts_to_tom. the #derine though yes I start out with #define FAIL -1

    but in the same had I also showed them things like a square which is #define SQR(a)  ((a)*(a)) and also pi see above and for pete sake hallway make your defines in UPPERCASE

    If you try and learn a programing language you need to learn ALL of it. not some of it. 

    btw what to make you Android programing easier?? don't use void loop()  as guess what void loop is a defined macro and invokes main()

    which means now you can have return codes in main()   like int main( ){  return FAIL;}

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • Jan Cumps
    Jan Cumps over 2 years ago in reply to phoenixcomm
    phoenixcomm said:
    btw what to make you Android programing easier?? don't use void loop()  as guess what void loop is a defined macro and invokes main()

    Are you sure about that one?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube