Yesterday,I learn some c23 new features. Maybe there is a bug in stdckdint.h.
bool ckd_add( type1 *result, type2 a, type3 b );
bool ckd_sub( type1 *result, type2 a, type3 b );
bool ckd_mul( type1 *result, type2 a, type3 b );
Macros always return false. Pelles c 13.01. Windows 11 Simplified Chinese.
#include <stdio.h>
#include <stdckdint.h>
int main(void)
{
int a = 655350;
int b = 10;
short int c = 0;
//int c = 0;
bool result = true;
result = ckd_mul(&c, a, b); // ckd_add(&c, a, b); ckd_sub(&c, a, b);
if (result == true) {
puts("true\n");
}else{
puts("false\n");
}
printf("c = %d\n", c);
return 0;
}
Hi Ander,
Well not always - If the numbers are big enough to 'wrap' you'll get 'true'
for example - output from code below :
true
c = -13107100
false
c = 1310700
false
c = 0
Press any key to continue...
#include <stdio.h>
#include <stdckdint.h>
int main(void)
{
long int a = 655350;
long int b = 655350;//10
int c = 0;
//int c = 0;
bool result = true;
result = ckd_mul(&c, a, b); // ckd_add(&c, a, b); ckd_sub(&c, a, b);
if (result == true) {
puts("true\n");
}else{
puts("false\n");
}
printf("c = %d\n", c);
result = ckd_add(&c, a, b); // ckd_sub(&c, a, b);
if (result == true) {
puts("true\n");
}else{
puts("false\n");
}
printf("c = %d\n", c);
result = ckd_sub(&c, a, b);
if (result == true) {
puts("true\n");
}else{
puts("false\n");
}
printf("c = %d\n", c);
return 0;
}
Cheers,
John Z
https://cppreference.net/c/numeric/ckd_mul.html (https://cppreference.net/c/numeric/ckd_mul.html)
Just easier to read:
#include <stdio.h>
#include <stdckdint.h>
char *ab[] = {"false", "true "};
int main(void)
{
long int a = 655350;
long int b = 655350;//10
int c = 0;
bool result;
result = ckd_mul(&c, a, b); // ckd_add(&c, a, b); ckd_sub(&c, a, b);
printf("result: %s c = %d\n", ab[result], c);
result = ckd_add(&c, a, b); // ckd_sub(&c, a, b);
printf("result: %s c = %d\n", ab[result], c);
result = ckd_sub(&c, a, b);
printf("result: %s c = %d\n", ab[result], c);
return 0;
}
👍👍👍
Definitely! Thanks Timo.
Better output too -
long int a = 65536;
long int b = 65535;//10
int c = 0;
---------output---------
result: true c = -65536
result: false c = 131071
result: false c = 1
Press any key to continue...
John Z
Quote from: TimoVJL on March 08, 2026, 03:10:42 PMhttps://cppreference.net/c/numeric/ckd_mul.html (https://cppreference.net/c/numeric/ckd_mul.html)
thank you for your reply, TimoVJL. I have read it. please try my test code.
Pelles C 13.01 will print :false
c = 0
but gcc 15.2 will print :true
c = 0
And I read the Pelles C help file for "stdckdint.h". I think the return value depends on first argument type. But Pelles C returns false when the first argument overflow in following code.
#include <stdio.h>
#include <stdckdint.h>
int main(void)
{
int a = 655350;
int b = 10;
short int c = 0;
bool result = true;
result = ckd_add(&c, a, b); //out of range of short. a and b are int, c is short int.
if (result == true)
{
puts("true");
}else{
puts("false");
}
printf("c = %d ", c);
return 0;
}
Quote from: John Z on March 08, 2026, 09:26:14 PM👍👍👍
Definitely! Thanks Timo.
Better output too -
long int a = 65536;
long int b = 65535;//10
int c = 0;
---------output---------
result: true c = -65536
result: false c = 131071
result: false c = 1
Press any key to continue...
John Z
Thank you for your reply, John Z.
The "long int" and the "int" are 32bit value in Pelles c.
I get the false when
only the first argument overflow in function ckd_add, I think it is a bug.
#include <stdio.h>
#include <stdckdint.h>
int main(void)
{
int a = 655350;
int b = 10;
short int c = 0;
bool result = true;
result = ckd_add(&c, a, b); //out of range of short. a and b are int, c is short int.
if (result == true)
{
puts("true");
}else{
puts("false");
}
printf("c = %d ", c);
return 0;
}
with dataset:
int a = 655350;
int b = 10;
short c = 0;
Clang output:
true c = -100
true c = 0
true c = -20
I think ckd_<op>() came more or less straight from GCC to the C23 standard, and like some other GCC "innovations" it's a bit over-worked / under-thinked.
I will revise my initial implementation slightly, mostly just to reject more bogus cases -- like the original bug report.
Quote from: Pelle on March 15, 2026, 11:32:37 AMI think ckd_<op>() came more or less straight from GCC to the C23 standard, and like some other GCC "innovations" it's a bit over-worked / under-thinked.
I will revise my initial implementation slightly, mostly just to reject more bogus cases -- like the original bug report.
Thank you, Pelle!