MCS-201 · Unit 2

Data Types, Operators and Expressions

Section 5 of 11

Block 1 · Unit 2Data Types, Operators and Expressions9 min · Section 5 of 11

2.5Symbolic Constants, Enumerations and typedef

Covers IGNOU MCS-201 §2.10

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:

C · enum_basic.c
#include <stdio.h>

int main(void)
{
    enum colour { BLACK, RED, BLUE, GREEN, WHITE };
    enum colour background = WHITE;

    printf("BLACK = %d, WHITE = %d\n", BLACK, WHITE);
    printf("background = %d\n", background);
    return 0;
}
Output
BLACK = 0, WHITE = 4
background = 4

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 1 · multiple choice

Which line correctly defines a symbolic constant for the GST rate 18%?

Show answer

Answer: B. #define GST_RATE 0.18

A #define has the name, a space and the replacement text, with no = and no semicolon. A trailing semicolon would be copied into every use.

Question 2 · multiple choice

Given enum level { LOW, MEDIUM = 5, HIGH };, what is the value of HIGH?

Show answer

Answer: C. 6

LOW is 0, MEDIUM is set to 5, and HIGH is the previous value plus one, 6. Counting positions (2) ignores the explicit value.

Question 3 · multiple choice

When is a #define symbolic constant replaced by its value?

Show answer

Answer: A. By the preprocessor, before compilation

The preprocessor performs text substitution before the compiler sees the code. Nothing about #define happens at run time.

Question 4 · multiple choice

What does typedef unsigned long ulong; do?

Show answer

Answer: C. Makes ulong another name for unsigned long

typedef creates an alias for an existing type. It does not declare a variable or a separate incompatible type.

Question 5 · multiple choice

What is printed?

C · macro_text.c
#include <stdio.h>

#define SIZE 10
#define DOUBLE_SIZE SIZE * 2

int main(void)
{
    printf("%d\n", 100 / DOUBLE_SIZE);
    return 0;
}
Show answer

Answer: C. 20

#define substitutes text, so the expression becomes 100 / 10 * 2, which is evaluated left to right as 10 × 2 = 20. The tempting answer 5 assumes DOUBLE_SIZE is the single value 20; writing #define DOUBLE_SIZE (SIZE * 2) would give 5.

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.

↑ ↓ move↵ openTab switch type