CON vs. =


Closed Thread
Results 1 to 7 of 7

Thread: CON vs. =

  1. #1
    Join Date
    Sep 2010
    Location
    Las Vegas, NV
    Posts
    305

    Default CON vs. =

    Is there any difference or advantage to declaring a constant using CON vs. using an = sign to assign a variable value? i.e

    widget CON 155 vs. widget = 155

    Using just the = sign would require declaring the value beforehand and maybe use another few bits of memory. Beyond that is there any other disadvantage?

  2. #2
    Join Date
    Dec 2012
    Location
    Tennessee
    Posts
    262


    Did you find this post helpful? Yes | No

    Default Re: CON vs. =

    if you dont use it that way it will be like this

    widget VAR Byte
    widget = 155

    and there is a possibility if your program is large that you might change widget = ?? something else later in your program
    also say you want to tell widget that it is 25000 halfway into your program, but you only delared it as a byte, so it will
    generate an error. but if you use CON then hey it auto sets the Variable size

  3. #3
    Join Date
    May 2007
    Posts
    604


    Did you find this post helpful? Yes | No

    Default Re: CON vs. =

    Look into the differences between constants and variables.
    Why pay for overpriced toys when you can have
    professional grade tools for FREE!!!

  4. #4
    Join Date
    Sep 2010
    Location
    Las Vegas, NV
    Posts
    305


    Did you find this post helpful? Yes | No

    Default Re: CON vs. =

    Well, when I didn't define the question correctly I got what I asked for. What I should have asked was, do both constants and variables use up memory or resources when programs compile or execute?

    According to what I've read in the PBP3 manual, the answer for constants is they don't use memory of the micro-controller. They make substitutions during compile which I would assume uses less program bits.

    Variables do use memory and depending how they are defined they can use a goodly portion. I assume that if a variable is defined as a word and you're only using a few bits you'd be wasting space. Better off defining it as a byte or using a small routine to store those bits as part of a larger word/byte. So if you're trying to save memory for other things, and your variable doesn't need to change, then defining it as a constant would free up RAM to be used elsewhere.

    I've yet to write a program where I'm concerned about memory but I figure I'd start with good habits. Especially ones that let me program more efficiently.

    Thanks for making me read the manual. It really is a wealth of info.

  5. #5
    Join Date
    May 2006
    Location
    Del Rio, TX, USA
    Posts
    343


    Did you find this post helpful? Yes | No

    Default Re: CON vs. =

    So, if I understand the question, “Is there a difference between the following code snippets?”

    Code:
    MyByte VAR BYTE
    MyByte = 155
    Code:
    MyByte VAR BYTE
    MyCon CON 155
    MyByte = MyCon
    The answer is: No.

    The only difference is how you read it as a human. For the PIC and it’s code, they are exactly the same.

  6. #6
    Join Date
    Sep 2010
    Location
    Las Vegas, NV
    Posts
    305


    Did you find this post helpful? Yes | No

    Default Re: CON vs. =

    I agree but both of the examples use some of the PICs RAM where declaring MYCON CON 155 only would not.

    At least that is my understanding.

  7. #7
    Join Date
    May 2006
    Location
    Del Rio, TX, USA
    Posts
    343


    Did you find this post helpful? Yes | No

    Default Re: CON vs. =

    Quote Originally Posted by AvionicsMaster1 View Post
    I agree but both of the examples use some of the PICs RAM where declaring MYCON CON 155 only would not.

    At least that is my understanding.
    You are correct, constants use no RAM, but variables use RAM space depenedent on their type.

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts