The Perils of Multi-Character Constants
In C , multi-character constants, such as 'EVAW', are valid but often unnecessary and potentially perilous. The C standard does not specify the order in which characters are packed into an integer, making their portability questionable.
The Issue with Multi-Character Constants
The compiler generates a warning when using multi-character constants because, as per the C 14 standard, the value of such a constant is implementation-defined. This means that different compilers or even different versions of the same compiler may interpret 'EVAW' differently, leading to inconsistent results.
Alternatives to Multi-Character Constants
Instead of using multi-character constants, consider the following alternatives:
const int WAVE_HEADER = 0x45564157;
enum WaveHeaderType { EVAW };
const char* waveHeader = "EVAW";
Portability Considerations
To ensure portability, avoid using multi-character constants with integral types. Stick to the alternatives mentioned above to maintain consistency and avoid unexpected behavior across different platforms.
The above is the detailed content of Why Are Multi-Character Constants in C Dangerous?. For more information, please follow other related articles on the PHP Chinese website!