C Reference Manual Reading Notes: 006 Constants

本文详细介绍了C语言中四种类型的常量:整数、浮点数、字符和字符串。包括这些常量的不同表示方法及其类型指定符。同时,还解释了字符和字符串常量中的转义字符。

    The lexical class of constants includes four different kinds of constants: integers, floating-point numbers, characters, and strings. Suck tokens are called literals in other languages to distinguish thm from objects whose value are constants(i.e., not changing) but that do not belong to lexically distinct classes. An example of these latter objects in C is enumeration constrants, which belong to the lexical class of identifiers. In this book, we use traditional C terminology of constrant for both cases.

 

    1. Integer Constants.

        Integer constants may be specified in decimal, octual, or hexadecimal notation. There are the rules for determining the radix of an integer constant:

        (a). If the integer constant begins with the letters 0X or 0x, then it is in hexadecimal notation, with the character a through f(or A through F)representing 10 through 15.

        (b). Otherwise, if it begins with digit 0, then it is in octal notation.

        (c). Otherwise, it is in decimal notation.

        An integer constant may be immediately followed by suffix letters to designate a minimum size for its type:

        (a). Letters l or L indicate a constant of type long

        (b). Letters ll or LL indicate a constant of type long long(C99)(Notes, Ll or lL is invalid)

        (c). Letters u or U indicate an unsigned type of (int, long, or long long)

        (d). The unsigned suffix may be combined with the long or long long suffix in any order, like as 100uLL or 200ULL.

        These are valid integer constants: 100, 1000L, 200ll, 0x300, 0777, 0x98FCEuLL.

 

    2. Floating-Point Constants

        Floating-point constants may be written with a decimal point, a signed exponent, or both. Standard C allows a suffix letter(floating-suffix) to designate constants of type float and long double. Without a suffix, the type of the constant is double. Letters f or F indicate a constant of type of float, and letters l or L indicate a constant of type of long double. In C99, a complex floating-point constant iw written as a floating-point constant expression involving the imaginary constant _Complex_I(or I) defined in complex.h. C99 permits floating-point constants to be expressed in hexadecimal notation; previous versions of C had only decimal floating-point constants. The hexadecimal format use the letter p to separate the fraction from the exponent because the customary e could be confused with a hexadecimal digit. The binary-exponent is a signed decimal number that represents a power of 2(not a power of 10).

        These are valid floating-point constants: 0., e21, 3.1415, .01, 1.E-3, 0.52f, 5.5E2L, 0x55fceep-5

 

    3. Character Constants

        A character constant is written by enclosing one or more characters in apostrophes. A special escape mechanism is providedto write characters or numeric values that would be inconvenient or impossible to enter directly in the source program. Standard C allows the character constant to be preceded by the letter L to specify a wide character constant. The value of a character constant is implementation-defined if:

        (a). there is no corresponding character int the execution character set.

        (b). more than a single execution character appears in the constant, or

        (c). a numeric escape has a value not represented in the execution character set.

        Character constants not preceded by the letter L have type int. And wide character constants that designated by the prefix letter L have type wchar_t.

        Examples of  single-character constants along with their(decimal) values under the ASCII encoding:

            'a',  '/r',  ' ',  '/'',  '"',  '/0',  '/377',  '23',  '//'

 

    4. String Constants

        A string constant is a (possibly empty) sequence of characters enclosed in double quotes. The same escape mechanism provided for character constants can be used to express the characters in the string. Standard C allows the string constant to be preceded by the letter L to specify a wide string constant.

        For each nonwide string constant of n characters, at run time there will be a statically allocated block of n+1 characters whose first n characters are the character from the string and whose last character is the null character, '/0'. This block is the value of the string constant and its type is char[n+1]. Wide sting constants similarly become n wide characters followed by a null wide character and have type wchar-t[n+1].

        If a string constant appears anywhere except as an argument to the address operator &, an argument to the sizeof operator, or as an initializer of a character array, then the usual array conversions come into play, changing the string from an array of characters to a pointerto the first character in the string.

 

    5. Escape Characters

        a -- alert,                b--backspace,               f--form feed,               n--new line,               r--carriage return

        t--horizontal tab,    v--vertical tab,              /--backslash,               '--single quote,         "--double quote

        ?--question mark

        numeric escape code: '/004', '/006', '/xabc', etc.

在使用 Sophus 库时,如果遇到编译错误提示 `‘epsilonSqrt’ is not a member of ‘Sophus::Constants<double>’`,通常是因为所使用的 Sophus 版本与代码中调用的函数不兼容。 Sophus 是一个用于处理李群和李代数的 C++ 库,常用于 SLAM(同步定位与地图构建)等领域。随着库的更新,某些函数或常量的名称可能会发生变化或被移除。在较旧版本的 Sophus 中,`epsilonSqrt` 曾作为 `Constants` 类的一个静态成员函数存在,用于返回平方根的机器精度值。然而,在后续版本中,该函数可能已被移除或重命名[^1]。 ### 常见原因 1. **版本不兼容**:代码中使用了旧版本的 Sophus 函数,而当前安装的 Sophus 版本已不再支持该函数。 2. **命名空间变化**:某些函数可能从 `Sophus::Constants` 中移出,或改名以符合新的命名规范。 3. **依赖库未正确安装**:编译时使用的 Sophus 头文件路径可能不是预期版本。 ### 解决办法 - **检查 Sophus 版本**:确认当前使用的 Sophus 版本,并查阅其官方文档或 GitHub 仓库的发布说明,了解 `epsilonSqrt` 是否仍然存在或被替代。 - **更新代码以适配新版本**:如果 `epsilonSqrt` 已被移除,可以尝试使用 `std::sqrt(std::numeric_limits<double>::epsilon())` 替代,这将提供类似的机器精度平方根值[^1]。 - **回退到旧版本**:如果项目依赖旧版本的 Sophus,可以考虑回退到支持 `epsilonSqrt` 的版本,例如 v1.0.0 或更早版本。 - **使用包管理器重新安装**:如果使用的是通过包管理器安装的 Sophus(如通过 `vcpkg` 或 `conan`),可以尝试重新安装或更新库。 ### 示例替代代码 如果 `epsilonSqrt` 不再可用,可以用以下代码替代: ```cpp #include <limits> #include <cmath> double epsilon_sqrt = std::sqrt(std::numeric_limits<double>::epsilon()); ``` 此代码直接使用标准库中的 `std::numeric_limits` 和 `std::sqrt` 函数来计算浮点数的机器精度平方根。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值