Home » Programming Tips & Tricks » C - Tips & Tricks

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

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.