C++ Define Directive

  • Post author:
Download PDF
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

This Post Has 9 Comments

  1. minecraft

    Simply want to say your article is as astonishing. The clearness in your
    post is just nice and i could assume you’re an expert on this
    subject. Fine with your permission let me to grab your RSS feed to keep up to date with forthcoming post.
    Thanks a million and please carry on the rewarding work.

  2. minecraft

    Very good post. I will be dealing with many of these issues as
    well..

  3. minecraft

    You should take part in a contest for one of the highest quality sites on the internet.
    I am going to recommend this blog!

  4. Michal

    Howdy would you mind letting me know which hosting company
    you’re utilizing? I’ve loaded your blog in 3
    different web browsers and I must say this blog loads a lot faster then most.
    Can you suggest a good web hosting provider at a honest price?
    Thank you, I appreciate it!

  5. Felica

    I do believe all of the ideas you have presented in your post.

    They’re very convincing and can certainly work. Still, the posts are very brief for newbies.
    Could you please extend them a bit from next time?

    Thank you for the post.

  6. here

    Usually I do not read post on blogs, but I would like to say that this write-up very pressured me to check
    out and do it! Your writing taste has been surprised me.
    Thank you, very great article.

  7. 192.168.1.1 login

    Hello! This is my 1st comment here so I just wanted to give a quick shout out and say
    I really enjoy reading through your articles. Can you suggest any other blogs/websites/forums that cover the same topics?
    Thank you!

  8. read more

    Great blog you’ve got here.. It’s hard to find quality writing like yours nowadays.

    I seriously appreciate individuals like you! Take care!!

  9. admin login

    I like the helpful information you provide to your articles.
    I will bookmark your weblog and check again right here regularly.
    I’m reasonably certain I will be informed lots of new stuff
    right right here! Good luck for the next!

Comments are closed.