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 9117 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…
  • shabaz
    shabaz over 2 years ago in reply to Andrew J

    Hehe that's crazy. I'm guessing it wasn't work related, or the firm has serious questions to answer, how that passed a code review! 

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

    Agreed.  A lot appear because they are just rehashes of prior examples that don't really explain, or at least only superficially, a particular problem by people being lazy or trying to appear clever.  Thus the incomplete explanations are forever propagated.  Where I've had to learn something new, I've done so with a very specific problem in mind to solve rather than following along blindly with foo and bar nonsense.  It helps of course to have the 10,000 hours under my belt so I can cross-relate principles and points.  And yet, C pointers......Confused

    And no, not work related per-se, just something tied to the museum that I've agreed to take on.  I've had to get to grips with PIC microprocessors and development and it was another egregious example of someone trying to be way too clever with their C code and unnecessary embedded assembler.  Although in fairness, he's a really nice chap and genuinely very, very clever so I don't think he realises!!

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Cancel
  • Jan Cumps
    Jan Cumps over 2 years ago in reply to Andrew J

    I'm fond of foo bar. It's part of it culture. One of the rare moments that there's some lightness in geek land. It also has a purpose: makes it very clear that a generic / abstract example is given. And recognisable by lovers and haters Grin.

    Other frequent visitors are hello, world!. And scott/tiger will bring warm memories to early Oracle developers. And the blinky.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
  • Jan Cumps
    Jan Cumps over 2 years ago in reply to shabaz
    shabaz said:
    the firm has serious questions to answer, how that passed a code review! 

    side story: I do code reviews in my company as volunteer. Usually when we have third-parties writing customisations for us.

    I learn more than I find issues. Often, externals are certified developers. They are used to write code that will be reviewed. Most things that I send back are magic numbers, arbitrary maximums, and constructs that will cause sustain issues later.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
  • shabaz
    shabaz over 2 years ago in reply to Jan Cumps

    I find the same too! It helps all parties to it. It's important for startups too, where there may be just one or two developers, it helps all if they use the remainder startup founders (who might not have done as much coding in the past) as their code reviewers, i.e. dragging anyone for a review (doesn't have to be heavily knowledgeable) it helps in that scenario, simply to speak through the design choices. The same applies for hardware reviews. 

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • 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
  • phoenixcomm
    phoenixcomm over 2 years ago in reply to Prasanth_R

    I mess about with this thing. Its fast for running a test. but to get to work please use Eclipse with the GCC toolchain You'll, thank me. 

    I have several of them one for C, one for Java and I have all of the embedded tools on a third. BTW I despise the Arduino IDE, (all I use is setup(), and then I write my own main() which Loop calls!!

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

    I agree with you on Eclipse, GCC.

    When we switch to the Arduino IDE, Setup(), Loop() and your code, I believe that the Arduino approach is better than what you are doing.

    • 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
<>
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