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
  • Jan Cumps
    Jan Cumps over 2 years ago
    • If I write proper C++ classes - and go through the effort to make good architectured designs - I use const and enumerators. In those cases, I try to use the new features that the language offers.

    class m25 {
    public:
      m25(spi_inst_t * spi, uint sclk, uint mosi, uint miso, uint ncs, uint nhold, uint nwrite) : 
        spi(spi), sclk(sclk), mosi(mosi), miso(miso), ncs(ncs), nhold(nhold), nwrite(nwrite) {}
      ~m25() {}
      void init();
      std::vector<uint8_t> rdid();
      std::vector<uint8_t> rdsr();
      void write(uint32_t address, std::vector<uint8_t> data);
      std::vector<uint8_t> read(uint32_t address, size_t length);
      void be();
    
      bool set_nhold(bool set);
      bool set_nwrite(bool set);
      void config_protect(bool srwd, bool bp2, bool bp1, bool bp0);
    
    private:
      enum commands : uint8_t { 
        WREN  = 0x06,
        RDID = 0x9F,
        RDSR = 0x05,
        WRSR = 0x01,
        READ = 0x03,
        PP   = 0x02,
        BE   = 0xC7 };
      spi_inst_t * spi;

    In the example, in some locations I used uint instead of uintxx_t, like baldengineer suggests. I am also a fan of the uintxx_t types. But this is a wrapper around the RP2400 SDK, and they use uint. 
    I try to adapt where I think it makes sense.

    • When writing C, and prototyping, I use defines. 
    • When I modify existing code, I usually stick to what the previous pundit used.
    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Cancel
  • Andrew J
    Andrew J over 2 years ago in reply to Jan Cumps

    Jan, what do functions rdid() and rdsr() do?  Ditto set_nhold() and set_nwrite()?  I don't mean, the code, but what you would say in a function comment for them?  rdid() - is that reading an id for example?  What are the purposes of variables srwd, bp2?  

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

    > what do functions rdid() and rdsr() do?

    That's the name of a function you can do with flash.

    image

    > Ditto set_nhold() and set_nwrite()?

    set the IC's NHOLD and NWRITE pins

    image

    > What are the purposes of variables srwd, bp2?  

    image

    I did that deliberately - match the manufacturer's names.

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

    Yes, not picking on you Slight smile.  

    Why do C programmers always feel the need to abbreviate the heck out of variable and function names so that they aren't readily apparent?  Why not readId() {} or bool blockProtectBit2 etc... It feels like it's some undocumented standard somewhere or some C programmer brotherhood thing!  It's not like source code contributes to the final executable size.  Or is it that C programmers are unduly lazy and their poor fingers can't cope with having to type an extra few characters Grin  Good job they didn't get into SmallTalk!

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

    Not a C thing in this case. A choice:

    I claim that I did not abbreviate anything Slight smile. I used the language of the problem domain.

    Andrew J said:
    Why not readId() {} or bool blockProtectBit2 etc... It feels like it's some undocumented standard somewhere or some C programmer brotherhood thing!

    RDID and BP2 are documented. If I use readID and blockProtectBit2, I'd have to document them. I think that it is an advantage of using domain specific lingo. OO advises to use the names of the problem domain.

    If I were to write an abstract Memory class, I'd probably use a different type of name. But this one is close to a particular problem domain and I used their names.

    I'd do the same with MISO, MOSI, ... . If I write a class that's a SPI wrapper, I'd call MISO, MOSI, nCS etc.
    If I wrote an abstraction layer around serial chip protocols, I'd probably use read(), write(), select()

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

    I wasn't referring to you specifically, I was being more philosophical, honest.  I bet the original "designers" of this thing were C programmers :)  They should have been calling them by their proper names for clarity.  

    OO does say use domain language but what you have is not a problem domain, it's a specific instance within the problem domain.  My definite knowledge is to also call a Spade a "DiggingImplement spade = new DiggingImpement(BladeType typeOfBlade);" and not a " DigImpl spd = new DigImpl(Blade_t tp);"  But then I come from a SmallTalk background which would have been something like spade = new DiggingImplementWithBladeType: bladeType (SmallTalk is untyped because everything, including literal integers for example, are full blown Object or Object subclass instances).  

    Anyway, as I say, I was being very generalist: you have to admit, there is a massive tendency in C programming to abbreviate function and variable names and I can't fathom the reason for it.  Except, yes, for MOSI, MISO I would use the same because the designers of that chose to abbreviate and they are absolutely well understood terms and used by everyone (unlike BP2, SRWD, WRDI etc.)

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

    exelearning.org/.../

    8. Also Use Problem Domain Names
    When there is no `programmer-ese' for what you're doing, use the name from the problem domain. At least the programmer who maintains your code can ask his boss what it means.

    In analysis, of course, this is the superior rule to (Use #7 Solution Domain Names), because the end-user is the target audience.

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

    exelearning.org/.../

    8. Also Use Problem Domain Names
    When there is no `programmer-ese' for what you're doing, use the name from the problem domain. At least the programmer who maintains your code can ask his boss what it means.

    In analysis, of course, this is the superior rule to (Use #7 Solution Domain Names), because the end-user is the target audience.

    • 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