Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C Constants

C Constants

Constants are fixed values that do not change during program execution. These fixed values are also calledliterals.

A constant can be any basic data type, such as an integer constant, floating-point constant, character constant, or string literal; there are also enumeration constants.

ConstantThey are just like regular variables, except that the value of a constant cannot be modified after it is defined.

Constants can be used directly in code or by defining them.

Integer Constants

An integer constant can be decimal, octal, or hexadecimal constants. A prefix specifies the base: 0x or 0X means hexadecimal, 0 means octal, and no prefix means decimal by default.

An integer constant may also carry a suffix that is a combination of U and L, where U means unsigned integer and L means long integer. The suffix may be uppercase or lowercase, and the order of U and L is arbitrary.

Here are a few examples of 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       /* 无符号长整数 */

Integer constants can have a suffix indicating the data type, for example:

Example

int

myInt

=

10

;

long

myLong

=

100000L

;

unsigned

int

myUnsignedInt

=

10U

;

Floating-point Constants

A floating-point constant consists of an integer part, a decimal point, a fractional part, and an exponent part. You can represent a floating-point constant using either a decimal form or an exponent form.

When using the decimal form, you must include the integer part, the fractional part, or both. When using the exponent form, you must include the decimal point, the exponent, or both. A signed exponent is introduced by e or E.

Here are a few examples of floating-point constants:



3.14159       /* 合法的 */

314159E-5L    /* 合法的 */

510E          /* 非法的:不完整的指数 */

210f          /* 非法的:没有小数或指数 */

.e55          /* 非法的:缺少整数或分数 */

Floating-point constants can have a suffix indicating the data type, for example:

Example

float

myFloat

=

3.14f

;

double

myDouble

=

3.14159

;

Character Constants

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

A character constant can be an ordinary character (e.g., 'x'), an escape sequence (e.g., '\t'), or a universal character (e.g., '\u02C0').

In C, certain characters have special meanings when preceded by a backslash and are used to represent things such as the newline (\n) or tab (\t). The following table lists some of these escape sequences:

Escape sequences Meaning
\ \ character
' ' character
" " character
? ? character
\a Alert (bell)
\b Backspace
\f Form feed
\n Newline
\r Carriage return
\t Horizontal tab
\v Vertical tab
\ooo One to three octal digits
\xhh . . . One or more hexadecimal digits

The example below shows some escape sequence characters:

Example

#include

<

stdio.h

int

main

(

)

{

printf

(

"

Hello

\

tWorld

\

n

\

n

"

)

;

return

0

;

}

When the code above is compiled and executed, it produces the following result:



Hello   World



The ASCII value of a character constant can be converted to an integer value through a cast.

Example

char

myChar

=

'a'

;

int

myAsciiValue

=

(

int

)

myChar

;

// Convert myChar to the ASCII value 97

String Constants

A string literal or constant is enclosed in double quotes " ". A string contains characters similar to character constants: ordinary characters, escape sequences, and universal characters.

You can use spaces as separators to split a very long string constant across multiple lines.

The following examples show some string constants. The strings shown in the following three forms are identical.



"hello, dear"



"hello, \



dear"



"hello, " "d" "ear"

A string constant ends with the null terminator \0 in memory. For example:

char myString[] = "Hello, world!"; //系统对字符串常量自动加一个 '\0'

Defining Constants

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

  1. Use #define Preprocessor: #define can define a constant in a program, which is replaced by its corresponding value at compile time.
  2. Use const Keyword: the const keyword is used to declare a read-only variable, that is, a variable whose value cannot be modified at runtime.

#define Preprocessor

The following is the form for defining constants using the #define preprocessor:



#define ConstantName ConstantValue

The following code defines a constant named PI:



#define PI 3.14159

When this constant is used in the program, the compiler replaces all occurrences of PI with 3.14159.

See the example below:

Example

#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 code above is compiled and executed, it produces the following result:



value of area : 50

const Keyword

You can use the const prefix to declare a constant of a specified type, as shown below:



const 数据类型 常量名 = 常量值;

The following code defines a constant named MAX_VALUE:



const int MAX_VALUE = 100;

When this constant is used in the program, its value will always be 100 and cannot be modified.

A const declaration of a constant must be completed in a single statement:

See the example below:

Example

#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 code above is compiled and executed, it produces the following result:



value of area : 50

Please note that defining constants in uppercase letters is a good programming practice.

Difference Between #define and const

Both #define and const can be used to define constants; which one to choose depends on specific needs and programming habits. Generally, it is recommended to use the const keyword to define constants because it has the advantages of type checking and scope, while #define only performs simple text replacement and may cause unexpected problems.

The #define preprocessor directive and the const keyword have some differences when defining constants: