C programming Constant
In this chapter you will learn about the various types of C programming constant and Constant.Constant and literal
Constant is a value or identifier. The value of the program is not changed. For example, "SATT Academy", 1, 2 etc.
Then we saw that the identifier can also be defended as a constant.
const double PI = 3.14
Here PI is a constant which means PI and 3.14 are the same in this program.
Different types of constants used in C programming and their descriptions are given below
Character Constant
The character constant is a single character special constant that is surrounded by a single quote (''). For example, 'a', 'm', 't', 's', etc.
Integer Constant
Integer's Bengali meaning integer.
The integer constant is the decimal and the numerical integer without the numerical integer constants or constants. There are three kinds of integer constants in C programming
Decimal constant - decimal constant (10 based constant)
Octal constant- octal constant (8 based constants)
Hexadecimal Constant-hexadecimal constant (16-based Constant)
For example:
8 based Constant 070, 055, 099 etc.
10 based Constant: 0, 20, 100 etc.
16 based Constant: 0x7f, 0x2a, 0x521 etc.
The octal constant in C programming starts with 0 (zero) and hexadecimal constant starts with 0x.
Floating Point Constant
The decimal meaning of the floating point is the decimal.
The floating point constant is the decimal and the mathematical constant with the index part.
10.05
0.03487
-0.15E-4
Note: E-4 = 10-4
Spawn Sequence
Sometimes there are some characters that can not be typed or carry special meaning in C programming. Such as backspace, tab, backslash etc. To use these, the escape sequence is to be used.
For example: \ n is used for the newline. \ B is used for Backspace.
Spawn Sequence
The name of the skype sequence
\ b Backspace
\ f Form feed
\ n Newline
\ r Return
\ t Horizontal tab
\ v Vertical tab
\\ Backslash
\ 'Single quotation mark
\ "Double quotation mark
\? Question mark
\ 0 Null character
String Constant
String constant is a constant that is surrounded by double quotes (""). For example:
"" // null String Constant
"SATT" // String Constant
"" String Constant with // 5 empty spaces
String constant with "t" // single character
"Love is heaven \ n" // print a string with a new line
Inmiriation Constant
Enumeration type is defined with the enum keyword. For example:
enum color {teal, green, orange, white};
Here the color is a variable and purple, green, black and white are the enumeration constants, whose values are 0, 1, 2 and 3 respectively. Visit our C Inummission section to know more about the insurrection.
Define Constant in C program
There are two common methods to define Constant in C program.
Using const keyword
#define uses a proxy processor
const keyword
To declare a specific type of constant, you can use the const keyword as a prefix.
Syntax
const type variable = value;
For example, the following example shows the use of const keyword:
#include <stdio.h>
int main () {
const char NEWLINE = '\ n';
const int LENGTH = 5;
const int WIDTH = 4;
int area;
area = LENGTH * WIDTH;
printf ("Total area:% d", area);
printf ("% c", NEWLINE);
return 0;
}
When the above program is compiled and executed it will show output as below
Output
Total area: 20
#define preprocessor
You can define the constants using the #define preprocessor.
Syntax
#define identifier value
For example, in the example below, the use of #define preprocessor is shown
#include <stdio.h>
#defineNEWLINE \ n '
#define LENGTH 5
#define WIDTH 4
int main () {
int area;
area = LENGTH * WIDTH;
printf ("Total area:% d", area);
printf ("% c", NEWLINE);
return 0;
}
When the above program is compiled and executed it will show output as below
Output
Total area: 20

0 Comments