Professional Documents
Culture Documents
Enumerations
User-defined type Members are constants written as identifiers contain a list of constants that can be addressed in integer values.
General Format
enum tag {member1, member2, , member n}; or typedef enum {mem1, mem2}TYPE; Once defined, enumeration variables can be declared as: enum tag variable1, variable2, ..variable_n; or TYPE variable1, variable2, ..variable_n;
Visiting TYPEDEF
-
Gives a name to a data type by creating a new type that can be used anywhere a type is permitted.
typedef int INTEGER; Typedef char* STRING; Main() { INTEGER num1, num2; }
Example
enum months {JAN, FEB, MAR, APR, MAY, JUN};
-
Example
main() {
enum months month; //from previous declaration int cnt; month = (enum months) 3;
Macros
Single identifiers equivalent to expressions or statements Identifier defined in a #define preprocessor directive May be defined with or without arguments Without arguments similar to symbolic constant
With arguments arguments substituted, then macro expanded
Macros
FORMAT #define macro_name replacement-text EXAMPLE (Without Arguments) #define Hello printf(Hello Everybody) main() { Hello; }
Macros
(Without Arguments)
Macros
FORMAT #define macro_name(argument1, ) replacement-text EXAMPLE (With Arguments) #define PRINT(x) printf(Value is %d, x) main() { int val = 5; PRINT(val); }
Macros
printf(Hello John);
Macros
(With Aruments)
EXAMPLE USING TEXT #define POS(x) printf(#x " = %d",x);(x>0? \ printf(" is positive"):printf(" is negative")) main() { int num = -9; POS(num2); } num2 = -9 is negative
Macros (nested)
#define PI 3.14 #define CIRCUM(rad) (PI * rad * rad)
main() { float radius = 12.34; printf(Circumference is %f, CIRCUM(radius)); }
Next Class
ARRAYS