add_of(3)

πŸ’– add_of(3) β€” the little overflow-checker that could πŸ’–

okay bestie, gather round, because today we're talking about add_of, a function from the Library Functions Manual and honestly? she's doing the most for you and you don't even know it yet.


✨ NAME ✨

add_of β€” add two integers, and check for arithmetic overflow 🫢

like. she adds. she checks. she's an icon.


🌸 SYNTAX 🌸

#include <libowfat/rangecheck.h>

int add_of(dest,a,b);

that's it, that's the whole vibe. include the header, call the macro, live your truth. ✨


πŸ¦‹ DESCRIPTION πŸ¦‹

so here's the tea β˜•: if calculating a+b is possible without causing undefined behavior or an arithmetic overflow in C, and the sum actually fits into the destination integer type? she does dest=a+b and returns 0. slay. no cap.

otherwise? she returns 1. that's it. that's the whole girl. πŸ’…

πŸŽ€ Note, bestie: this is a macro, so dest does not have to be a pointer. yeah. you heard me. iconic behavior honestly.

πŸ™ˆ BUGS πŸ™ˆ

okay so this is the part where things get a lil chaotic and we need to talk about it 😭

in the multiplication functions, a return value of 1 signals success and 0 signals failure.

but in add_of, sub_of and assign? it's the other way around. πŸ’€

so like. read the return value carefully. do not get got. i'm begging you. 🫢


πŸ’œ SEE ALSO πŸ’œ

go read them too, they're all part of the same friendship group ✨


and that's the whole page, bestie πŸŽ€ shoutout to the Library Functions Manual for hosting her, and shoutout to add_of(3) herself for being the responsible one who checks before she sums. stay safe out there and always check your overflow, ily πŸ’–πŸ¦‹πŸŒΈ