#define directive – Preprocessor for Names to constant replacement – Computer Sir Ki Class


Lost your password?

Don't have an account ?
Register (It's FREE) ×


Code Learning #CPP#4580    siteicon   siteicon  

#define directive – Preprocessor for Names to constant replacement

Understanding the purpose of #define pre-processor directive.

Learning Objectives

  • Understanding the use of #define for the purpose of symbol to constant value replacement.

Source Code

TC++ #4580


Source Code

Run Output

Area of circle with radius 5 = 78.54
Circumference of circle with radius 5 = 31.416

Code Understanding

#define PI 3.1416
Here we define a symbol called PI. Whenever PI occurs in a program (not inside string literals etc) it will be replaced by a value 3.1416

#define R 5
Here another symbol R has been defined which will be replaced by value 5

int main(){

double area=PI*R*R;
double circum=2*PI*R;
Here both PI and R will be replaced by 3.1416 and 5 respectively while compilation

cout<<“Area of circle with radius “<<R
<<” = “<<area<<endl;
cout<<“Circumference of circle with radius “<<R
<<” = “<<circum<<endl;
In both the above statements R will be replaced by 5. Notice that R is not a variable but it is a predefined value known only to the compiler and it is not relevant at runtime.

return 0;


  • #defines are powerful ways of value replacements at the time of compilation. But some people do not like them as they do give importance to their data type. Data type errors if any will not be detectable at compile time.



Common Errors

  • Notice that #define lines in the C++ program are not actually statements in true sense, that is why you should not put a semicolon (;) character at the end.

Suggested Filename(s): pp-def.cpp, define-directive.cpp


sunmitra| Created: 1-Sep-2018 | Updated: 1-Sep-2018|

Introductory Sessions Beginning to Program Tokens Keyword and Identifiers Data Types Variables and Constants Operators Simple User Input Building Expressions and Formulas Simple Real World Problems Simple If and If Else Multiple-Nested-Ladder of If Else Switch case selection Simple Loops Tricks in Loops - break continue scope Loop Applications - Handling numerals Series printing loops Nested Loops Pattern printing loops Number Varieties and Crunches String Handling (Null Terminated) Strings - string class type Functions (Built-in) Functions - user defined Functions Reference Passing/Returning Arrays Concepts and 1-D Arrays Array Data Management Two dimensional arrays and Matrices Structures Basics Structures passing/returning 2D Array Memory Addressing Display Using IO Manipulation Display Using C Formatting Tricks User Defined Data Types Enumerated Types Preprocessor Directives And Macros Exception Handling Programming Paradigms and OOPs Advantages Abstraction and Encapsulation Polymorphism Inheritance Function Overloading Concepts Function Overloading Varieties Function Overloading Special Cases Defining Classes Creating and Using Class Objects Class Members Accessibility Class Function Types Inline Functions Constant Functions Nesting of Functions Class Members Scope Resolution Static Members in a Class Array of Objects Constructor Concepts Default Constructor Parameterized Constructor Copy Constructor Constructor Overloading Destructors Inheritance Fundamentals Public Derivations Private and Protected Derivations Multiple Inheritance Multi-Level Inheritance Class Nesting Data File Concepts Handling Text Files Handling Binary Files Pointer Concepts Pointer and Arrays Pointers and Functions Object Pointers This Pointer Linked Lists Stacks Queues