C++ Define Directive

  • Post author:
  • Post category:cpp-category
  • Reading time:6 mins read
Advertisement

C++ Define Directive

Define directive is also used to define a constant. The difference between the const qualifier define directive is that the directive does not specify the data type of the constant. Directive starts with the symbol #. It is not terminated with the semicolon.

Syntax

The syntax is as follows:

#define identifier value

# It indicates the start of the pre-processor directive.
Define It is used to define a constant.
Identifier It is the name of constant.
Value It represents the value associated with the identifier.

 

The pre-processor directive replaces all occurrences of the identifier with value. The identifier is conventionally written in uppercase.

Example

#define PI 3.141593

Remarks

The #define order makes the compiler substitute token-string for every event of the identifier in the source record. The identifier is supplanted just when it frames a token. That is, the identifier isn’t supplanted on the off chance that it shows up in a remark, in a string, or as a major aspect of a more extended identifier. For more data, see Tokens.

The token-string contention comprises of a progression of tokens, for example, catchphrases, constants, or finish proclamations. At least one void area characters must separate token-string from the identifier. This blank area isn’t viewed as a feature of the substituted content, nor is any void area that pursues the last token of the content.

A #define without a token-string expels events of the identifier from the source document. The identifier stays characterized and can be tried by utilizing the #if characterized and #ifdef orders.

The second linguistic structure shape characterizes a capacity like a full scale with parameters. This frame acknowledges a discretionary rundown of parameters that must show up in enclosures. After the full scale is characterized, each resulting event of identifier( identifier out, …, identifier out ) is supplanted with a rendition of the token-string contention that has genuine contentions substituted for formal parameters

Formal parameter names show up in token-string to stamp the areas where genuine qualities are substituted. Every parameter name can seem on various occasions in token-string, and the names can show up in any request. The number of contentions in the consider must match the number of parameters in the full-scale definition. Liberal utilization of enclosures ensures that unpredictable genuine contentions are translated accurately.

Advertisement

The formal parameters in the rundown are isolated by commas. Each name in the rundown must be one of a kind, and the rundown must be encased in enclosures. No spaces can isolate the identifier and the opening enclosure. Utilize line connection — put an oblique punctuation line (\) instantly before the newline character — for long orders on various source lines. The extent of a formal parameter name reaches out to the new line that closures token-string.

At the point when a full scale has been characterized in the second punctuation frame, resulting printed occurrences pursued by a contention list show a large scale call. The genuine contentions that pursue an occasion of the identifier in the source record are coordinated to the comparing formal parameters in the large scale definition. Every formal parameter in token-string that isn’t gone before by a string (#), characterizing (#@), or token-sticking (##) administrator, or not pursued by a ##operator, is supplanted by the relating real contention. Any macros in the genuine contention are extended before the mandate replaces the formal parameter. (The administrators are portrayed in Preprocessor Operators.)

Program:

Write a program that inputs the radius of a circle and displays the circumference by using formula 2. Store the value of in a constant by using the DEFINE directive.

  #include <iostream>
  #include <conio.h>
  #define PI 3.141
  using namespace std;
   void main()
  {
    float radius, area;
    cout<<"Enter Radius:";
    cin>>radius;
    area = 2.0* PI * radius;
    cout << "Area = "<<area;
    getch();
    return 0;
  }

 

C++ Constant

Advertisement

Zitoc

We are a team of writers, researchers, and editors who are passionate about helping others live their best lives. We believe that life is a beautiful gift. We try to live our lives to the fullest and enjoy every moment. We are always learning and growing, and we cherish the relationships we have with our family and friends.