C Programming/Constants: Difference between revisions
Jump to navigation
Jump to search
(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...") |
|||
(2 intermediate revisions by the same user not shown) | |||
Line 8: | Line 8: | ||
#include <stdio.h> | #include <stdio.h> | ||
#define LENGTH 10 | #define LENGTH 10 | ||
#define WIDTH | #define WIDTH 5 | ||
#define NEWLINE '\n' | #define NEWLINE '\n' | ||
Line 16: | Line 16: | ||
int area; | int area; | ||
area = LENGTH * WIDTH; | area = LENGTH * WIDTH; | ||
printf(" | printf("Value of Area : %d", area); | ||
printf("%c", NEWLINE); | printf("%c", NEWLINE); | ||
return 0; | return 0; | ||
Line 29: | Line 29: | ||
int main() | int main() | ||
{ | { | ||
const int LENGTH = 10; | const int LENGTH = 10; | ||
const int WIDTH = 5; | const int WIDTH = 5; | ||
const char NEWLINE = '\n'; | const char NEWLINE = '\n'; | ||
int area; | int area; | ||
Line 37: | Line 37: | ||
printf("Value of Area : %d", area); | printf("Value of Area : %d", area); | ||
printf("%c", NEWLINE); | printf("%c", NEWLINE); | ||
return 0; | return 0; | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> |
Latest revision as of 05:02, 5 February 2018
There are two simple ways in C to define constants:
- Using
#define
preprocessor. - 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;
}