Programs full of unexplained numbers such as 0.18 or 500 are hard to read and harder to change. C offers three ways to give meaningful names: #define for symbolic constants, enum for sets of named integers, and typedef for new names of types.
Symbolic constants with #define
Syntax
#define NAME replacement-text
Examples: #define MAX 100, #define PI 3.14159, #define TRUE 1.
Rules and conventions:
No = and no semicolon: #define MAX 100; would put 100; into every use of MAX.
Names are written in capitals by convention (not a rule), so they stand out from variables.
The replacement is plain text substitution done by the preprocessor; the compiler never sees the name.
Advantages: the program is easier to read, and a value used in many places is changed in one place only.
Enumerated types
An enumeration defines a type whose values are named integer constants:
The compiler numbers the constants from 0 upwards unless values are given. A constant without a value gets the previous value plus one.
typedef
typedef creates a new name (an alias) for an existing type. It does not create a new type.
Syntax
typedef existing-type new-name;
For example, typedef unsigned int uint; lets you write uint count; instead of unsigned int count;. In later units, typedef is mainly used to give short names to structures.
Key takeaways
#define NAME text is replaced by the preprocessor before compilation; no = or semicolon.
Enumeration constants are integers numbered from 0, each one more than the previous unless set.
typedef creates an alias for an existing type.
Practice questions
Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 6 · short answer
List two advantages of symbolic constants and write directives for a maximum of 60 students and a pass mark of 40.
Show worked answer
Advantages: (1) a meaningful name such as MAX_STUDENTS makes the code easier to read than the bare number 60; (2) if the value changes, it is edited in one place and the preprocessor updates every use. Directives: #define MAX_STUDENTS 60 and #define PASS_MARK 40.
MCA Online notes are made for reading on screen. Open https://mcastudy.online/mcs-201/data-types-operators-and-expressions/symbolic-constants-enumerations-and-typedef/ to read this page.