Constants in C are the
fixed values that are used in a program, and its value remains the same during
the entire execution of the program.
Example: 9,3.14,'T'
etc.
Points to remember:
· Constants can be of any
of the basic data types like an integer constant, a floating constant,
a character constant, or a string literal. There are enumeration constants
as well.
· Constants are also
called literals.
· It is considered best
practice to define constants using only upper-case names.
How to define constants?
In C program we can define constants in two ways as shown below:
In C program we can define constants in two ways as shown below:
- Using #define preprocessor directive
- Using const keyword
1).Using #define preprocessor directive
This
directive used to declare an alias name for existing variable or any value. We
can use this to declare a constant as shown below:
Syntax:
#define
IDENTIFIERNAME value
where IDENTIFIERNAME is
the name given to constant and value is any value assigned to it.
Below is the C program
to explain how to use #define to declare constants:
#include<stdio.h>
#define PI 3.14
int main(){
int radius;
float area;
//Ask user for radius of the cicle
printf("\nEnter radius of circle:");
scanf("%d",&radius);
//calculate areaof the circle
area=PI*radius*radius;
printf("\nArea of the circle having radius %d is
%f.\n",radius,area);
return 0;
}
Output:
2).Using const keyword
Using const keyword to define constants is as simple as
defining variables, the difference is you will have to precede the definition
with a const keyword.
Syntax:
const
dataType IDENTIFIERNAME =value;
or
dataType
const IDENTIFIERNAME =value;
Example:
const float rate=12.5;
Below
program shows how to use const to declare constants of different data types:
#include<stdio.h>
int main(){
const
float PI=3.14;
int
radius;
float
area;
//Ask
user for radius of the cicle
printf("\nEnter
radius of circle:");
scanf("%d",&radius);
//calculate
areaof the circle
area=2*PI*radius;
printf("\nArea
of the circle having radius %d is
%f.\n",radius,area);
return
0;
}
|
Output:
Types of constants
1).Integer
Constants
An
integer is a numeric constant (associated with number) without any
fractional or exponential part. There are three types of integer constants in C
programming:
·
decimal constant(base 10)
·
octal constant(base 8)
·
hexadecimal constant(base 16)
2. Floating-point constants
A floating
point constant is a numeric constant that has either a fractional form or an
exponent form.
3. Character constants
A
character constant is created by enclosing a single character inside single
quotation marks.Character constants have a specific set of integer values known
as ASCII values (American Standard Code for Information Interchange).
Example: '9',
'T', ';'
4. String Literals
A string
literal is a sequence of characters enclosed in double-quote marks. For
example:
Example: "Techabaya",
"2019".
Please comment if you find anything
incorrect, or you want to improve the topic discussed above..
No comments:
Post a Comment