Benefits of using '#define' to declare a constant in C/C++

By: IncludeHelp, on 06 APR 2017

Learn: how to declare a constant using '#define' to make execution faster and save memory consumption?

By using #define (Macro definition) you can define a constant without consuming any amount of memory.

It helps to make our program more maintainable, because we have to just define a constant once and we can use it any number of times anywhere (Depending on the scope where it defined). If you want to make any change in the value of that particular macro you have to do it once only.

It also increases the execution speed of the program because Macros expand while compiling the program.

Let's consider the program here we are declaring a float constant using #define

#include <stdio.h>

#define GRAVITY 9.8f

int main()
{
	printf("Gravity: %f\n",GRAVITY);
	return 0;
}

Output

Gravity: 9.800000




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.