Fundament al s of t he C Pr ogr ammi ng Language Symbol i c Const ant s Home Training Fundamentals of the C Programming Language Symbolic Constants What Ar e Const ant s ? Symbolic constants are what most people think of when someone refers to "constants" in any program language. Symbolic constants are nothing more than a label or name that is used to represent some fixed value that never changes throughout the course of a program. For example, one might define PI as a constant to represent the value 3.14159. Why Do We Need Const ant s ? Avoi d Magi c Number s Magic numbers are literals that get used in your code without any obvious reason. For example, if I have a line of code like: Why is the value of 0x30 used here? Why not 0x31 or 0xBEEF? What is the significance of that particular value? Only the programmer knows, and he has likely forgotten. A better approach is to build a description into the value by creating a named/symbolic constant and using it in place of the raw value: Where ASCII_OFFSET is defined somewhere in the program to have a value of 0x30. Even if you don't know what is meant by "ASCII_OFFSET", you at least have some clue as to what its purpose is so you can ask the right questions. Qui c k l y Change Val ues Dur i ng Devel opment When developing a complex program, there may be situations where you need to calibrate or otherwise tweak your constant values until the system works the way you expect. For example, say you have a delay routine used all over your program that you need to play with to get the timing right before releasing your code. If you load a counter register with a value like 255 everywhere you use the delay, it would be very cumbersome to change the value in multiple locations. If on the other hand you create a constant called DELAY_COUNT and use it everywhere you use the delay, changing the value is as easy as changing the constant's definition. Symbol i c Const ant s Labels (names) that represent fixed values that never change during the course of a program. 1 DisplayValue =UserChoice +0x30; ? 1 DisplayValue =UserChoice +ASCII OFFSET; ? CONTENTS Fundamentals of C Programming Get Started Here Sof t w ar e and Fi l es f or Lab Ex er c i ses C in an Embedded Environment Comments Variables The #include Directive Literal Constants Symbolic Constants Symbol i c Const ant s Text Substitution Labels Constant Variables Lab Ex er c i se 2: Symbol i c Const ant s The printf() Function Operators Expressions and Statements Decision Statements Loops Functions Multi-File Projects & Storage Class Specifiers Arrays and Strings Data Pointers Function Pointers Structures Bit Fields Unions Enumerations Preprocessor Macros with #define Resources Exit Tutorial Rel at ed Sel f -Pac ed Tr ai ni ng Coming Soon Tut or i al Home Fundamentals of the C Programming Language Dev el oper Hel p The right information, right now
Home Microchip Help Technical Training What's New? About Us Sign in Search this site Symbolic Constants - Developer Help http://microchip.wikidot.com/tls2101:symbolic-constants[6/21/2014 11:33:53 PM] Microchip Technology Inc.
Return to Top How t o Def i ne Const ant s There are two ways to create constants in C that we will look at over the next two pages: Text Substitution Labels Constant Variables (Say what? - Read on) Rel at ed Pages Literal Constants - Fundamentals of the C Programming Language Fundamentals of the C Programming Language - Training +FLAGS Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising fromthis information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip fromany and all damages, claims, suits, or expenses resulting fromsuch use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights. Main Site: Microchip.com Powered by Wikidot.com Training Text Substitution Labels - Developer Help http://microchip.wikidot.com/tls2101:text-substitution-labels[6/21/2014 11:34:10 PM]
Return to Top Fundament al s of t he C Pr ogr ammi ng Language Tex t Subst i t ut i on Label s Home Training Fundamentals of the C Programming Language Text Substitution Labels In most cases, constants are used for the convenience of the person writing the code or for those who will read the code later. In this capacity, constants are used to replace "magic numbers", to make code more readable, and to make it easier during the development process to modify a value that is used in many places throughout the program. This kind of constant need not reside in the PIC microcontroller's memory in the same way that a variable does. Instead it is something that can be handled at compile time as a text substitution. Because this is a preprocessor operation (all directives starting with a "#" are instructions to the C preprocessor), everything this line does is finished before the program is passed on to the compiler. The preprocessor will replace every instance of label with text. The preprocessor doesn't care about variable types, numbers, characters, or anything else related to C's syntax in this situation. It simply does a text substitution. So, if you had something like: every place the preprocessor finds "PI" in your source file, it will replace it with the text "3.14159". It doesn't matter what the context is. Once all these substitutions are made, the code then gets passed to the compiler where it decides how to interpret the text "3.14159". (In this case, the text would be interpreted as a double precision floating point value.) The primary benefit in using #define text substitutions for constants is that they have no impact on memory usage. So for exactly zero overhead, you can replace every "magic number" with its own unique, descriptive name/label all while improving the efficiency (and sanity) of any developer who must read your code, especially your future self. Rel at ed Pages Constant Variables - Fundamentals of the C Programming Language Symbolic Constants - Fundamentals of the C Programming Language A Simple C Program - Fundamentals of the C Programming Language Fundamentals of the C Programming Language - Training +FLAGS #def i ne label text 1 #define PI 3 14159 ? CONTENTS Fundamentals of C Programming Get Started Here Sof t w ar e and Fi l es f or Lab Ex er c i ses C in an Embedded Environment Comments Variables The #include Directive Literal Constants Symbolic Constants Symbolic Constants Tex t Subst i t ut i on Label s Constant Variables Lab Ex er c i se 2: Symbol i c Const ant s The printf() Function Operators Expressions and Statements Decision Statements Loops Functions Multi-File Projects & Storage Class Specifiers Arrays and Strings Data Pointers Function Pointers Structures Bit Fields Unions Enumerations Preprocessor Macros with #define Resources Exit Tutorial Rel at ed Sel f -Pac ed Tr ai ni ng Coming Soon Tut or i al Home
Fundamentals of the C Programming Language
Dev el oper Hel p The right information, right now
Home Microchip Help Technical Training What's New? About Us Sign in Search this site Text Substitution Labels - Developer Help http://microchip.wikidot.com/tls2101:text-substitution-labels[6/21/2014 11:34:10 PM] Microchip Technology Inc. Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights. Main Site: Microchip.com Powered by Wikidot.com Training Constant Variables - Developer Help http://microchip.wikidot.com/tls2101:constant-variables[6/21/2014 11:35:14 PM]
Return to Top Fundament al s of t he C Pr ogr ammi ng Language Const ant Var i abl es Home Training Fundamentals of the C Programming Language Constant Variables The term "constant variables" seems like a complete contradiction in terms, but it is an accurate description. Earlier in this course, we discussed the concept of a variable and how it defines memory storage requirements and how the value it contains should be handled by the compiler. All of that still applies here, but with one significant difference. When the variable is defined, its type is modified by the const keyword: This has the effect of creating a variable, allocating memory for it, and initializing it with the value value as discussed earlier, but the const keyword will prevent you from building any code that changes the value of the variable. So, if we have the following declaration: The linker would allocate 4 bytes of memory to store the IEEE-754 encoded value representing the number 3.14159. This can be a good thing in some cases, but in the embedded world, it is usually a poor use of resources when #define could do it without the overhead. In recent years, many books on C have shown this method of constant creation as the only method, completely ignoring the #define method which has been used since C's earliest days. While this is fine on a PC where memory is cheap, it is a very expensive proposition in the embedded world. Constant variables take up as much space as any other variable. If all you intend to do is remove magic numbers from your program to make it easier to read, this will result in a huge waste of memory (unless you have a clever linker and a good optimizer). However, if you are using these values in much the same way as you would a variable, such as coefficients for a digital filter, it might make senese to declare them as constant variables. So while there are some good reasons to use constant variables in specific circumstances, they should generally be avoided whenever a text substitution created with #define would suffice. Rel at ed Pages Symbolic Constants - Fundamentals of the C Programming Language Fundamentals of the C Programming Language - Training +FLAGS c onst type identifier = value; 1 c onst f l oat PI = 3 14159; ? CONTENTS Fundamentals of C Programming Get Started Here Sof t w ar e and Fi l es f or Lab Ex er c i ses C in an Embedded Environment Comments Variables The #include Directive Literal Constants Symbolic Constants Symbolic Constants Text Substitution Labels Const ant Var i abl es Lab Ex er c i se 2: Symbol i c Const ant s The printf() Function Operators Expressions and Statements Decision Statements Loops Functions Multi-File Projects & Storage Class Specifiers Arrays and Strings Data Pointers Function Pointers Structures Bit Fields Unions Enumerations Preprocessor Macros with #define Resources Exit Tutorial Rel at ed Sel f -Pac ed Tr ai ni ng Coming Soon Tut or i al Home Fundamentals of the C Programming Language Dev el oper Hel p The right information, right now
Home Microchip Help Technical Training What's New? About Us Sign in Search this site Constant Variables - Developer Help http://microchip.wikidot.com/tls2101:constant-variables[6/21/2014 11:35:14 PM] Microchip Technology Inc. Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights. Main Site: Microchip.com Powered by Wikidot.com Training