Tsallaka zuwa abun ciki
Justice Tech School

Converting Strings and Numbers

atoi, strtol and strtod turn text into numbers, and snprintf builds strings safely. sscanf parses formatted text.

Example
int n = atoi("42");
long big = strtol("123456", NULL, 10);
double d = strtod("2.5", NULL);
printf("%d %ld %.1f\n", n + 1, big, d * 2);
char buf[32];
snprintf(buf, sizeof buf, "id-%04d", 7);
printf("%s\n", buf);
int a, b; sscanf("10 20", "%d %d", &a, &b);
printf("%d\n", a + b);

Result

43 123456 5.0
id-0007
30

Exercise

Safe formatted write.

(buf, sizeof buf, "x")

Quiz Test

Which formats into a buffer safely?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.