C language programming - constants

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

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

Constants are like regular variables, except that the value of a constant cannot be changed after it is defined.

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

integer constant

Integer constants can be decimal, octal, or hexadecimal constants. The prefix specifies the radix: 0x or 0X means hexadecimal, 0 means octal, and no prefix means decimal by default.

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

Here are a few examples of integer constants:

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

The following are examples of integer constants of various types:

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

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

int myInt = 10;
long myLong = 100000L;
unsigned int myUnsignedInt = 10U;

floating point constant

A floating-point constant consists 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, it must contain an integer part, a fractional part, or both. When using exponential form, you must include a decimal point, exponent, or both. Signed exponents are introduced with e or E.

Here are a few examples of floating-point constants:

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

Floating-point constants can be suffixed to indicate the data type, for example:

float myFloat = 3.14f;
double myDouble = 3.14159;

character constant

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

A 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 certain characters that have special meaning when they are preceded by a backslash, and are used to represent such as newline (\n) or tab (\t). The following table lists some such escape sequence codes:

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 result:

Hello   World

ASCII values ​​of character constants can be converted to integer values ​​by casting.

char myChar = 'a';
int myAsciiValue = (int) myChar; // 将 myChar 转换为 ASCII 值 97

string constant

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 break a long string constant into new lines.

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

"hello, dear"

"hello, \

dear"

"hello, " "d" "ear"

String constants are terminated in memory with the null terminator \0. For example:

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

define constant

In C, there are two simple ways of defining constants:

  1. Use a #define preprocessor.

  2. Use the const keyword.

#define preprocessor

Here's how to define a constant using the #define preprocessor:

#define identifier value

Please see the following examples 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 result:

value of area : 50

const keyword

You can declare constants of a specified type using the const prefix, as follows:

const type variable = value;

const Declaring a constant is done in one statement:

Please see the following examples 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 produces the following result:

value of area : 50

Note that it is good programming practice to define constants in uppercase.

define Pay attention to the "edge effect", for example: #define N 2+3, the value of N is 5.

double a;
a = (float)N/(float)2;

When compiling, we expected a=2.5 , but the actual printing result is 3.5 . The reason is that in the preprocessing stage, the compiler processes a=N/2 into a=2+3/2. This is the edge effect of the define macro, so we should Write #define N (2+3).

#include <stdio.h>

#define N 2+3
//正确写法 #define N (2+3)

int main(){   
    double a ;
    a = (float)N/(float)2;
    printf("a 的值为 : %.2f", a);   
  
    return 0;
}

以下是一个求矩形面积的例子:

#include <stdio.h>

#define LENGTH 10+10
//正确写法 #define LENGTH (10+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;
}

The output of the above example is:

value of area : 60

So if we need to get the correct result, we should change #define LENGTH 10+10 to #define LENGTH (10+10).

Guess you like

Origin blog.csdn.net/shiwei0813/article/details/131135632