Chp 15 Miscellaneous Features Flashcards
Enumerated Data Types:
Enables us to invent our data types and define what values the variable of this data type can take.
enum definition:
enum mar_status { single, married, divorced, widowed }; enum mar_status person1, person2;
Enumerators:
Possible values the variable of our invented data type can take.
What do parts of enum declaration do:
1. enum mar_status { single, married, divorced, widowed }; Declares the data type and specifies its possible values (enumerators).
- enum mar_status person1, person2;
declares variables of this data type.
internally how does the compiler treat enumerators:
compilers treats enumerators as integers.
Each value on the list of permissible values corresponds to an integer starting from 0 or
it can be overridden by initialising the enumerators to diff integer as :
enum mar_status { single = 100, married = 200, divorced=300, widowed=400 }; enum mar_status person1, person2;
Use of Enumerated Data Type:
Clarify the operation of program.
Weakness of enum:
There is no way to use enumerate values directly in input/output functions like printf() and scanf().
It will print the integer corresponding to it.
look into scanf().
A way to achieve exactly what enum does?
why use enums?
Using macro #define SINGLE 0
MACROS are global.
enum can be global( if declared outside all functions) or local ( declared inside a function).
Renaming Data types with typedef:
define it.
rules.. uses..
typedef unsigned long int TWOWORDS;
Its purpose is to redefine the name of an existing variable type.
Preferred uppercase to show that we are dealing with a renamed data type.
Used especially when long data type names such as structure.
Its also used to rename pointer datatype.
Use of typedef for structures:
and pointer:
Syntax
struct employee
{ … };
typedef struct employee EMP;
EMP e1,e2;
OR typedef struct employee { ... } EMP; EMP e1,e2; OR
struct employee { ... int age; ...}; typedef struct employee *PEMP; PEMP p; p -> age = 32;
Typecasting:
To explicitly convert the value of an expression to a particular type.
example: (int)a;
It doesn’t permanently change. Rather it is the value of the expression that undergoes type conversion whenever the cast appears.
Bit Fields:
There are no 1 bit, 2 bit, 3-bit data type available in C. However when there are several values whose maximum value are small enough to pack in a single memory location, we can use ‘bit fields’ to store several values in a single integer.
declare a structure using bit field:
struct employee
{
unsigned gender : 1 ; unsigned mar_stat : 2;
};
The colon tells the compiler we are talking about bit fields and the number followed by it tells how many bits to allot for the field.
Notice Unsigned.
Pointers to Function:
int ( *func)()
{….}
Its prototype int (*func)(); func = funcName; Means func is a pointer to a function, which returns int. To call it: (*func)();
Address of function: funcName
Pointers to every type of variable? is it true?
Every type of variable has an address with exception of register.