0

What is#define

hi

4th Feb 2017, 4:42 PM
Kadiwala Yash
Kadiwala Yash - avatar
3 Answers
+ 1
Whatever you define with #define will be replaced with its definition once the code compiles. Example: #define MAX 64 Now every time MAX appears in your code it will be replaced by the value 64. int a[MAX]; /* this turns into 'int a[64];' This is quite useful for C where you can't create arrays with variable length. However you can still use this in C++ if you want to*/ A even better use of define (and a more complex one) is macros. Let's start with a simple macro that will return the absolute value (turns the value positive) for a given variable: #define abs(a) ((a) < 0 ? -(a) : (a)) Usage: y = abs(x); /* this will set y to the absolute value of x */ Once again the compiler replaces abs(x) with ((x) < 0 ? -(x) : (x)) even before running the code. This is the big difference between macros and functions. You can use #define with anything you want, even names of other functions or variables. The precompiler will replace everything with the defined name with its definition.
4th Feb 2017, 7:05 PM
Robobrine
Robobrine - avatar
0
#define lets you define the language on your own, for example, #define print cout print << "Hello World!"; //This will work as you have defined cout with print.
4th Feb 2017, 5:50 PM
Dawzy
Dawzy - avatar
0
plz tell in other way
4th Feb 2017, 5:54 PM
Kadiwala Yash
Kadiwala Yash - avatar