C Constants
Constants are fixed values that do not change during program execution. These fixed values, also known as literal quantities.
Constants can be any of the basic data types, such as integer constants, floating-point constants, character constants, or string literals, and enumerated constants.
Constants are like regular variables, except that the values of constants cannot be modified after they are defined.
Integer constants
Integer constants can be decimal, octal, or hexadecimal constants. prefix specifies cardinality: 0x or 0X is hexadecimal, 0 is octal, and decimal is by default without a prefix.
Integer constants can also take a suffix, which is a combination of u and L, u represents an unsigned integer (unsigned), and L represents a long integer. Suffixes can be uppercase or lowercase, and the order of U and L is arbitrary.
Examples of several integer constants are listed below:
- 212/* Legal * *
- 215u/* Legal * *
- 0xFeeL/* Legal * *
- 078/* Illegal: 8 is not a octal number * *
- 032UU/* Illegal: cannot repeat suffix * *
The following are instances of various types of integer constants:
- 85/* Decimal * *
- 0213/* Octal * *
- 0X4B/* Hex *
- 30/* Integer * *
- 30u/* unsigned integer * *
- 30l/* Long integer * *
- 30ul/* unsigned long integer * *
Floating-point constants
A floating-point constant consists of an integer part, a decimal point, a fractional part, and an exponent. You can use decimal form or exponential form to represent floating-point constants.
When you use a decimal representation, you must include a decimal point, an exponent, or both. When used in exponential notation, you must include either an integer part, a decimal part, or both. The signed exponent is introduced by E or E.
Several instances of floating-point constants are listed below:
- 3.14159/* Legal * *
- 314159E-5L/* Legal * *
- 510E/* Illegal: Incomplete index * *
- 210f/* Illegal: No decimal or Index * *
- . E55/* Illegal: missing integer or Score * *
Word Constants Quantity
The amount of constants is enclosed in single quotes, for example, ' x ' can be stored in a simple variable of type char.
The constants amount can be an ordinary character (such as ' X '), an escape sequence (such as ' \ t '), or a generic character (such as ' \u02c0 ').
In C, there are certain characters that, when preceded by a backslash, have special meanings that are used to denote a newline character (\ n) or a tab character (\ t). The following table lists some of these escape sequence codes:
Escape sequence |
meaning |
\\ |
\ character |
\' |
' Character |
\" |
"Character |
\? |
? Character |
\a |
Alarm Ring |
\b |
Backspace key |
\f |
Page breaks |
\ n |
Line feed |
\ r |
Enter |
\ t |
Horizontal tab |
\v |
Vertical tab |
\ooo |
Number of octal in one to three digits |
\xhh ... |
Hexadecimal number of one or more digits |
The following example shows some escape sequence characters:
#include <stdio.h>
int main ()
{
printf ("hello\tworld\n\n");
return 0;
}
When the above code is compiled and executed, it produces the following results:
Hello World
String constants
String literals or constants are enclosed in double quotes "". A string contains characters similar to the character constants: Ordinary characters, escape sequences, and generic characters.
You can use a space as a separator to branch a long string constant.
The following example shows some string constants. The following three forms show the same string.
"Hello, dear" "Hello,
\
Dear"
"Hello," "D" "ear"
Defining constants
In C, there are two simple ways to define constants:
1. Use the #define preprocessor.
2. Use the const keyword.
#define Preprocessor
The following is the form of defining constants using the #define preprocessor:
#define Identifier Value
Take a look at the following example:
#include <stdio.h>
#define LENGTH
#define WIDTH 5
#define newline ' \ n '
int main ()
{
int area;
Area = LENGTH * WIDTH;
printf ("Value of area:%d", area);
printf ("%c", newline);
return 0;
}
When the above code is compiled and executed, it produces the following results:
Value of AREA:50
const keyword
You can declare constants of the specified type using the const prefix, as follows:
Const type variable = value;
Take a look at the following example:
#include <stdio.h>
int main ()
{
const int LENGTH = ten;
const int WIDTH = 5;
const char newline = ' \ n ';
int area;
Area = LENGTH * WIDTH;
printf ("Value of area:%d", area);
printf ("%c", newline);
return 0;
}
When the above code is compiled and executed, it produces the following results:
Value of AREA:50
Note that it is a good programming practice to define constants as uppercase letters.
The above is the C language constant data collation, follow-up continue to supplement the relevant information, thank you for your support of this site!