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 9089 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
  • baldengineer
    baldengineer over 2 years ago

    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(pin) == NOT_PRESSED.) Good solution for people who mistakenly think that pull-ups make the inputs inverted.

    In the past, I would always define things like pin functions as a const. But lately, I've been using #defines more. For any value that will fit into an 8-bit number, there is no performance difference between a #define macro or a const (of an 8-bit variable type.) For a const that fits into a register, most compilers won't bother treating it as a variable anyway.

    As usual, it is more important to be consistent in your code than get worked up over which is better.

    Regarding int versus byte, versus, whatever, except for byte and char, I have stopped using types like int. Instead, I use fixed-width integer types like uint8_t or uint32_t or int8_t. These are all part of stdint.h.

    Using fixed width removes the ambiguity of "How big is this variable on this processor with this compiler on this week of the year?"

    • Cancel
    • Vote Up +6 Vote Down
    • Sign in to reply
    • Cancel
  • colporteur
    colporteur over 2 years ago in reply to baldengineer

    I did a review of your material at the link for De-Bounce. I had read that #define is a text-based symbol substitution. I receive a tech tip in an email that suggested using it to #define MY_LED 13. This is what led to my confusion.

    int ledpin = 13;

    const byte ledpin = 13;

    and now

    #define led_pin 13

    What is a ressurectionist suppose to believe.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • Andrew J
    Andrew J over 2 years ago in reply to colporteur

    int ledpin = 13; // The compiler will reserve 16-bits (assuming 16-bit processor) at an address in memory to store the number 13 0b0000000000001101.  that address will be used to retrieve the literal when used, every time, because the compiler has no knowledge if that value will change.

    const byte ledpin = 13; // the compiler (if it is half decent) will almost certainly substitute the use of variable ledpin with an 8-bit literal value 0b00001101.

    #define led_pin 13; // the pre-processor will text substitute, in source non-permanently, the word "led_pin" with the text "13" and then send the source to the compiler.  The compiler will then treat it as a literal integer (so like the const definition) but whether that would be as a 16-bit literal or an 8-bit literal would need to be researched because I don't actually know (it's an interesting thought actually and my first suspicion is that it would choose the most optimum bit size, 8 in this case!)

    Any of these work but a ressurectionist will always struggle because they look radically different.  However, this stuff is as basic to programming as understanding Ohms law in electronics.  I'm an (ex-)developer, albeit one who hasn't had a professional development role for over 20 years, who had zero knowledge of C when I picked up an Arduino, so I actually spent some time understanding how the language dealt with types, working out the differences between const and #define, what happens if types are mixed in an expression (e.g int aVar = 12 / 7.1; ) and so on.  These things are so fundamental that even a resurrectionist should take the time to understand it if they intend to spend any time with microprocessors, including Arduino, because copy-and-paste will very often introduce strange errors into existing code (as, I think, you have found out on many occasions.)  I say this in the nicest possible way and not to criticise at all - I honestly believe it will increase your enjoyment of working with Arduinos if you understand a little more than you currently do.  It really will be worth the effort and we can help you clarify points of understanding.  

    I still have a problem with C in that it would appear that many 'experienced' C programmers seem to go out of their way to write C code in as an obscure a manner as possible!  I also struggle with pointers despite reading umpteen articles about them and I have to revisit constantly.  

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

    int ledpin = 13; // The compiler will reserve 16-bits (assuming 16-bit processor) at an address in memory to store the number 13 0b0000000000001101.  that address will be used to retrieve the literal when used, every time, because the compiler has no knowledge if that value will change.

    const byte ledpin = 13; // the compiler (if it is half decent) will almost certainly substitute the use of variable ledpin with an 8-bit literal value 0b00001101.

    #define led_pin 13; // the pre-processor will text substitute, in source non-permanently, the word "led_pin" with the text "13" and then send the source to the compiler.  The compiler will then treat it as a literal integer (so like the const definition) but whether that would be as a 16-bit literal or an 8-bit literal would need to be researched because I don't actually know (it's an interesting thought actually and my first suspicion is that it would choose the most optimum bit size, 8 in this case!)

    Any of these work but a ressurectionist will always struggle because they look radically different.  However, this stuff is as basic to programming as understanding Ohms law in electronics.  I'm an (ex-)developer, albeit one who hasn't had a professional development role for over 20 years, who had zero knowledge of C when I picked up an Arduino, so I actually spent some time understanding how the language dealt with types, working out the differences between const and #define, what happens if types are mixed in an expression (e.g int aVar = 12 / 7.1; ) and so on.  These things are so fundamental that even a resurrectionist should take the time to understand it if they intend to spend any time with microprocessors, including Arduino, because copy-and-paste will very often introduce strange errors into existing code (as, I think, you have found out on many occasions.)  I say this in the nicest possible way and not to criticise at all - I honestly believe it will increase your enjoyment of working with Arduinos if you understand a little more than you currently do.  It really will be worth the effort and we can help you clarify points of understanding.  

    I still have a problem with C in that it would appear that many 'experienced' C programmers seem to go out of their way to write C code in as an obscure a manner as possible!  I also struggle with pointers despite reading umpteen articles about them and I have to revisit constantly.  

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
Children
No Data
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