非常基本的问题:如何在 C++ 中编写 short
文字?
我知道以下几点:
2
是一个int
2U
是一个unsigned int
2L
是long
2LL
是long long
2.0f
是一个float
2.0
是一个double
'\2'
是一个char
。
但是我将如何编写 short
文字?我试过 2S
但这给出了编译器警告。
最佳答案
((short)2)
是的,严格来说,它不是一个简短的文字,更像是一个强制转换的整数,但行为是相同的,我认为没有直接的方法。
That's what I've been doing because I couldn't find anything about it. I would guess that the compiler would be smart enough to compile this as if it's a short literal (i.e. it wouldn't actually allocate an int and then cast it every time).
以下内容说明了您应该对此有多担心:
a = 2L;
b = 2.0;
c = (short)2;
d = '\2';
编译->反汇编->
movl $2, _a
movl $2, _b
movl $2, _c
movl $2, _d
关于c++ - 如何在 C++ 中编写简短的文字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/208433/