Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between #define and creating a normal type?

In C/C++, what is the difference between using #define [and #ifndef #endif] to create values, when you can easily do it with an int or std::string [C++] too?

#ifndef MYVAL
#define MYVAL(500)
#endif

//C++

cout << MYVAL << endl;

//C

printf(MYVAL);

//C++

int MYVAL = 500;
cout << MYVAL << endl;

//C
int MYVAL = 500;
printf(MYVAL);
like image 201
Joe Avatar asked Nov 27 '22 13:11

Joe


1 Answers

Your assumptions are wrong. #define doesn't create "values", it creates replacement text in your source code. It has basically nothing to do with C or C++ at all.

like image 154
Kerrek SB Avatar answered Dec 07 '22 23:12

Kerrek SB