constant vs. define

See the following codes to understand constant vs. define.

const type variable #define type variable
It has a specific data type such as int, float, char

#include<stdio.h>
int main()
{
     const float pi=3.1416;
     printf("pi = %f",pi);
     return 0;
}

OUTPUT

pi = 3.141600

All #define type data are auto adjusted.

#include<stdio.h>
#define pi 3.1416
#define msg "value of pi"
int main()
{
     printf("%s = %f",msg,pi);
     return 0;
}

OUTPUT

value of pi = 3.141600


Next Previous