C Constants and Define Preprocessor – POFTUT

C Constants and Define Preprocessor


[rps-include post=6557]

Up to now we have worked with variables. As its names states variables values may change in the flow of application. Variables changes by adding, removing, subtracting etc. This may be ideal solution for most of time but how can we avoid it if we need constant values. Because if we want to define Pi number and do not want to change it as Pi is fixed number in mathematics. Here the solution is making them explicitly constant.

Constant

To make variables constant const variable is used. By adding const keyword before variable type the variable becomes a constant and can not be changed.

const float PI=3.14;

Not just numbers also other variable types can be made constant

String Constant

Here string poftut.com is string constant and can not be change during execution of the application.

const char site[]="poftut.com"

#define

Define preprocessor provides another way to define constant. This way definition do not need a variable type. #define simple puts value all occurrences of identifier

#define IDENTIFIER VALUE

Here is an example usage of #define

#include <stdio.h> 
#define PI 3.14 
 
int main(){ 
 
        printf("PI"); 
 
        return 0; 
}

[rps-include post=6557]

LEARN MORE  strlen() Function In C and C++ Tutorial with Examples

Leave a Comment