News for hackers. Stuff that matters.
Re:Guide to Advanced Programming in C by Scene_Cast2 on Thursday February 06, 2014 @09:40PM (#7194499)
The sum() example does not work as the article says it does. Under Visual Studio 2013 compiling for x86 (C++ compiler, but C++ also has integer promotion rules), the function returns zero. The reason is: 65535 = 2^16-1. uint16_t is signed, therefore it has 15 bits to represent the value. When executing "int16_t a = 65535;" under a debugger, "a" is set to -1.
Re:Guide to Advanced Programming in C by pfacka on Friday February 07, 2014 @03:36AM (#7195327)
Thanks, I tested with GCC and Clang on Linux on 64-bit x86, GCC+OpenBSD on 32-bit+x86 and GCC+Linux on PowerPC 603. The point I was aiming for is that described operation is indeed undefined.
Re:Guide to Advanced Programming in C by burstmode on Friday February 07, 2014 @06:11AM (#7195724)
If uint16_t really is a signed 15bit value under VS2013, somebody in the compiler development department has a great sense of humor.
Re:Guide to Advanced Programming in C by Scene_Cast2 on Friday February 07, 2014 @08:36AM (#7196150)
My bad - that's a typo. I meant int16_t, the same as in the sample code.
Re:Guide to Advanced Programming in C by EpicEng on Thursday February 06, 2014 @10:26PM (#7194654)
It's actually undefined behavior.