以下是将字符转换为int的示例。
#include <iostream>
using namespace std;
int main() {
char c = '8';
int i = c - 48;
cout << i;
i = c - '0';
cout <<"\t" << i;
return 0;
}输出结果
8 8
在上面的程序中,字符“ c”用值初始化。字符将转换为整数值,如下所示-
char c = '8'; int i = c - 48; cout << i; i = c - '0'; cout <<"\t" << i;