Constant in programming C
Constant refers to fixed values that the program cannot change during execution. Those fixed values are also called literals .
Constants can be any data type as data types: integers, real numbers, characters or strings . There are enumeration constants (enumeration).
A constant can be thought of as a variable often in addition to its value cannot be changed after being defined.
Integer constant in C
The integer constant value can be decimal (decimal), octal (octal) or hexadecimal (hexadecimal). The prefix (basic prefix) defines the base or radix: 0x or 0X for hexadecimal (system 16), 0 for octal (system 8), and nothing is decimal.
A constant value may have a suffix (combination) of U and L, for Unsigned and Long types . The ending may be uppercase or lowercase in any order.
Here is an example for the type of integer constant:
212 /* la hop le */ 215u /* la hop le */ 0xFeeL /* la hop le */ 078 /* Khong hop le: 8 khong la ky so trong he bat phan (octal) */ 032UU /* Khong hop le: ban khong the lap lai hau to (suffix) */
Here are other examples with a few ways to declare with integer types:
85 /* he thap phan */ 0213 /* he bat phan (octal) */ 0x4b /* he thap luc phan (hexadecimal) */ 30 /* int */ 30u /* unsigned int */ 30l /* long */ 30ul /* unsigned long */
Real constant in C
A floating point real constant has an integer part, a decimal value, a fractional part, and a exponent. You can represent floating point values in decimal and fractional types.
When representing values in decimal format, you must add integers, exponents, or both. The hat is written by e or E.
Here are a few examples for floating point sections:
3.14159 /* Hop le */ 314159E-5L /* Hop le */ 510E /* Khong hop le: phan mu chua hoan thien */ 210f /* Khong hop le: khong co phan decimal va phan mu */ . e55 /* Khong hop le: thieu phan phan so va phan nguyen */
Hang characters in C
The character part is closed and closed in single quotes ('), for example' x 'and can be stored in a simple variable of type char .
A character can be a lowercase letter (eg 'x') or escape string (eg, 't'), or a universal character (eg 'u02C0').
There are specific characters in C when starting with a sign will have special meaning and are used to represent new lines (n), new tabs (t). Here is a list of special characters:
Escape sequence Meaning Character 'Character' "Character"? Characters ? a Bell b Backspace f Form feed n New line r Carriage return tab horizontal v tab vertical ooo Number in base 8 of 1 to 3 digits xhh. . . Hexadecimal number of one or more digitsHere is an example to show some escape sequence characters:
#include int main () { printf ( "HoctLaptTrinhtCtTaitQTMnn" ); printf ( "===========================n" ); printf ( "QTM chuc cac ban hoc tot! n" ); return 0 ; }
Compile and run the above C program to see the results
Hang string in C
The string constant is enclosed with "" quotes. A string consists of characters similar to a character constant: pure characters, escape sequences and common characters .
You can divide long lines into lines using string values and separate them by spaces.
Here are a few examples with string constants. The following three strings have the same value:
"hello, vietnam" "hello, vietnam" "hello, " "v" "ietnam"
Constant definition in C
There are two simple ways in C to define constants:
- Use #define preprocessor .
- Using const keyword.
Use the #define preprocessor in C
Below is a sample to use the #define preprocessor to define a constant:
#define dinh_danh gia_tri
Here is a detailed example:
#include #define CHIEUDAI 15 #define CHIEURONG 12 #define NEWLINE 'n' int main () { int dientich ; dientich = CHIEUDAI * CHIEURONG ; printf ( "Dien tich hinh chu nhat la: %d" , dientich ); printf ( "%c" , NEWLINE ); printf ( "===========================n" ); printf ( "QTM chuc cac ban hoc tot! n" ); return 0 ; }
Compile and run the above C program to see the results
Use the const keyword in C
You can use the const prefix to declare constants with a specific type as follows:
const kieu_du_lieu ten_bien = gia_tri ;
Here is a detailed example:
#include int main () { const int CHIEUDAI = 15 ; const int CHIEURONG = 12 ; const char NEWLINE = 'n' ; int dientich ; dientich = CHIEUDAI * CHIEURONG ; printf ( "Dien tich hinh chu nhat la: %d" , dientich ); printf ( "%c" , NEWLINE ); printf ( "===========================n" ); printf ( "QTM chuc cac ban hoc tot! n" ); return 0 ; }
Compiling and running the above C program will produce the same results as above.
Note that in practice, we often name the word " CA" .
According to Tutorialspoint
Previous lesson: Turning in programming C
Next lesson: Class stored in C
You should read it
- Constants in PHP
- Hang (Constant / Literal) in C / C ++
- LITERAL in SQL Server
- Learn about ES6 in Javascript
- The function strpbrk () in C
- The strstr () function in C
- Regular Expression in C #
- String (String) in C / C ++
- float.h in C
- The strcspn () function in C
- The strspn () function in C
- History of discovery and application in mathematics of mysterious pi numbers