Are long-suffix and unsigned-suffix needed when declaring long literals in C++?

前端 未结 4 2024
天命终不由人
天命终不由人 2021-01-07 01:22

I have some ancient memories of writing C code like:

long value = 0;

in the bad old Win16 days and ending up with value being

相关标签:
4条回答
  • 2021-01-07 02:05

    No this should not be required any more. The behavior you are describing, if visible in the program and not just the debugger, is a bug.

    0 讨论(0)
  • 2021-01-07 02:10

    Since you are presuming a 32-bit compiler, you may not need L as you did when using a 16-bit compiler, but you could need LL, e.g.:

    long long ll;
    ll = 1<<32;
    

    gcc -std=c99 warns "left shift count >= width of type." Instead, you'd want:

    ll = 1LL<<32;
    

    See also https://stackoverflow.com/a/8108658/782738, for C++11 and C99 standard references.

    0 讨论(0)
  • 2021-01-07 02:13

    They are not required in the examples you gave. However, they may be needed in some somewhat recondite circumstances. For example, the following may produce different values:

    sizeof(1)
    sizeof(1L)
    
    0 讨论(0)
  • 2021-01-07 02:20

    But this notation is required for wchar_t strings, like

    L"I am a wchar_t string."
    

    and for long long integers, too...

    I guess this is something that should be removed, it isn't useful (the type is declared with its name and not with some suffix), but still supported in C++11.

    Hm.

    0 讨论(0)
提交回复
热议问题