gpt4 book ai didi

c - 当 case 的数字前面加上 0 时,switch case 如何工作?

转载 作者:太空宇宙 更新时间:2023-11-04 05:12:05 25 4
gpt4 key购买 nike

我正在研究 Ingenico 的 EDC 终端。以下代码存在于以前的实现中。在调试时,我遇到了这段我难以理解的代码块。

short bankPEM = 0;
//bankPEM = 41; //Chip
bankPEM = 17; //Swipe
//bankPEM = 801; //Fallback

switch(bankPEM)
{
case 021: cout<<"021"; break; //Swipe
case 051: cout<<"051"; break; //Chip
case 801: cout<<"801"; break; //Fallback
default: cout<<"Default"; break;
}

bankPEM 是一个短变量。我发现以下执行观察:

  1. 当它包含41时,执行case 051
  2. 当它包含17时,执行case 021
  3. 当它包含801时,执行case 801

我希望代码为编号 1 和编号 2 执行 default 案例。在这种情况下,任何人都可以展示一些光。

我还将代码转换为汇编语言。我会在调试汇编代码后分享我的理解。

提前致谢。

最佳答案

引用c standard

6.4.4.1 Integer constants

A decimal constant begins with a nonzero digit and consists of a sequence of decimaldigits. An octal constant consists of the prefix 0 optionally followed by a sequence of thedigits 0 through 7 only. A hexadecimal constant consists of the prefix 0x or 0X followedby a sequence of the decimal digits and the letters a (or A) through f (or F) with values10 through 15 respectively.

强调我的

显示的代码似乎/是 也可以引用c++14

2.13.2 Integer literals

An integer literal is a sequence of digits that has no period or exponent part, with optional separating singlequotes that are ignored when determining its value. An integer literal may have a prefix that specifies its baseand a suffix that specifies its type. The lexically first digit of the sequence of digits is the most significant.A binary integer literal (base two) begins with 0b or 0B and consists of a sequence of binary digits. An octalinteger literal (base eight) begins with the digit 0 and consists of a sequence of octal digits. A decimalinteger literal (base ten) begins with a digit other than 0 and consists of a sequence of decimal digits. Ahexadecimal integer literal (base sixteen) begins with 0x or 0X and consists of a sequence of hexadecimaldigits, which include the decimal digits and the letters a through f and A through F with decimal valuesten through fifteen. [ Example: The number twelve can be written 12, 014, 0XC, or 0b1100. The literals1048576, 1’048’576, 0X100000, 0x10’0000, and 0’004’000’000 all have the same value. — end example ]

强调我的

关于c - 当 case 的数字前面加上 0 时,switch case 如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43630748/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com