280 likes | 395 Views
EEE 243B Applied Computer Programming. Limiting Variables, Type Definition and Enumerated Types § Apx J, 12.1 – 12.2. Review. How is a the name of a function similar to an array?
E N D
EEE 243BApplied Computer Programming Limiting Variables, Type Definition and Enumerated Types §Apx J, 12.1 – 12.2
Review • How is a the name of a function similar to an array? • I want to declare a pointer variable named: fpChar that points to a function which returns a pointer to char and takes two pointers to char as parameters. • Write your declaration here: Prof S.P. Leblanc
Outline • Limiting Variables • Spatial Limitation – scope • Temporal Limitation – extent • Storage Classes • The type definition typedef • Enumerated types • Enumerated type definition Prof S.P. Leblanc
1a. Spatial limitation – scope • We have talked about global and local variables before • These two terms refer to the scope of a variable or its spatial limitation • The scope of a variable determines its visibility in space • Where you can see and use the variable • The scope of a variable should be kept to the smallest useful size: If a variable is not needed outside a block do not make it visible by declaring it outside that block Prof S.P. Leblanc
1a. Spatial limitation – scope • The main function contains a primary block. • If a variable is declared above the main function (outside the primary block) than it is in scope for the entire compilation unit. It is possible to make variables available to any compilation unit that know about the variable!!! • These are known as "globals". • Only type-defined structures and enumerated type definitions should be global to the compilation unit. Not variables. Prof S.P. Leblanc
1a Example: scope int array[7] = {21, 43, 8, 99, 12}; // Scope of array int max(int x, int y) { return (x>y? x: y); } // Scope of Max int main(void) { int temp; // Scope of temp static intcallCount; // Scope of callCount double* newD; // Scope of newD; } What is the scope of the following functions and variables? Prof S.P. Leblanc
1b. Temporal limitation - extent • So now we know about scope • the spatial limitation of a variable; • its territory. • What about its lifespan? • The temporal limitation of a variable or its extent determines when a variable comes to life, when it can be used and when it dies. Prof S.P. Leblanc
1b. Temporal limitation - extent • Variables typically exist only for the duration of their function call • When the function is called, new space is allocated on the stack for those variables • The variables are used during the function's execution • On return from the function, the space where those variables were held is freed and the variable cease to exist • On next function call, the variables are recreated on the stack (smashing the previous values) Prof S.P. Leblanc
1b Temporal limitation - extent • By declaring a variable as static inside a function, the variable will save the value between executions of the function • Recall that that static variables are saved in the data section of our memory model • The variable will only die at program termination • A static variable can be initialized where it is declared. It will only be initialized once. • If you do not initialize a variable its value is zero. Prof S.P. Leblanc
1b Temporal limitation - extent • A static variable is declared like this: … void Fctn (void) { static intanInt = 0; //init only once return anInt++; //This will keep a count //of how many times the //function is called. } Prof S.P. Leblanc
1b Temporal limitation - extent • Note that even if the extent of the variable changes by using the key word static, the scope does not change! • The variable can only be used inside the function. Prof S.P. Leblanc
Scope and extent • static is one of the storage classes • So far all your variables that you used were from the auto class; you never used this as a keyword in front of your variables because it is the default • There are two other storage classes (extern and register) but we will not study those in this course Prof S.P. Leblanc
1b Example: Extent int array[7] = {21, 43, 8, 99, 12}; // Extent of array int max(int x, int y) { return (x>y? x: y); } // Extent of Max int main(void) { int temp; // Extent of temp double *newD; // Extent of newD; } intmyFunction (void) { static intcallCount; // Extent of } // callCount What is the extent of the following functions and variables? Prof S.P. Leblanc
Structures and derived types • So far we have seen one data structure: the array. • An array has three characteristics: • Name, type and a fixed number of elements • Once declared, the array can only contain elements of the same type • We have also seen two derived types: the array and the pointer. • These types are derived because they change the range of values and the operations defined on the basic type Prof S.P. Leblanc
2. Type definition • Type definitions (typedef) are central to creating new data types in C • With typedef you can create a new type from any other type • Standard or Derived • We will see later today how typedef helps us to define complex data types • But we will first start with two simple examples Prof S.P. Leblanc
2. Type definition • You define a new type with the typedef definition format: typedef type IDENTIFIER; Uppercase by convention Keyword Any basic or derived type Prof S.P. Leblanc
2. Type definition • You could (but should not) define a new type from the type int by doing the following: typedefint POUNDS; • Then the following two declarations would then be equivalent: int number=180; POUNDS number=180; • Typically, this is done to make programs more readable or portable • eg: typedef unsigned intINT16U Prof S.P. Leblanc
2. Type definition • Some programmers use typedef to define a new type called STRING from the derived type pointer to character (char*) typedef char* STRING; … STRING myString;//a pointer to char • This would have been the same as: char* myString; Prof S.P. Leblanc
2. Type definition • The previous two examples are simple, but they show the built-in flexibility to define new types in the C language • In the syntax of the language, you can use the name of a type definition wherever you can use the name of a standard type: • Declaration of variables • Declaration of a function type • Declaration of parameters • Casting operations,… Prof S.P. Leblanc
3. Enumerated types • The enumerated type, enum builds on top of the int type • In an enumerated type, each integer value is associated with an identifier called an enumeration constant • You already have seen and used several enumerated types: • MOTORDIRECTION: off, fwd, rev • MOTORPORT: LEFT_MOTOR, RIGHT_MOTOR, BOTH_MOTORS Prof S.P. Leblanc
3. Enumerated types • There are two basic ways of declaring an enumerated type, but we only teach the preferred method: //The enumeration proper enum TAG {enumeration constants}; //Declaring a variable for the enum enum TAG variable_identifier; Prof S.P. Leblanc
3. Enumerated types - example • For example if I want to set an enumerated type for the months of the year, I could do the following: enum MONTHS {JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC}; enum MONTHS birthMonth; //declaration enum MONTHS graduationMonth = MAY; Prof S.P. Leblanc
3. Enumerated types • Note that because in C data structures are zero-based, the value of the first element in the enumeration is 0 so JAN = 0 • But we can specify values explicitly: enum MONTHS {JAN = 1, FEB = 2,…}; • If I only specify the first value then the complier fills in the next values by adding one to each new item. enum MONTHS {JAN = 1, FEB, MAR, APR,…}; Prof S.P. Leblanc
3. Enumerated type Two words of caution about enumerated types. • C allows two enumeration constants to have the same value. This is not a good practice. enum SIZES {SMALL = 1, BIG = 2, VERY_BIG = 2}; • There is no range checking on the values assigned to variables: enum SIZES mySizes= 4; //just a compiler warning Prof S.P. Leblanc
4. Enumerated type definition • Every time you declare a new variable for your enumerated type, you must use the enum keyword: enum MONTHS birthMonth; • We learned earlier today that we can rename a type to something else using typedef Prof S.P. Leblanc
4. Enumerated type definition • Here we simply combine both the typedef and the enum keywords: typedefenum {RED, BLUE, YELLOW} PRIMCOLOR; PRIMCOLOR firstColor = RED; PRIMCOLOR secondColor = BLUE; Prof S.P. Leblanc
Quiz Time • Define an enumerated type for transmission type (manual or automatic) • Define a type-defined enumerated type for car colour (RED, WHITE, YELLOW, GREEN, BEIGE, GREY, BLACK) Prof S.P. Leblanc
Next lecture • Structures Prof S.P. Leblanc