Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

C Constant


May 11, 2021 C


Table of contents


C Constant

Constants are fixed values that do not change during program execution. These fixed values, also known as literal quantities.

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

Constants are like regular variables, except that the values of constants cannot be modified after they are defined.

Integer constant

An integer constant can be a heteth, octal, or heteen constant. The prefix specifies a base: 0x or 0X for hex, 0 for octal, and a hex by default without prefix.

An integer constant can also have a suffix, which is a combination of U and L, which represents an unsigned integer (unsigned), and L, which represents a long integer. The suffix can be capital, or it can be small, 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       /* 无符号长整数 */

Floating-point constant

Floating-point constants consist of integer parts, adget points, a small part, and an exponential part. You can use the form of a small number or an exponent to represent floating-point constants.

When expressed in the form of a number of points, you must include a number of points, an exponent, or both. W hen expressed exponentially, you must include an integer part, a small part, or both. The signed exponent is introduced with e or E.

Here are a few examples of floating-point constants:

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

The character constant

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

The character constant can be a normal character (such as 'x'), an escape sequence (such as ''t'), or a generic character (e.g. ''u02C0').

In C, there are specific characters that have a special meaning when they have backslashes in front of them, and are used to represent things like line breaks or tabs. The following table lists some of these escape sequence codes:

Escape sequence meaning
\\ \ Characters
\' 'Characters
\" "Character
\? ? Character
\a Alarm ringtone
\b Retractable
\f Change the page
\n Wrap
\r Bus stop
\t Horizontal tab
\v Vertical tab
\ooo One to three binary numbers
\xhh . . . One or more numbers of hexadecimal numbers

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 constant

String literal values or constants are enclosed in double quotes "". A string contains characters that resemble character constants: ordinary characters, escape sequences, and generic characters.

You can use spaces as separators to branch a long string constant.

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

"hello, dear"

"hello, \

dear"

"hello, " "d" "ear"

Define the constant

In C, there are two simple ways to define a constant:

  1. Use #define preprocessor.
  2. Use the const keyword.

#define preprocessor

Here's #define the preprocessor-defined constants using the #define of the using :

#define identifier value

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

value of area : 50

Const keyword

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

const type variable = value;

See the following 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 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 capital letters.