1 / 23

Enumerated Types

Enumerated Types Sometimes we may wish to have a variable whose values are not numeric, for example, the variable size might have values coming from the set small, medium, large.

dlourdes
Télécharger la présentation

Enumerated Types

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Enumerated Types Sometimes we may wish to have a variable whose values are not numeric, for example, the variable size might have values coming from the set small, medium, large. We could create strings for this purpose, but this would require doing string comparisons to determine which value size currently has. An easy solution is to use an integer for each, so small might be represented by 0, medium by 1, and large by 2.

  2. Enumerated Types The problem with this is that the programmer must remember which integer is associated with each value. A solution to this problem is to use enumerated types. enum name{ value1, value2, . . . }; Enumerated types are special types that allow non-numeric values to have integers associated with them: the first has a value of 0, the second a value of 1, and so on. Enumerated types make it easier to read the source code since the non-numeric names can be used.

  3. Enumerated Types Here is an example of a program without enumerated types: #include <stdio.h> int main(void) { /* apple = 0; lemon = 1; orange = 2; pear = 3 */ int tree; tree = 2; /* 2 means orange */ switch(tree) { case 0: printf("You bought an apple tree.\n"); break; case 1: printf("You bought a lemon tree.\n"); break; case 2: printf("You bought an orange tree.\n"); break; case 3: printf("You bought a pear tree.\n"); break; } }

  4. Enumerated Types Here is the program with enumerated types: #include <stdio.h> enum fruit {apple, lemon, orange, pear}; int main(void) { enum fruit tree; tree = orange; switch(tree) { case apple: printf("You bought an apple tree.\n"); break; case lemon: printf("You bought a lemon tree.\n"); break; case orange: printf("You bought an orange tree.\n"); break; case pear: printf("You bought a pear tree.\n"); break; } }

  5. Typedef We can use the typedef keyword to give an alternate name to a type. Example: #include <stdio.h> typedef double DBL; /* typedefs don’t have to be capitalized, but that is the convention. */ int main(void) { DBL num = 2.3; /* creates a variable of type double */ printf("the size of DBL is %d\n", sizeof(DBL) ); printf("num is %f\n", num); } produces the size of DBL is 8 num is 2.300000

  6. Typedef typedef can be used with structures; in fact, FILE is a structure for which a typedef was used. Example: #include <stdio.h> typedef struct { char name[20]; int age; } PERSON; int main(void) { PERSON John = {"John Smith", 25}; PERSON *ptr = &John; printf("%s is %d years old.\n", John.name, ptr->age); }

  7. static Variables The variables that we have used so far are called automatic variables. • Variables created inside functions only exist as long as the function is executing. • The values of these variables are lost after the function completes its execution. • The keyword for declaring an automatic variable is auto: auto int x; • Local variables are automatic variables by default.

  8. Static Variables Static variables: • Remain in memory until the program terminates. • If a static variable is created in a function it will still exist with its last value the next time the function is called. • Static variables are automatically initialized to zero. • The keyword for declaring a static variable is static. See example-static.c on the course website.

  9. Defining Constants Sometimes we may have a value that is constant and used in several places in a program. For this, we may wish to use a preprocessor directive to define the constant. We use the following syntax for this: #define name value Then in our program, we use name in each place where we would have used value.

  10. Defining Constants #include <stdio.h> #define PI 3.14159 double calcArea(double); double calcCircumference(double); int main(void) { double r = 1.5; printf("The area of a circle with a radius of %5.2f is %f\n", r, calcArea(r) ); printf("Its circumference is %f\n", calcCircumference(r) ); } double calcArea(double r) { return PI*r*r; } double calcCircumference(double r) { return 2*PI*r; }

  11. Global Variables All of the variables that we have declared in our programs have been local variables. That is, they were known only within the function in which they were declared. Variables declared outside of any function are called global variables. Global variables are static by default.

  12. Global Variables You may look at this and see global variables as the solution to having to pass variables to functions. Global variables are generally considered bad practice. They can make code more difficult to debug since it is unclear which function changed a variable’s value. This becomes more the case as the number of functions in a program grows, especially if functions are in multiple source files to make them reusable.

  13. Multiple Source Files The source code for large programs is typically split over many different files. This has the advantage of making the re-use of functions easy. Function declarations for functions defined outside a file are placed in a separate header file. The header file needs to be included in each file where one of the declared functions is called.

  14. Multiple Source Files – GCC If we have our source code split among the files mainfile.c, myfunctions.c, and myfunctions.h, we can compile it using gcc mainfile.c myfunctions.c -o mainfile to produce mainfile.

  15. Bits and Bytes Recall that the smallest storage unit is a bit, which we typically think of having a value of 0 or 1. C allows us to make changes at the bit level. However, we can’t do so directly, but there are indirect methods to change bits. Some uses for bit operations are systems programming, encryption, compression, and image encoding.

  16. Binary Here are the first 16 numbers in decimal, hexadecimal and binary (assumes an 8-bit representation): Decimal Binary Hexadecimal 0 00000000 0 1 00000001 1 2 00000010 2 3 00000011 3 4 00000100 4 5 00000101 5 6 00000110 6 7 00000111 7 8 00001000 8 9 00001001 9 10 00001010 A 11 00001011 B 12 00001100 C 13 00001101 D 14 00001110 E 15 00001111 F

  17. Bit Operators Symbol Name ~ bitwise-negation & bitwise-and | bitwise-or ^ bitwise-xor >> shift right << shift left Note: These can only be used with the integer types.

  18. Bitwise-negation The bitwise-negation reverses the bit pattern for a number. Example 1: If x has a bit pattern of 0001, then x has a bit pattern of 1110. Example 2: We use it like this: y = ˜x; or x = ˜x;

  19. Bitwise-and The bitwise-and compares the bit patterns of two numbers to produce a new bit pattern. When the two compared bits are a 1, the resulting bit will be a 1. Otherwise, it will be a 0. Example 1: 0001 0011 ------- 0001 Example 2: We use it like this: z = x & y; or x &= y;

  20. Bitwise-or The bitwise-or compares the bit patterns of two numbers to produce a new bit pattern. When either of the compared bits is a 1, the resulting bit will be a 1. Otherwise, it will be a 0. Example 1: 1001 0011 ------- 1011 Example 2: We use it like this: z = x | y; or x |= y;

  21. Bitwise-xor The bitwise-xor (where xor is exclusive or) compares the bit patterns of two numbers to produce a new bit pattern. When the compared bits differ, the resulting bit will be a 1. Otherwise, it will be a 0. Example 1: 0001 0011 ------- 0010 Example 2: We use it like this: z = x ^ y; or x ^= y;

  22. Shift Left and Shift Right The shift operators allow us to shift the bit patterns to the left or right. Example 1: If x has a bit pattern of 0001, then x << 1 has a bit pattern of 0010. Example 2: We use them like this: x = y >> 2; or y >>= 2;

  23. More Examples See example-bitwise.c on the course website.

More Related