非常教程

C参考手册

C 语法

integer constant

允许直接在表达式中使用整数类型的值。

句法

整型常量是表单的非左值表达式。

decimal-constant integer-suffix(optional)

(1)

octal-constant integer-suffix(optional)

(2)

hex-constant integer-suffix(optional)

(3)

其中

  • 十进制常数是一个非零十进制数(123456789),随后的零个或多个十进制数字(0123456789
  • 八进制常数是数字零(0),接着是零个或更多八进制数字(01234567
  • 十六进制常数是字符序列0x或字符序列0X后跟一个或多个十六进制数字(0123456789aAbBcCdDeEfF
  • 整数后缀(如果提供)可能包含以下一项或两项(如果同时提供了这两项),它们可能以任何顺序出现:
    • 无符号后缀(字符u或字符U
    • 长后缀(字符l或字符L)或长后缀(字符序列ll或字符序列LL)(自C99以来)

说明

1)十进制整数常量(以10为底,第一位数字最重要)。

2)八进制整数常量(基数8,第一位数字最重要)。

3)十六进制整数常量(基数16,第一个数字是最重要的,字母'a'到'f'代表十进制值10到15)。

以下变量初始化为相同的值:

int d = 42;
int o = 052;
int x = 0x2a;
int X = 0X2A;

整型常量的类型

整数常量的类型是该值可以匹配的第一个类型,它取决于使用哪个数字库和哪个整数后缀的类型列表。

| 整数常量允许的类型|

|:----|

| suffix | decimal bases | hexadecimal or octal bases |

| no suffix | int long int unsigned long int (until C99) long long int (since C99). | int unsigned int long int unsigned long int long long int(since C99) unsigned long long int(since C99). |

| u or U | unsigned int unsigned long int unsigned long long int(since C99). | unsigned int unsigned long int unsigned long long int(since C99). |

| l or L | long int unsigned long int(until C99) long long int(since C99). | long int unsigned long int long long int(since C99) unsigned long long int(since C99). |

| both l/L and u/U | unsigned long int unsigned long long int(since C99). | unsigned long int unsigned long long int(since C99). |

| ll or LL | long long int(since C99) | long long int(since C99) unsigned long long int(since C99). |

| both ll/LL and u/U | unsigned long long int(since C99) | unsigned long long int(since C99) |

如果整数常量的值太大而不适合后缀/基数组合所允许的任何类型,并且编译器支持扩展整数类型(如__int128),则该常量可以是扩展整数类型; 否则,该计划是不合格的。

笔记

在整数常量字母是不区分大小写的:0xDeAdBaBeU0XdeadBABEu表示相同的数目(一个例外是长长后缀,其是llLL,从未lLLl)。

没有负整数常量。诸如-1将一元减号运算符应用于由常量表示的值的表达式可能涉及隐式类型转换。

当在#if或#elif的控制表达式中使用时,所有有符号整型常量的作用就好像它们具有类型,intmax_t并且所有无符号整型常数的作用就好像它们具有类型一样uintmax_t

整型常量可以用在整型常量表达式中。

#include <stdio.h>
 
int main(void)
{
    printf("123 = %d\n", 123);
    printf("0123 = %d\n", 0123);
    printf("0x123 = %d\n", 0x123);
    printf("12345678901234567890ull = %llu\n", 12345678901234567890ull);
    // the type is unsigned long long even without a long long suffix
    printf("12345678901234567890u = %llu\n", 12345678901234567890u );
 
//  printf("%lld\n", -9223372036854775808); // ERROR
// the value 9223372036854775808 cannot fit in signed long long, which is the
// biggest type allowed for unsuffixed decimal integer constant
 
    printf("%llu\n", -9223372036854775808u );
    // unary minus applied to unsigned value subtracts it from 2^64,
    // this gives 9223372036854775808
 
    printf("%lld\n", -9223372036854775807 - 1);
    // // correct way to represent the value -9223372036854775808
}

输出:

123 = 123
0123 = 83
0x123 = 291
12345678901234567890ull = 12345678901234567890
12345678901234567890u = 12345678901234567890
9223372036854775808
-9223372036854775808

参考

  • C11 standard (ISO/IEC 9899:2011):
    • 6.4.4.1 Integer constants (p: 62-64)
  • C99 standard (ISO/IEC 9899:1999):
    • 6.4.4.1 Integer constants (p: 54-56)
  • C89/C90 standard (ISO/IEC 9899:1990):
    • 3.1.3.2 Integer constants

C 语法相关

1.#define directive
2.#elif directive
3.#else directive
4.#endif directive
5.#error directive
6.#if directive
7.#ifdef directive
8.#ifndef directive
9.#include directive
10.#line directive
11.#pragma directive
12.alignas
13.Alternative operators and tokens
14.Analyzability
15.Arithmetic operators
16.Arithmetic types
17.Array declaration
18.Array initialization
19.ASCII Chart
20.Assignment operators
21. types
22.Basic concepts
23.Bit fields
24.break statement
25.C language
26.C Operator Precedence
27.cast operator
28.character constant
29.Comments
30.Comparison operators
31.compound literals
32.Conditional inclusion
33.Conformance
34.const type qualifier
35.Constant expressions
36.continue statement
37.Declarations
38.do-while loop
39.Enumerations
40.Escape sequences
41.Expressions
42.External and tentative definitions
43.File scope
44.floating constant
45.for loop
46.Function declarations
47.Function definitions
48.Functions
49.Generic selection
50.goto statement
51.Identifier
52.if statement
53.Implicit conversions
54.Increment/decrement operators
55.Initialization
56.inline function specifier
57.Lifetime
58.Logical operators
59.Lookup and name spaces
60.Main function
61.Member access operators
62.Memory model
63.Objects and alignment
64.Order of evaluation
65.Other operators
66.Phases of translation
67.Pointer declaration
68.Preprocessor
69.restrict type qualifier
70.return statement
71.Scalar initialization
72.Scope
73.sizeof operator
74.Statements
75.static assert declaration
76.Static storage duration
77.Storage-class specifiers
78.string literals
79.Struct and union initialization
80.Struct declaration
81.switch statement
82.Thread storage duration
83.Type
84.Type
85.Typedef declaration
86.Undefined behavior
87.Union declaration
88.Value categories
89.Variadic arguments
90.volatile type qualifier
91.while loop
92._Alignof operator
93._Noreturn function specifier
C

C 语言是一门通用计算机编程语言,应用广泛。C 语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。