What is constant in C programming? In the previous
tutorial on C programming, we learned what is Variable? Today in this tutorial,
we will learn what is constant in C
programming? What is constant in
C?
What
is constant in C programming?
Constant
is also a normal variable but its value does
not change during the execution of the program. that refers to constant fixed value in programming which means once
the execution of the program is defined. Its value cannot be changed during
Fixed value is called literal.
If a constant 's value
is tried to change during the execution
of the program, a compilation error is
found.
This
question definitely arises in the mind of almost every new programmer, why
should we use constant instead of
using simple variable? As I said above, constant is same as simple variable but its value does
not change during execution of program.
We
use Constant when we have to fix
a value whose value never changes such that the value of pie is 3.14 which is
always fixed.
C
programming is
the base of all programming language, so it is also called mother
language. When we create a program on the basis of class and object in
programming, we can easily use it in all the functions by defining constant in one place, whereas the object or this
keyword has to be used to use the variable in another function. Which is a
slight alter, so we use constant .
Constant 's value does not
change during the execution of the program, so constant does not need to be reloaded in memory
repeatedly.
types of C Constant
- Integer Constant
- Floating point constant
- String constant
- Character constant
Integer constant
Integer
constant has values stores of integer
type such as 5, 7, 8. Integer constant can contain decimal, octal and Hexadecimal
literals. Prefix specifies base and radix. Such as 0x and 0X represent
Hexadecimal, 0 Octal and if any prefix character is not preceded by any number,
it represents decimal.
The
suffix (end) of Integer literals can also have U or L in it, where U represents
unsigned while L represents long. Suffix (U or L) can be upper case and lower
case in any case.
Some
examples of different types of integer literals
345 /** Decimal **/
0520 /** Octal **/
0x365a /** Hexadecimal
**/
604 /** Int **/
604u /** unsigned int **/
42l /** long**/
42ul /** unsigned long
**/
Floating point constant
Floating
point constant consists of value
stores of floating type, meaning value stores of decimal type. Floating point
literals have decimal part, integer part, fractional part and exponent part.
You can represent floating point literals in decimal part and exponential part.
When
you represent a decimal form, it must be a decimal part or exponent part or
both. And when representing the exponential form, it must be either integer
part or fractional part or both. Signed exponent is introduced by e and e.
Some examples of
floating point literals:
3.1414 /** legal **/
314159E-5L /**legal**/
403E /* Illegal: incomplete exponent */
220f /* Illegal: no decimal or exponent */
.e75 /* Illegal: missing integer or
fraction */
Character Constant
Character
constant stores a single
character. Character literals are enclosed in single quotes such as 'a', 'y'.
Character literals can be plain character (e.g., 'a'), escape sequence (e.g.,
'\ n') or universal character (e.g., '\ u02C0').
There
are many characters in C programming whose prefixed backslash (\)
creates their special meaning like \ n = new line, \ t = tab,
Here
are the list of escape sequence.
Escape Sequence
|
Meaning
|
\\
|
\
character
|
\'
|
'
character
|
\"
|
"
character
|
\a
|
Alert
or bell
|
\b
|
Backspace
|
\f
|
Form
feed
|
\n
|
Newline
|
\r
|
Carriage
return
|
\t
|
Horizontal
tab
|
\v
|
Vertical
tab
|
\?
|
?
character
|
\ooo
|
Octal
number of one to three digits
|
\xhh...
|
Hexadecimal
number of one or more digits
|
String constant
String constant has value stores of string type. String
literals (string type values) are enclosed in double quotes. String literals
can have one or more character combinations. It also has plain characters,
escape sequences and universal characters store as character constant .
The null character (\
0) at the end of the string is automatically created by the compiler.
const char a[4] =
"wait";
define constant in C?
In C programming, constant can be defined in two ways.
• #define using preprocessor directive
• using const keyword
Let us now learn how to define constant
in the program using both methods.
# how to define constant via define preprocessor directive?
The source code is processed by a
program called preprocessor before the C program compile in the compiler. Here
we can define constant using
#define.
Syntax:
#define identifierName
value
Here
identifierName means meaningful constant name and value means that value which will be
assigned to that constant .
The below
given program explains how to define constant in C programming using #define
preprocessor.
#include
#define FIRSTNUMBER 5420
#define SECONDNUMBER
430.2
#define CHARVALUE 'A'
#define STRVALUE
"Hello"
int main()
{
printf("Integer Constant :
%d\n",FIRSTNUMBER);
printf("Floating point Constant :
%f\n",SECONDNUMBER);
printf("Character Constant :
%c\n",CHARVALUE);
printf("String Constant :
%s\n",STRVALUE);
return 0;
}
Output
of the above program
Integer Constant :
5420
Floating point Constant
: 430.200000
Character Constant :
A
String Constant :
Hello
How
to define constant in C
programming using const keyword?
The
const keyword is a pre-defined word that is reserved for the system. If const
keyword is written before any simple variable, then that variable becomes constant
. like :
Syntax:-
const
datatype variableName value;
defining
constant using const keyword
In the
given program, the process of constant defining using const keyword has been
described.
#include
int main()
{
const int FIRSTNUMBER = 5420;
const float SECONDNUMBER = 430.2;
const char CHARVALUE = 'A';
const char STRVALUE[5] = "Hello";
printf("Integer Constant :
%d\n",FIRSTNUMBER);
printf("Floating point Constant :
%f\n",SECONDNUMBER);
printf("Character Constant :
%c\n",CHARVALUE);
printf("String Constant :
%s\n",STRVALUE);
return 0;
}
Output
of the above program
Integer Constant :
5420
Floating point Constant
: 430.200012
Character Constant :
A
String Constant :
Hello
It is a good programming practice that always write constant
name in capital letter.
Post a Comment
Please do not any spam link in the comment