| GitHub | YouTube |
| Home | Projects | Resume |
| Project Links |
|---|
| Github |
Strint is a simple C library that converts integers to strings and strings to integers.
It was a very simple one-day project, but it is so useful that I figured it needed to be included here.
In C, there is a function in the standard library called printf.
The most basic use of this function is to write text to the terminal. For example:
printf("Hello, World!\n");
// prints: "Hello, World!"
You can also use formatters to insert variables:
int age = 23;
printf("I am %i years old.\n", age);
// prints: "I am 23 years old."
You can also have printf write to a buffer with snprintf.
Therefore, you can use the formatters to convert an integer to a string and
write it to a buffer:
char buf[32];
int age = 23;
snprintf(buf, sizeof(buf), "%i", age);
printf("%s\n", buf);
// prints: "23"
This may seem perfectly fine, but the problem is that printf is
essentially a tiny interpreter. Just like Python or any other scripting
language, the interpreter adds an extra layer of abstraction. This makes the
code easier to write, but much slower to run.
If you are ever writing a program that needs to convert millions of numbers
into strings, printf will not be your friend.
Also, I'm sure many other people have written the same exact library, but it always helps me to know that my code does what I need it to do.