#define and const

what is difference between #define and const?
closed account (z1CpDjzh)
A const is a variable which is non-modiffiable...
Allowing you to do this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
const char *pie = "fart";

#include <iostream>

void fart_equilizer(const char *pie);

int main()
{
 fart_equilizer("bigger FART");
}
void fart_equilizer(const char *pie)
{
std::cout << pie;
std::cin.get();
}



While #define is a processor command which before compilation replaces everything with whatever it is

So:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#define pie google

#include <iostream>

void fart_equilizer(const char *pie);

int main()
{
 fart_equilizer("bigger FART");
}
void fart_equilizer(const char *pie)
{
std::cout << pie;
std::cin.get();
} 

is equevelnt to:
1
2
3
4
5
6
7
8
9
10
11
12
13
14

#include <iostream>

void fart_equilizer(const char *google);

int main()
{
 fart_equilizer("bigger FART");
}
void fart_equilizer(const char *google)
{
std::cout << google;
std::cin.get();
} 


and also thing defined by #define can be undefined using #undef allowing this code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#define pie google

#include <iostream>

void fart_equilizer(const char *pie);

int main()
{
 fart_equilizer("bigger FART");
}
#undef pie
#define pie google2
void fart_equilizer(const char *pie)
{
std::cout << pie;
std::cin.get();
}
to be the same as
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

void fart_equilizer(const char *google);

int main()
{
 fart_equilizer("bigger FART");
}


void fart_equilizer(const char *google2)
{
std::cout << google2;
std::cin.get();
}
while a constant is unmoddifable
Last edited on
Thank man to reply.
Is there any advantages of using macro in compilation time?
If yes then how?
There is little use for macro in C++: consexpr functions and variables are way safer and better.

Only feature of macro which is not replicable is parameter name stringification and parameter contacenation. Which is rarely used except in assert macro.
Macros can sometimes be useful if you want different code to be compiled on different platforms, or with different versions of a library.

1
2
3
4
5
#ifdef _WIN32
	// Call some Windows function.
#else
	// Use some other function that doesn't require the WinAPI.
#endif 


http://en.wikipedia.org/wiki/C_preprocessor#Conditional_compilation
closed account (z1CpDjzh)
@ak16 Yes there is many uses i can think of but here is just 3 of 'em:

Safe Guards
You every got the issue where you #include <CFOO> 8 different times and get a error? Its gonna be hard to start counting howmainy times you include ______! VS2012 got the #pragma once to avoid that but what happens if ya ain't got VS2012? Simple you make your own "#pragma once " like this :
1
2
3
4
5
#ifndef CFOO_HEADER_OF_AWSOMNESS
#define CFOO_HEADER_OF_AWSOMNESS
// Your CFOO_HEADER_OF_AWSOMNESS CODE

#endif 


Whats Your OS?

Did you murder someone lately? Yes! You say its allowed on mars? But you ain't on mars!...You want to run WinAPI on mac? Yes! You say its allowed on Windows? but you ain't using windows!...

Macros ARE VERY IMPORTANT to know what works and what doesn't....

Are you familiar with SFML? here is i piece of its code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#if defined(_WIN32)

    // Windows
    #define SFML_SYSTEM_WINDOWS
    #ifndef NOMINMAX
        #define NOMINMAX
    #endif

#elif defined(__APPLE__) && defined(__MACH__)

    // Apple platform, see which one it is
    #include "TargetConditionals.h"

    #if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR

        // iOS
        #define SFML_SYSTEM_IOS

    #elif TARGET_OS_MAC

        // MacOS
        #define SFML_SYSTEM_MACOS

    #else

        // Unsupported Apple system
        #error This Apple operating system is not supported by SFML library

    #endif

#elif defined(__unix__)

    // UNIX system, see which one it is
    #if defined(__ANDROID__)

        // Android
        #define SFML_SYSTEM_ANDROID

    #elif defined(__linux__)

         // Linux
        #define SFML_SYSTEM_LINUX

    #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)

        // FreeBSD
        #define SFML_SYSTEM_FREEBSD

    #else

        // Unsupported UNIX system
        #error This UNIX operating system is not supported by SFML library

    #endif

#else

    // Unsupported system
    #error This operating system is not supported by SFML library

#endif 


Acording to SFML when you got ________, C++ defines _________.
Windows - _WIN32
Apple - __APPLE__ and __MACH__
Apple IOS - TARGET_OS_IPHONE or TARGET_IPHONE_SIMULATOR
Apple MacOS - TARGET_OS_MAC
Unix - __unix__
Unix Android - __ANDROID__
Unix Linux - __linux__
Unix FreBSD - __FreeBSD__ or __FreeBSD_kernel__

Maxing stuff Shorter....
Do you seriously want to memorize this? :
std::numeric_limits<int>::max(); when you could just do this #define Infinity std::numeric_limits<int>::max(); and start placing
Infinity
everywhere
Last edited on
TheGentlmen wrote:
std::numeric_limits<int>::max(); when you could just do this #define Infinity

This is not a very good example of the usefulness of macros. If you really want to use Infinity instead of std::numeric_limits<int>::max(); it's better to use const.

 
const int Infinity = std::numeric_limits<int>::max();
Thank you all guys for your reply.

you above 1st and 2nd is really useful things we can do with Macro.

But I am agree with Peter87 on 3rd point.

const is always use full in such case.
Topic archived. No new replies allowed.