Fundament al s of t he C Pr ogr ammi ng Language Var i abl es Home Training Fundamentals of the C Programming Language Variables Looking at the simple C program we saw two pages back, we can observe the three topics of this section in action. Line 7 shows an example of a var i abl e declaration (actually two variable declarations). A variable is simply a container for holding data, where the container occupies one or more bytes in the microcontroller's memory (typically RAM). A variable must be dec l ar ed before it can be used, as shown above. The declaration of a variable consists of two parts: 1. A dat a t ype (or just simply a type) that serves two purposes: Tells the compiler how to handle or manipulate the data stored in the variable. In the example above, the word f l oat is one of several data types we'll discuss later in this section. Tells the linker how much memory to reserve to store the contents of the variable. 2. An i dent i f i er (or name) that will be used to uniquely identify the variable whenever we want to access or modify its contents. The words r adi us and ar ea in the example above are identifiers. To the right is a visual representation of how three variables might be stored in the 16-bit wide data memory of a PIC24 family device (there wouldn't actually be gaps between them). The first variable warp_factor is of type int which is defined as a 16-bit value in the MPLAB XC16 Compiler, so it takes one Var i abl e A variable is the combination of a data type and an identifier (name) that represents one or more memory locations used to hold a program's data. 1 2 3 4 5 6 7 8 9 10 11 12 13 #include <stdio.h>
#define PI 3.14159
i nt main(voi d) { f l oat radius, area;
//Calculate area of circle radius =12.0; area =PI * radius * radius; pr i nt f ("Area =%f", area); } ? 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 Var i abl es Identifiers ANSI Keywords Data Types Data Type Qualifiers Variable Declarations and Definitions Lab Ex er c i se 1: Var i abl es and Dat a Types The #include Directive Literal Constants Symbolic Constants 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 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 Variables - Developer Help http://microchip.wikidot.com/tls2101:variables[6/21/2014 11:25:28 PM] Microchip Technology Inc.
Return to Top complete word (two bytes) of data memory. The second variable letter is of type char which is almost always an 8-bit value - only compilers with Unicode support might define it otherwise. So, it takes up one byte, or a half of a word of data memory. If other char type variables were declared, a second one would be packed into the same word as the first, so both bytes would be occupied. The third variable length is of type float which is usually a 32-bit value with most compilers. It takes up two full words, or 4-bytes of data memory. It's data is also encoded (not shown) in a modified form of the IEEE-754 floating point format, so it needs to be handled differently from int and char type variables. Rel at ed Pages Constant Variables - Fundamentals of the C Programming Language Literal Constants - Fundamentals of the C Programming Language Identifiers - Fundamentals of the C Programming Language Variables - Fundamentals of the C Programming Language A Simple C Program - 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 Rel at ed Sel f -Pac ed Tr ai ni ng Coming Soon Training Identifiers - Developer Help http://microchip.wikidot.com/tls2101:identifiers[6/21/2014 11:26:59 PM]
Return to Top Fundament al s of t he C Pr ogr ammi ng Language I dent i f i er s Home Training Fundamentals of the C Programming Language Identifiers Identifiers in C must be strings of characters from the valid C character set, which includes all the letters of the English alphabet (both upper and lower case), the numbers 0-9, and the underscore. The first character of an identifier must NOT be a number and under no circumstances may an identifier contain a space. All identifiers are case sensitive, so you can have two identical identifiers except for the case of the first character and they will be considered two completely different identifiers. Also, according to the ANSI C standard, only the first 31 characters of an identifier are significant. So, if you had two 32 character identifiers differing only in the last character, some compilers would not recognize them as being different. With that said, most modern compilers far exceed the limit of 31 characters, allowing very long identifiers with many more significant characters. Rel at ed Pages I dent i f i er An identifier is a name given to a program element such as a variable, function, or array. This name may be used to refer to the program element without knowing its specific location in memory. 1 2 i nt myVar; // This variable... i nt MyVar; // is NOT the same as this variable ? 1 2 i nt abcdefghijklmnopqrstuvwxyz01234X; // This variable... i nt abcdefghijklmnopqrstuvwxyz01234Y; // ...might be indistinguishable from this one ? 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 Variables I dent i f i er s ANSI Keywords Data Types Data Type Qualifiers Variable Declarations and Definitions Lab Ex er c i se 1: Var i abl es and Dat a Types The #include Directive Literal Constants Symbolic Constants 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 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 Identifiers - Developer Help http://microchip.wikidot.com/tls2101:identifiers[6/21/2014 11:26:59 PM] Microchip Technology Inc. Variable Declarations and Definitions - Fundamentals of the C Programming Language Variables - 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 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 Rel at ed Sel f -Pac ed Tr ai ni ng Coming Soon Training Keywords - Developer Help http://microchip.wikidot.com/tls2101:keywords[6/21/2014 11:27:27 PM]
Return to Top auto break case char const continue default do double else enum extern float for goto if int long register return short signed sizeof static struct switch typedef union unsigned void volatile while Fundament al s of t he C Pr ogr ammi ng Language Keyw or ds Home Training Get Started with MPLAB X IDE and Microchip Tools Keywords While we have extensive leeway with respect to the names we can use for identifiers, there are some words that cannot under any circumstances be used as identifiers. These are known as k eyw or ds. ANSI C Keyw or ds These 32 keywords have specific meanings in ANSI C and as such, may not be used for anything other than their intended purpose. In any ANSI C implementation, these keywords will have the exact same meanings which we will explore throughout the rest of this class. However, some compilers deviate from the standard ANSI implementation resulting in potential differences in the behavior of some keywords. Some compiler implementations may define additional keywords. While these extra keywords are not part of the ANSI C standard, they will have the same restrictions as the ANSI keywords in their compiler tool chain. The Microchip MPLAB XC16 compiler, for example, defines 12 additional keywords. These and any other compiler specific keywords will be covered in the compiler classes. Rel at ed Pages Fundamentals of the C Programming Language - Training +FLAGS Keyw or d Keywords are words that have special meaning to the compiler and may not be used as identifiers. 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 Variables Identifiers ANSI Keyw or ds Data Types Data Type Qualifiers Variable Declarations and Definitions Lab Ex er c i se 1: Var i abl es and Dat a Types The #include Directive Literal Constants Symbolic Constants 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 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 Keywords - Developer Help http://microchip.wikidot.com/tls2101:keywords[6/21/2014 11:27:27 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 Rel at ed Sel f -Pac ed Tr ai ni ng Coming Soon Training Data Types - Developer Help http://microchip.wikidot.com/tls2101:types[6/21/2014 11:28:04 PM]
Return to Top Fundament al s of t he C Pr ogr ammi ng Language Dat a Types Home Training Fundamentals of the C Programming Language Data Types When you create variables, the compiler needs to know how much memory should be allocated for storage and how the data should be handled in arithmetic and logical operations. There are four fundamental data types. Two of them handle integer type data while the other two handle floating point data. Type Desc r i pt i on Si ze (bi t s) c har Single Character 8 i nt Integer 16 f l oat Single Precision Floating Point Number 32 doubl e Double Precision Floating Point Number 64 The size of these types is not standardized, though the sizes presented here are very common. The i nt data type is the most variable from one compiler to another since it is typically sized to be the same width as the ALU (Arithmetic Logic Unit) / data memory word. So on a 16-bit microcontroller, an i nt would be 16-bits, while on a 32-bit microcontroller an i nt would be 32-bits. This is almost always true, but frequently breaks down in the 8-bit world. Many 8-bit compilers define i nt as 16-bits, while some define it as 8-bits. So, before you start writing code, make sure you read your compiler's user manual to find out how big an i nt is. Even c har , which in the past was almost always implemented with 8-bits to accommodate 7-bit ASCII encoding of characters, can now be 16- bits on some compilers that support Unicode encoding. There are two more data types: voi d and enum which will be discussed later in the class due to their special applications. Rel at ed Pages Literal Qualifiers - Fundamentals of the C Programming Language Literal Constants - Fundamentals of the C Programming Language Variable Declarations and Definitions - Fundamentals of the C Programming Language Variables - Fundamentals of the C Programming Language A Simple C Program - Fundamentals of the C Programming Language Fundamentals of the C Programming Language - Training +FLAGS Dat a Type A data type defines the storage requirements, handling requirements, and behavior of variables and function parameters. 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 Variables Identifiers ANSI Keywords Dat a Types Data Type Qualifiers Variable Declarations and Definitions Lab Ex er c i se 1: Var i abl es and Dat a Types The #include Directive Literal Constants Symbolic Constants 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 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 Data Types - Developer Help http://microchip.wikidot.com/tls2101:types[6/21/2014 11:28:04 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 Rel at ed Sel f -Pac ed Tr ai ni ng Coming Soon Training Data Type Qualifiers - Developer Help http://microchip.wikidot.com/tls2101:type-qualifiers[6/21/2014 11:28:52 PM] Fundament al s of t he C Pr ogr ammi ng Language Dat a Type Qual i f i er s Home Training Fundamentals of the C Programming Language Data Type Qualifiers The fundamental data types may be modified by the prefixes si gned, unsi gned, shor t , and l ong. These modifications change the range of values that can be represented by the fundamental data type. Modi f i ed I nt eger Types Qual i f i ed Type Mi n Max Si ze (Bi t s) unsi gned c har 0 255 8 c har , si gned c har -128 127 8 unsi gned shor t int 0 65535 16 shor t int, si gned shor t int -32768 32767 16 unsi gned int 0 65535 16 i nt , si gned int -32768 32767 16 unsi gned l ong int 0 2 -1 32 l ong int, si gned l ong int -2 2 -1 32 unsi gned l ong l ong int 0 2 -1 64 l ong l ong int, si gned l ong l ong int -2 2 -1 64 The keyword i nt is optional when it is modified (shown in italics in table above). For example, an unsi gned i nt could be specified more simpy as unsi gned. The only time i nt must appear is when used by itself. Also note that the fundamental data types are all si gned by default. So i nt and si gned i nt mean exactly the same thing. si gned is not required, but often used when one wants to make their code explicitly clear. Some compilers, such as the legacy MPLAB C18 compiler, support the apparently contradictory shor t l ong which is typically defined to be 24- bits. This data type is usually found on compilers that support digital signal processors where 24-bit data is used in high-end audio applications. As with the fundamental data types, the sizes shown in the table above may vary from one compiler to the next. For example, the shor t modifier has no effect on i nt with the MPLAB XC16 compiler, while the MPLAB XC32 compiler defines a shor t as 16-bits versus the full i nt as 32-bits. Modi f i ed Fl oat i ng Poi nt Types On the floating point side of data types the only valid qualifier is l ong and it is typically used only with doubl e. 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 Variables Identifiers ANSI Keywords Data Types Dat a Type Qual i f i er s Variable Declarations and Definitions Lab Ex er c i se 1: Var i abl es and Dat a Types The #include Directive Literal Constants Symbolic Constants 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 Tut or i al Home 32 31 31 64 63 63 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 Data Type Qualifiers - Developer Help http://microchip.wikidot.com/tls2101:type-qualifiers[6/21/2014 11:28:52 PM] Microchip Technology Inc.
Return to Top Qual i f i ed Type Absol ut e Mi n Absol ut e Max Si ze (Bi t s) f l oat ~10 ~10 32 doubl e ~10 ~10 32 l ong doubl e ~10 ~10 64 Most compilers define f l oat and doubl e as shown in the table above, while some allow l ong to be applied to f l oat and doubl e. The data above is representative of the MPLAB XC16 compiler, but should be similar for all IEEE-754 format floating point implementations. Some compilers provide a setting or command line switch that will force doubl e to behave as l ong doubl e in the table. Rel at ed Pages Literal Qualifiers - 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 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 Rel at ed Sel f -Pac ed Tr ai ni ng Coming Soon Training -44.85 38.53 -44.85 38.53 -323.3 308.3 Variable Declarations and Definitions - Developer Help http://microchip.wikidot.com/tls2101:variable-declarations-definitions[6/21/2014 11:29:23 PM] Fundament al s of t he C Pr ogr ammi ng Language Var i abl e Dec l ar at i ons and Def i ni t i ons Home Training Fundamentals of the C Programming Language Variable Declarations and Definitions In the C programming language, variables must be declared before they can be used. This tells the compiler how to work with the variable and tells the linker how much space needs to be allocated for it. Var i abl e Dec l ar at i ons To declare a variable, you need to provide its type and an identifier (name). The general form for dec l ar i ng variables is: This means you can declare one or more variables of the same type by starting with the type, followed by a comma separated list of identifiers with a semicolon at the very end. Variables of the same type don't need to be declared together. You could also split them up into multiple declarations of a single variable, each on its own line. In fact, this is a more common practice as it makes it easier to add a comment after each variable to describe its purpose. Here are a few examples of variable declarations: Var i abl e Dec l ar at i ons w i t h Def i ni t i ons Sometimes, you will want to ensure that a variable has an initial value for its first use. Conveniently, this can be done as part of the variable's declaration. The general form to both dec l ar e and def i ne a variable in one step looks like this: Var i abl e Dec l ar at i on A variable declaration is when you specify a type and an identifier, but have not yet assigned a value to the variable.
Var i abl e Def i ni t i on A variable definition is when you assign a value to a variable, typically with the assignment operator =. type identifier , identifier , identifier ; 1 2 3 4 i nt x, y, z; f l oat warpFactor; c har text_buffer[10]; // This is an array variable - will discuss later in class unsi gned index; // int is optional when modified - this is an unsigned int ? type identifier = value , identifier = value , identifier = value ; 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 Variables Identifiers ANSI Keywords Data Types Data Type Qualifiers Var i abl e Dec l ar at i ons and Def i ni t i ons Lab Ex er c i se 1: Var i abl es and Dat a Types The #include Directive Literal Constants Symbolic Constants 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 Tut or i al Home 1 2 n 1 1 2 2 n n 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 Variable Declarations and Definitions - Developer Help http://microchip.wikidot.com/tls2101:variable-declarations-definitions[6/21/2014 11:29:23 PM] Microchip Technology Inc.
Return to Top While initializing variables like this can be very convenient, there is a price to pay in terms of startup time. The C Runtime Environment startup code initializes these variables before your main() function is called. The initial values are stored along with program code in the non-volatile flash memory. When the device powers on, the startup code will loop through all the initialized variables and copy their initial values from flash into the variable's RAM location. Here are a few examples showing the various ways variables can be declared alone or declared and defined together: Rel at ed Pages Variables - Fundamentals of the C Programming Language Fundamentals of the C Programming Language - Training +FLAGS 1 2 3 4 5 6 7 unsi gned i nt x; unsi gned y = 12; i nt a, b, c; l ong i nt myVar = 0x12345678; l ong z; c har first = 'a' , second, third = 'c'; // first and third initialized, second not f l oat big_number = 6.02e+23; ? 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 Exit Tutorial Rel at ed Sel f -Pac ed Tr ai ni ng Coming Soon Training