Broad Network


The C++ Pragma Directive and Some Predefined Macro Names

C++ Preprocessing Directives – Part 5

Forward: In this part of the series, I talk about the Pragma Directive and some predefined macro names in C++.

By: Chrysanthus Date Published: 18 Sep 2012

Introduction

This is part 5 of my series, C++ Preprocessing Directives. In this part of the series, I talk about the Pragma Directive and some predefined macro names in C++. You must be reading the tutorials in this series, in the order given.

The pragma Directive
The syntax of the pragma directive is:

#pragma pp-tokensopt new-line

Now, pragma is a reserved word; pp-tokensopt is a word-like code that the C++ programmer types; new-line means press the Enter Key.

The #pragma directive is used by compilers. If a company produces a compiler that does not operate strictly according to the C++ specification, then the company would always require that a C++ programmer types the #pragma directive at the top of the main C++ file. Failure to do this, means the program will either not compile properly or not compile at all. The company tells you the programmer what exactly you should type in place of pp-tokensopt, in the directive. An example of such a company is Borland.

A good company would take this decision, with the hope of producing a compiler that is better than what the specification expects.

Some Predefined Macro Names

Predefined macro names are constant identifiers. Such an identifier identifies only one particular value. Such identifiers are predefined in the compiler and you do not have to re-define them again in your program.The rest of this part of the series, deals with some of them.

The __LINE__ Macro Name
The __LINE__ identifier (macro name) holds the value of the line in the source file in which it is typed. Read and try the following program that displays the line in which __LINE__ is typed:


#include <iostream>
using namespace std;

#define VERSION 2

#ifdef VERSION
#define EDITION 3
#else
#define VERSION 3
#endif

int main()
    {
        cout << VERSION << "\n";

        cout << __LINE__ ;

        return 0;
    }


Note: __ is two underscores typed next to one another.

The __FILE__ Macro Name
This identifier holds the name of the source file. Read and try the following code:

#include <iostream>
using namespace std;

int main()
    {

        cout << __FILE__ ;

        return 0;
    }

The __DATE__ Macro Name
This macro name would hold a date read from the computer. It is the date of compilation of the source file (it is a character string literal of the form "Mmm dd yyyy", and the first character of dd is a space character if the value is less than 10).

Read and try the following code that illustrates this:

#include <iostream>
using namespace std;

int main()
    {

        cout << __DATE__ ;

        return 0;
    }

The __TIME__ Macro Name
This macro name would hold a time read from the computer. It is the time of compilation of the source file (it is a character string literal of the form "hh:mm:ss").

Read and try the following code that illustrates this:

#include <iostream>
using namespace std;

int main()
    {

        cout << __TIME__ ;

        return 0;
    }

End of Tutorial and End of Series
The is the end of the tutorial and the end of the series. I hope you appreciated the series. A question you probably have is how to produce a large C++ program as opposed to a long C++ program (that is limited in capacity - content); I address that in a series ahead of this one.

I assume you have completed this series. It means you already had the professional skills in C++. Now you are in an advanced course of C++. You may already be working. If you ever have too much work, you can subcontract (outsource) some or all to someone in the site below.

Chrys

Related Courses

C++ Course
Relational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem

Comments

Become the Writer's Fan
Send the Writer a Message