Defining Constants

 Defining Constants



There are two ways to define constant in C programming

                - Using the const key word

                                         -      const data_type constant_name = constant_value;

                                         -      const float PI = 3.1412;


Example - 

#include <stdio.h>

int main ()

{

    const float PI = 3.1412;

    float radius;

    printf ("Enter the radius in mm : \n");

    scanf ("%f",&radius);

    printf("The perimeter is : %.2f", 2*PI*radius);

return 0;

}

                - Using the #define directive.

                - #define come before the program main block.

                                            -       #define constant_name constant_value

                                            -      #define PI 3.1412

Example -

#include <stdio.h>

#define PI 3.1412

int main ()

{

    float radius;

    printf ("Enter the radius in mm : \n");

    scanf ("%f",&radius);

    printf("The perimeter is : %.2f", 2*PI*radius);

    return 0;

}

Post a Comment

1 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.