Saturday, April 19, 2025

C++ Constants - #define

Another simple way to create constants is to use #define preprocessor.

Again #define section must be above main() section, just as when we were using "const" keyword.

The important thing here is that there is no need to use "=" symbol between a constant name and value we provide.

It's very simple and clean solution: 

#include <iostream>

using namespace std;

#define PI 3.14
#define CAR 'Y'
#define STR "This is some string"
#define HEAD_LINE "------------------------"

int main() {

    cout << HEAD_LINE << endl;

    cout << PI << endl;
    cout << CAR << endl;
    cout << STR << endl;

    cout << HEAD_LINE << endl;

    return 0;

}

Result:

------------------------
3.14
Y
This is some string
------------------------

Process returned 0 (0x0)   execution time : 0.182 s
Press any key to continue.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...