2075 7

CSC115 ยท 2075 ยท 5 marks

What is preprocessor directive? Discuss #define derivative with example. [1.5+3.5]

What is preprocessor directive? Discuss #define derivative with example. [1.5+3.5]

C ProgrammingChapter 2: Elements of CStructure of a C program

Quick answer

A preprocessor directive is an instruction given to the C preprocessor -- a program that processes the source code before actual compilation begins. Preprocessor directives tell the preprocessor to perform specific operations such as file inclusion, macro substitution, or conditional compilation.

Preprocessor Directive and #define Directive


What is a Preprocessor Directive? [1.5 marks]

Key characteristics:

  • All preprocessor directives begin with the # (hash) symbol.
  • They are not C statements, so they do not end with a semicolon (;).
  • They are processed before the compiler translates the source code into object code.
  • They appear at the top of the program, in the preprocessor directives section of the C program structure.

Common preprocessor directives:

DirectivePurpose
#includeIncludes a header file
#defineDefines a macro or symbolic constant
#undefUndefines a macro
#ifdef / #ifndefConditional compilation

The #define Directive [3.5 marks]

The #define directive is used to define macros in C. A macro is a name (identifier) that is replaced by a value or expression throughout the program by the preprocessor before compilation.

Syntax:

#define MACRO_NAME replacement_text

The preprocessor replaces every occurrence of MACRO_NAME in the source code with replacement_text before the compiler processes the file.


Types of #define Usage:


1. Symbolic Constants (Object-like Macros)

Used to define constant values. This improves readability and maintainability of the program -- if the value needs to change, it only needs to be changed in one place.

Example:

#include <stdio.h>
#define PI 3.14159
#define MAX 100

int main() {
    float radius, area;
    printf("Enter radius: ");
    scanf("%f", &radius);
    area = PI * radius * radius;
    printf("Area of circle = %.2f\n", area);
    return 0;
}

Output (for radius = 5):

Area of circle = 78.54

Here, every occurrence of PI is replaced by 3.14159 before compilation.


2. Function-like Macros (Macros with Parameters)

#define can also define macros that accept arguments, similar to functions but handled by the preprocessor.

Syntax:

#define MACRO_NAME(parameter) expression

Example:

#include <stdio.h>
#define SQUARE(x)   ((x) * (x))
#define MAX(a, b)   ((a) > (b) ? (a) : (b))

int main() {
    int num = 5;
    printf("Square of %d = %d\n", num, SQUARE(num));
    printf("Maximum of 10 and 20 = %d\n", MAX(10, 20));
    return 0;
}

Output:

Square of 5 = 25
Maximum of 10 and 20 = 20

Note: Parentheses around parameters in macros are important to avoid operator precedence errors. For example, SQUARE(2+3) without parentheses would give 2+3*2+3 = 11 (wrong), but with parentheses ((2+3)*(2+3)) = 25 (correct).


Important Points about #define:

PointDetail
No memory allocationMacros are just text substitution; no variable is created
No type checkingUnlike variables or functions, the preprocessor does not check data types
No semicolon#define PI 3.14; would include the semicolon in the replacement, causing errors
ScopeA macro defined with #define is valid from the point of definition to the end of the file (or until #undef)

Summary

The #define directive is a powerful preprocessor tool in C that allows programmers to define symbolic constants and function-like macros. It makes programs more readable, portable, and easy to maintain by avoiding the use of hard-coded values throughout the code.

Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.