No products in the cart.

Difference between #define and const???

CBSE, JEE, NEET, CUET

CBSE, JEE, NEET, CUET

Question Bank, Mock Tests, Exam Papers

NCERT Solutions, Sample Papers, Notes, Videos

Difference between #define and const???
  • 1 answers

Sia ? 3 years, 5 months ago

Const is a declaration of constant, compiler will do appropriate checks during the compile time
#define is a macro compiler will see a value.
Speaking in terms of memory const declaration could be more efficient:

const short x =3D 1;=20
// this will allocate 16 bits for x (actually it depends on the machine, I assume unix platform with 32 bits integers)

#define x (short)1=20
// this will probably allocate 32 bits to hold the constant 1.

Use const instead of #define mainly because of compiler checks. And inline functions instead of macros as well. Preprocessor is the obsolete remnant from early C days, there’s no use of it in the oop environment.

http://mycbseguide.com/examin8/

Related Questions

What are called tokens
  • 2 answers
What is mean by buffering in file handling?
  • 1 answers

myCBSEguide App

myCBSEguide

Trusted by 1 Crore+ Students

Test Generator

Test Generator

Create papers online. It's FREE.

CUET Mock Tests

CUET Mock Tests

75,000+ questions to practice only on myCBSEguide app

Download myCBSEguide App