我如何在C ++中写一个短文字? [英] How do I write a short literal in C++?

查看:130
本文介绍了我如何在C ++中写一个短文字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

非常基本的问题:我如何在C ++中写一个 short 文字?

Very basic question: how do I write a short literal in C++?


  • 2 int

  • 2U unsigned int

  • 2L

  • 2LL 长长

  • 2.0f float

  • 2.0 c> double

  • '\2' code>。

  • 2 is an int
  • 2U is an unsigned int
  • 2L is a long
  • 2LL is a long long
  • 2.0f is a float
  • 2.0 is a double
  • '\2' is a char.

但是我如何写一个 short ?我尝试了 2S 但是给出了编译器警告。

But how would I write a short literal? I tried 2S but that gives a compiler warning.

推荐答案

((short)2)

,更多的是casted-int,但是行为是一样的,我认为没有直接的方法。

Yeah, it's not strictly a short literal, more of a casted-int, but the behaviour is the same and I think there isn't a direct way of doing it.


这是我一直在做,因为我找不到任何东西。我猜想编译器会聪明地编译这个,就像它是一个简短的字面量(即,它不会实际分配一个int,然后投射它每次)。

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).

下面说明你应该担心多少:

The following illustrates how much you should worry about this:

a = 2L;
b = 2.0;
c = (short)2;
d = '\2';

Compile - > disassemble - >

Compile -> disassemble ->

movl    $2, _a
movl    $2, _b
movl    $2, _c
movl    $2, _d

这篇关于我如何在C ++中写一个短文字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆