Long Vs. Int C/C++ - What's The Point?

前端 未结 5 396
情深已故
情深已故 2020-12-12 22:00

As I\'ve learned recently, a long in C/C++ is the same length as an int. To put it simply, why? It seems almost pointless to even include the dat

5条回答
  •  鱼传尺愫
    2020-12-12 22:45

    long is not the same size as int, it is at least the same size as int. To quote the C++03 standard (3.9.1-2):

    There are four signed integer types: “signed char”, “short int”, “int”, and “long int.” In this list, each type provides at least as much storage as those preceding it in the list. Plain ints have the natural size suggested by the architecture of the execution environment); the other signed integer types are provided to meet special needs.

    My interpretation of this is "just use int, but if for some reason that doesn't fit your needs and you are lucky to find another integral type that's better suited, be our guest and use that one instead". One way that long might be better is if you 're on an architecture where it is... longer.

提交回复
热议问题