tags:

views:

1759

answers:

5

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

I know the following:

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

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

+2  A: 

As far as I know, you don't, there's no such suffix. Most compilers will warn if an integer literal is too large to fit in whatever variable you're trying to store it in, though.

unwind
+10  A: 

((short)2)

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

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

movl    $2, _a
movl    $2, _b
movl    $2, _c
movl    $2, _d
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).
Kip
The "cast" is not really doing anything. There is no "cast" assembler instruction when we're talking C or C++ (.NET MSIL is a different story though). There on the metal, it's all just binary digits
Isak Savo
What are the types of a,b,c and d above?
Ates Goral
@Ates Goral: All ints. Changing to short or char would presumably change the instruction to movw or movb across the board.
+1  A: 

I did some searching on google and found a good reference here

From that site an unsigned short can be done by L'ab' One or two characters in single quotes ('), preceded by the letter L

jsl4980
Found the same page and tried it. Doesn't work in gcc/g++
VoidPointer
FYI, that's meant to be for wide-character literals not really for short integers. Just like you would specify a wide character string as L"somestring" you can specify a single wide-character as L'a'.
Gerald
According to Stroustrup, L'ab' is allowed but "the number of characters between the quotes and their meanings is implementation-defined to match the `wchar_t` type."
jwfearn
Indeed, wrong type. And on Mac OSX, even with the wrong number of bits. (short=16, wchar_t=32)
MSalters
A: 

You don't, you just write a regular int literal and write it short enough to fit into a short. Or if your c++ doesn't have wchar_t as a native type, you could try L'xx' but thats kinda hacky.

Dan
"If your c++ doesn't have wchar_t as a native type, you could try L'xx' but that's kinda wacky"...remove the "doesn't", I think. And a C++ compiler without wide characters is a fairly (I'm tempted to say very) retrograde compiler.
Jonathan Leffler
However, wchar_t isn't necessarily a short; on many platforms it's wchar_t UCS-4.
puetzk
my point was that if wchar_t isn't a native type that it is typically represented as a short instead. But as puetzk has pointed out that's not always the case. But I did say it's *hacky* as in it is a hack.
Dan
+2  A: 

Even the writers of the C99 standard got caught out by this. This is a snippet from Danny Smith's public domain stdint.h implementation:

/* 7.18.4.1  Macros for minimum-width integer constants

    Accoding to Douglas Gwyn <[email protected]>:
    "This spec was changed in ISO/IEC 9899:1999 TC1; in ISO/IEC
    9899:1999 as initially published, the expansion was required
    to be an integer constant of precisely matching type, which
    is impossible to accomplish for the shorter types on most
    platforms, because C99 provides no standard way to designate
    an integer constant with width less than that of type int.
    TC1 changed this to require just an integer constant
    *expression* with *promoted* type."
*/
Michael Burr