0% found this document useful (0 votes)
92 views15 pages

Programming Ii: Enumeration Constants & Macros

This document discusses enumeration constants, macros, and typedefs in C programming. It defines: 1) Enumeration constants as user-defined types that contain a list of constants that can be assigned integer values. Enumerations are defined using the "enum" keyword. 2) Macros as single identifiers that are equivalent to expressions or statements. Macros are defined using the #define preprocessor directive and can be used with or without arguments. 3) Typedefs as a way to define a new name for an existing data type that can then be used anywhere a type is permitted. It provides examples of declaring and using enumerations, macros, and typedefs in C code.

Uploaded by

Stephan Smith
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
92 views15 pages

Programming Ii: Enumeration Constants & Macros

This document discusses enumeration constants, macros, and typedefs in C programming. It defines: 1) Enumeration constants as user-defined types that contain a list of constants that can be assigned integer values. Enumerations are defined using the "enum" keyword. 2) Macros as single identifiers that are equivalent to expressions or statements. Macros are defined using the #define preprocessor directive and can be used with or without arguments. 3) Typedefs as a way to define a new name for an existing data type that can then be used anywhere a type is permitted. It provides examples of declaring and using enumerations, macros, and typedefs in C code.

Uploaded by

Stephan Smith
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 15

PROGRAMMING II

Enumeration Constants & Macros

Enumerations

User-defined type Members are constants written as identifiers contain a list of constants that can be addressed in integer values.

Uses the keyword enum


Values start at 0, unless otherwise specified.

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};
-

Type is enum months Identifiers set to integers 0 5 respectively To number months 1 6:

enum months {JAN = 1, FEB, MAR, APR, MAY, JUN};

Example
main() {
enum months month; //from previous declaration int cnt; month = (enum months) 3;

} Sample program here

for (month =JAN; month <= JUN; month++) printf(%d, month);

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)

EXAMPLE #define volume ( (length) * (breadth) * (height) )


main() { length = 4 * 2; breadth = 8 + 4; height = length + breadth; printf(Volume is %d, volume); }

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 (With Arguments)


#define EVEN(X) (X%2 == \ 0? printf(yes) ) main() { int num = 5; EVEN(num); }

Macros

(String Converting Operator #)

EXAMPLE USING TEXT #define HELLO(x) printf(Hello #x) main() { HELLO(John); }

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

You might also like