C constant


Constant values ​​are fixed values ​​that do not change during program execution. These fixed values ​​are also called literals.

Constants can be any basic data type, such as integer constants, floating point constants, character constants, or string literals, as well as enumeration constants.

Constant is like a regular variable, except that the value of a constant cannot be modified after it is defined.

Integer constants

Integer constants can be decimal, octal or hexadecimal constants. The prefix specifies the base: 0x or 0X for hexadecimal, 0 for octal, and without a prefix the default is decimal.

Integer constants can also have a suffix. The suffix is ​​a combination of U and L. U represents an unsigned integer (unsigned) and L represents a long integer (long). The suffix can be uppercase or lowercase, and U and L can be in any order.

The following are examples of several integer constants:

212         /* 合法的 */215u        /* 合法的 */0xFeeL      /* 合法的 */078         /* 非法的:8 不是八进制的数字 */032UU       /* 非法的:不能重复后缀 */

The following are examples of various types of integer constants:

85         /* 十进制 */0213       /* 八进制 */0x4b       /* 十六进制 */30         /* 整数 */30u        /* 无符号整数 */30l        /* 长整数 */30ul       /* 无符号长整数 */

Floating point constants

Floating Point constants consist of an integer part, a decimal point, a fractional part, and an exponent part. You can represent floating-point constants in decimal or exponential form.

When expressed in decimal form, the decimal point, exponent, or both must be included. When expressed in exponential form, an integer part, a decimal part, or both must be included. Signed exponents are introduced with e or E.

The following are examples of floating point constants:

3.14159       /* 合法的 */314159E-5L    /* 合法的 */510E          /* 非法的:不完整的指数 */210f          /* 非法的:没有小数或指数 */.e55          /* 非法的:缺少整数或分数 */

Character constants

Character constants are enclosed in single quotes, for example, 'x' can be stored in ## In a simple variable of type #char.

Character constant can be an ordinary character (such as 'x'), an escape sequence (such as '\t'), or a universal character (such as '\u02C0').

In C, there are some specific characters. When they are preceded by a backslash, they have special meanings and are used to represent newlines (\n) or tabs (\t )wait. The following table lists some such escape sequence codes:

#Escape sequenceMeaning \\\ characters\'' characters\"" Characters\?? Characters\aAlarm ringtone\bBackspace key\fForm feed\nLine break\rEnter\tHorizontal tab##\v\ooo##\xhh . . .One or more digits in hexadecimal number

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 character constants: ordinary characters, escape sequences, and universal characters.

You can use spaces as delimiters to separate a long string constant into lines.

The following example shows some string constants. The strings displayed in the following three forms are the same.

"hello, dear""hello, \

dear""hello, " "d" "ear"

Define constants

In C, there are two simple ways to define constants:

  1. Use #define preprocessor.

  2. Use the const keyword.

#define preprocessor

The following is the form of using the #define preprocessor to define constants:

#define identifier value

Please see the following example for details:

#include <stdio.h>#define LENGTH 10   #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 Keywords

You can use the const prefix Declare a constant of the specified type as follows:

const type variable = value;

Please see the following example for details:

#include <stdio.h>int main(){   const int  LENGTH = 10;   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 will produce the following results:

value of area : 50

Please note that it is a good programming practice to define constants in uppercase letters.

Vertical tab
One to three digits in octal number