...
Code Block |
---|
|
unsigned int ui1,ui;
unsigned int ui2,;
unsigned int sum;
if (~ui1~ui < ui2) {
/* Handle error condition */
}
sum = ui1ui + ui2;
|
This code assumes that the implementation uses two's complement representation. This assumption is commonly true but is not guaranteed by the standard.
...
Code Block |
---|
|
unsigned int ui1, ui2, ui;
unsigned int ui2;
unsigned int sum;
if (UINT_MAX - ui1ui < ui2) {
/* Handle error condition */
}
sum = ui1ui + ui2;
|
If the noncompliant form of this test is truly faster, talk to your compiler vendor because, if these tests are equivalent, optimization should occur. If both forms have the same performance, prefer the portable form.
...
Code Block |
---|
|
void f() {
char buf[80BUFSIZ];
fprintf(stderr, "Error: %s\n",
strerror_r(errno, buf, sizeof buf));
}
|
...
Code Block |
---|
|
#define _XOPEN_SOURCE 600
#include <string.h>
#include <stdio.h>
#include <errno.h>
void f() {
char buf[80BUFSIZ];
int result;
result = strerror_r(errno, buf, sizeof buf);
if (0 != result) {
strcpy(buf, "Unknown error");
}
fprintf(stderr, "Error: %s\n", buf);
}
|
...