ustrtol(3alleg4)

✨ ustrtol β€” bestie, we're turning strings into integers ✨

okay so this is ustrtol from the Allegro manual (yes, the 3alleg4 girlies know) β€” it's a little function from the Allegro game programming library, and she is so useful. let's get into it πŸ’…


🌸 SYNOPSIS 🌸

#include <allegro.h>

long ustrtol(const char *s, char **endp, int base);

that's it. that's the whole vibe. three little args and she converts for you 🫢


πŸ’– DESCRIPTION πŸ’–

so here's the tea: this function converts the initial part of s to a signed integer. and if endp is not a NULL pointer, she sets *endp to point at the first unused character β€” like, "here's where I stopped reading, bestie, the rest is yours" 😭

the base argument tells her what base those digits (or letters, no judgement) should be treated as. it's giving very flexible, very inclusive πŸ¦‹

and here's the ✨iconic✨ part β€” if base is zero, she's a detective: she looks for 0x, 0X, or 0 as the first part of the string, and sets the base used to 16, 16, or 8 if she finds one. no cap. and the default base is 10 if none of those prefixes show up. periodt πŸ’…

example time, because we love a demo:

char *endp, *string = "456.203 askdfg";
int number = ustrtol(string, &endp, 10);

look at her go πŸ₯Ή


🌈 RETURN VALUE 🌈

returns the string converted as a value of type long int. main character energy fr fr πŸ‘‘

but β€” and listen closely β€” if nothing was converted, she returns zero with *endp pointing to the beginning of s. so like, she didn't even move. that's your vibe check right there πŸ’€


πŸŽ€ SEE ALSO πŸŽ€

go read those too, they're all part of the same friend group 🫢✨


and that's the whole page, bestie!! this one came from the Allegro manual, version 4.4.3, and yeah β€” ustrtol(3alleg4) is her full government name. go forth and parse responsibly πŸ§šπŸ’–πŸŒΈ