The constants refer to fixed values that cannot be changed during execution of the program. These fixed values are also called literals.
C Constants |
Numeric Constants
Numeric constants consist of numeric digits, they may or may not have decimal point(.). These are the rules for defining numeric constants:
- Numeric constant should have at least one digit.
- No comma or space is allowed within the numeric constant.
- Numeric constants can either be positive or negative but default sign is always positive.
Integer Constants
There are three types of integer constants based on different number systems(decimal, octal, hexadecimal). The permissible characters that can be used in these constants are:
- Decimal constants – 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 (base 10)
- Octal constants – 0, 1, 2, 3, 4, 5, 6, 7 (base 8)
- Hexadecimal constants – 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F, a, b, c, d, e, f ( base 16)
Some valid decimal integer constants are:
0
132
3745
24789
Some invalid decimal integer constants are:
Invalid | Remarks |
---|---|
3.7 | illegal character(.) |
4#7 | illegal character (#) |
78 5 | No blank space allowed |
0827 | First digit cannot be zero |
7,324 | Comma is not allowed |
In octal integer constants, first digit must be 0. Some examples are:
0
05
077
0324
In hexadecimal integer constants, first two characters should be
0x
or 0X
. Some examples are:
0X23
0x515
0XA15B
0xFFF
0xac
By default the type of an integer constant is
int
. But if the value of integer constant exceeds the range of values represented by int
type, the type is taken to be unsigned int
or long int
. We can also explicitly mention the type of the constant by suffixing it with l
or L
(for long), u
or U
( for unsigned), ul
or UL
( for unsigned long).For example:
7453 Integer constant of type int
55138722UL or 55138722ul Integer constant of type unsigned long int
6675U or 6675u Integer constant of type unsigned int
Real(floating point) constants
Floating point constants are numeric constants that contain decimal point. Some valid floating point constants are:
0.7
633000.0
0.0092
3497.79
For expressing very large or very small real constants, exponential (scientific) form is used. Here the number is written in the mantissa and exponent form, which are separated by 'e' or 'E'. The mantissa can be an integer or a real number, while the exponent can be only an integer (positive or negative). For example the number
1500000
can be written as 1.5e6
, here 1.5
is mantissa and 6
is the exponent. Some more examples are as:Number | Exponential form |
---|---|
3500000000 | 3.5e9 |
0.0000086 | 8.6e-6 |
-370000 | -3.7E5 |
By default the type of a floating point constant is
double
. We can explicitly mention the type of constant by suffixing it with a f
or F
(for float
type), l
or L
(for long double
). For example:
2.3e5 floating point constant of type double
2.4e-9l or 2.4e-9L floating point constant of type long double
3.52f or 3.52F floating point constant of type float
Character Constants
A character constant is a single character that is enclosed within single quotes. Some valid character constants are:
'9' 'D' '$' ' ' '#'
Some invalid character constants are:
Invalid | Remark |
---|---|
'five' | There should be only one character within quotes |
"g" | Double quotes are not allowed |
' ' | No character between single quotes |
N | Single quotes missing |
Every character constant has a unique integer value associated with it. This integer is the numeric value of the character in the machine’s character code. If the machine is using ASCII (American Standard Code for Information Interchange), then the character 'G' represents integer value 71 and the character '5' represents value 53. Some ASCII values are:
A-Z ASCII value (65-90)
a-z ASCII value (97-122)
0-9 ASCII value (48-57)
; ASCII value (59)
There are certain characters in C when they are proceeded by a back slash they will have special meaning and they are used to represent like newline (\n) or tab (\t).
List of Escape Sequences
Escape sequence | Meaning |
---|---|
\\ | \ character |
\' | ' character |
\" | " character |
\? | ? character |
\a | Alert or bell |
\b | Backspace |
\f | Form feed |
\n | Newline |
\r | Carriage return |
\t | Horizontal tab |
\v | Vertical tab |
\ooo | octal number |
\xhh | hexadecimal number |
\0 | Null |
Following is the example to show few escape sequence characters:
#include <stdio.h>
int main()
{
printf("Hello\tWorld\n\n\n");
return 0;
}
Output :
Hello World
String Constants
A string constant is enclosed within double quotes (" "). At the end of string, the null character '\0' is automatically placed by the compiler. Some examples of string constants are:
"Manish"
"673"
"9"
" "
"A"
Note that "A" and 'A' are different, the first one is a string constant which consists of character A and '\0' while the second one is a character constant which represents integer value 65(in ASCII).
Symbolic Constants
If we want to use a constant several times, we can provide it a name. For example if we have to use the constant
3.14159265
at many places in our program, we can give it a name PI
and use this name instead of writing the constant value everywhere. These types of constants are called symbolic constants or named constants.
A symbolic constant is a name that substitutes for a sequence of characters. The characters may represent a numeric constant, a character constant or a string constant.
There are two simple ways in C to define symbolic constants:
- Using
#define
preprocessor
- Using
const
keyword
The #define
Preprocessor
Following is the form to use
#define
preprocessor to define a constant:
#define name value
Here name is the symbolic name for the constant, and generally written in uppercase letters. value can be numeric, character or string constant.
Some examples of symbolic constants are:
#define MAX 200
#define PI 3.14159625
#define CH 'y'
#define NAME "Manish"
In the program, these names will be replaced by the corresponding values. These symbolic constants improve the readability and modifiability of the program.
Following example explains it in detail:
#include <stdio.h>
#define NUMA 10
#define NUMB 20
#define NEWLINE '\n'
int main()
{
int result;
result = NUMA + NUMB;
printf("Sum of two numbers are %d", result);
printf("%c",NEWLINE);
return 0;
}
Output:
Sum of two numbers are 30
The const
Keyword
You can use
const
prefix to declare constants with a specific type as follows:
const type variable = value;
Following example explains it in detail:
#include <stdio.h>
int main()
{
const int NUMA = 10;
const int NUMB = 20;
const char NEWLINE = '\n';
int result;
result = NUMA + NUMB;
printf("Sum of two numbers are %d", result);
printf("%c", NEWLINE);
return 0;
}
Output:
Sum of two numbers are 30
No comments:
Post a Comment