What is the differece between #define and constant in C?

Questions by byadwad   answers by byadwad

Showing Answers 1 - 10 of 10 Answers

a.s.gowri sankar

  • May 15th, 2006
 

A const identifier allows a variable to be constant throughout the file in which it's declared....

Whereas a #define is a macro which replaces the macro-name with a user-defined text statement.....

Also,a macro can be deleted,if we wish,using #undef whereas a const variable once defined can't be done so....

  Was this answer useful?  Yes

Bhagwat Bhakuni

  • May 19th, 2006
 

One More Difference is Thatconst variable may have block scope, function scope, and file scope but #define macro always have file scope.

  Was this answer useful?  Yes

Basavaraj

  • May 19th, 2006
 

Thanks a lot for info ............ U both cleared my doubt!!

  Was this answer useful?  Yes

vssunil

  • May 31st, 2006
 

#define is a macro and it is declared before the main()...

but a constant can only be declared after the main()...

but also as per the books constants can also be declared any where in the program...

this is my thoughts...

  Was this answer useful?  Yes

Satish

  • Jun 12th, 2006
 

Hi,

  just to add,

file scope:> should mean for all statements below the #define. This is because we can have a #define anywhere in a file, this would apply to all stmts below this definition.

Also, #defines are preprocessed.

const is a qualifier to a data-type, #defines are not.

  Was this answer useful?  Yes

ramnath

  • Jun 27th, 2006
 

hi

#define is a macro

it also used as constant.for example

#define ram 25 ( now ram = 25 )

#define swap(x,y) {x+=Y;y=x-y;x-=y}

while canstant is a constant assignment to variable of any data type.

  Was this answer useful?  Yes

Rasheed

  • Jul 6th, 2006
 

In C, #define and const are both used to define constants.

only the deferences are:

#define is used to define symbolic constants in programming, there is no data type associated with it.

const variables are like other variables whose value is always

constant through the entire program.

  Was this answer useful?  Yes

ARUNESHWAR

  • Jul 8th, 2006
 

#define is a symbolic constant(macro).

for eg. #define A 5

A++;(it will give error)

constant int a=5;(we cant modify the value of a).

  Was this answer useful?  Yes

shaanxxx

  • Aug 20th, 2006
 

macro will be part of the code. 'const' can behave as a macro or it may behave a normal variable(memory allocated in data segment). const behaviour is compiler depndent.

  Was this answer useful?  Yes

suresh

  • Aug 31st, 2006
 

Scope of both #define and constant will be differ.

        #define will be accessed by whole of the program.   but if u declare constant inside any of the function in the program that will be accessible only in that function itself.     That is the main difference must be i feel...

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions