C Programming/Constants

From Chorke Wiki
Revision as of 05:00, 5 February 2018 by Shahed (talk | contribs) (Created page with "There are two simple ways in '''C''' to define constants: # Using <code>#define</code> preprocessor. # Using <code>const</code> keyword. == The #define Preprocessor == <synta...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

There are two simple ways in C to define constants:

  1. Using #define preprocessor.
  2. Using const keyword.

The #define Preprocessor

//main.c
#include <stdio.h>

#define LENGTH 10
#define WIDTH  5
#define NEWLINE '\n'

int main()
{
   int area;
   area = LENGTH * WIDTH;
   printf("value of area : %d", area);
   printf("%c", NEWLINE);
   return 0;
}

The const Keyword

//main.c
#include <stdio.h>

int main()
{
   const int  LENGTH = 10;
   const int  WIDTH = 5;
   const char NEWLINE = '\n';
   int area;

   area = LENGTH * WIDTH;
   printf("Value of Area : %d", area);
   printf("%c", NEWLINE);

   return 0;
}