Difference between const and #define(macros)
#define is processed by the preprocessor doing what amounts to simple text replacement and that value cannot be changed.
example
#define MY_CONST 42
Doesn't actually create a variable. It replaces the token MY_CONST across the file with the literal 42 at compile time.
const is the Qualifiers it is not possible to modify but using pointer we can modify .
Actually creates a variable like normal but complains if you try and change it. A const value can sometimes be altered using pointers, but defining constants using macros creates literals, and literals cannot be changed.
example
const int a=100;
difference between const and #define |
#include <stdio.h>
#include <stdlib.h>
#define b 10
int main()
{
const int a=100;
int *ptr;
printf("a is %d\n",a);
ptr =&a;
*ptr =200;
printf("b is %d\n",b);
printf("a is %d\n",a);
printf("value of a is %d\n",*ptr);
return 0;
}
Comments