Types of Data / The Category Integer
Subtitles of the Movie
One of the things that you find yourself doing a lot in C is declaring an editor variable to be used as an indicator of some sort and then using the P processor to define a collection of named integer values to go into it. The value is used to specify a classification or category. It usually looks something like this. During execution of the program, the time of day variable is set to one of the three values to be used as an indicator. Then later in the program it is only necessary to compare the value with the defined constants to determine the time of day. A new construct borrowed from another language can be used to do exactly the same thing. You declare it this way. This defines an integer variable named time of day and three named constants. The values for the constants are chosen automatically. In your program you can use the named constant just like you would a defined constant and assign its value to the variable. Then later you can test the contents of the variable for a match on one of the names. The names defined this way look like they are members of the enum the way you can have members of a struct or union. But they're not. They're simply defined constants and time of day is just another integer. You could assign any value to it and you can use it in any expression. Normally you don't need to know the values assigned to the names but there is a pattern. The first one is always number zero unless you choose to override it with another value. After that the values of each name is one greater than the name that comes before it, unless you use the equal sign to override the default value. In this example, the values are blue zero, red one, orange five and yellow six. Unless you override a value, it's always one more than the name right before it. The names at the top and at the bottom of the enum definition work just like the names at the top and the bottom of a struct. In this example, the names cover and patch both are declared integers expecting to be assigned enum values. The name at the top, cloth, can be used on an enum declaration later to create more integer declarations to hold enum-defined values. The only real difference between using the pre-processor and using the enum keyword is the scope. With a pre-processor, the names are defined for the entire source module. With the enum declaration, it is possible to limit the scope to a single function.
Tutorial Information
| Course: | Advanced C Programming |
| Author: | Arthur Griffith |
| SKU: | 33965 |
| ISBN: | 1-935320-24-6 |
| Release Date: | 2009-01-30 |
| Duration: | 5.5 hrs / 82 lessons |
| Work Files: |
Yes |
| Captions: | Available on CD and Online University |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |
VTC Sign up & Benefits
- Unlimited Access
- 81,350 Video Tutorials (20,800 free)
- Video Available as Flash or QuickTime
- Over 782 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 