Master C String‑Number Conversion Functions: atof, atoi, atol, strtod, strtol, and More
This guide consolidates essential C standard library functions for converting between strings and numeric types—including atof, atoi, atol, gcvt, strtod, strtol, strtoul, toascii, tolower, and toupper—detailing their headers, prototypes, behavior, and practical code examples with expected outputs.
atof – Convert String to Double
Header: #include <stdlib.h> Prototype: double atof(const char *nptr); Explanation: Scans the input string, skips leading whitespace, then parses an optional sign, digits, decimal point, or exponent (e/E). Conversion stops at the first non‑numeric character or the string terminator, returning the resulting double value. It behaves like strtod(nptr, NULL).
Example:
/* Convert strings a and b to numbers and add them */
#include <stdlib.h>
int main(){
char *a = "-100.23";
char *b = "200e-2";
double c = atof(a) + atof(b);
printf("c=%.2f
", c);
}Output:
c=-98.23atoi – Convert String to Integer
Header: #include <stdlib.h> Prototype: int atoi(const char *nptr); Explanation: Similar to atof but returns an int. It stops conversion at the first non‑digit character. Equivalent to strtol(nptr, NULL, 10).
Example:
/* Convert strings a and b to integers and add them */
#include <stdlib.h>
int main(){
char a[] = "-100";
char b[] = "456";
int c = atoi(a) + atoi(b);
printf("c=%d
", c);
}Output:
c=356atol – Convert String to Long Integer
Header: #include <stdlib.h> Prototype: long atol(const char *nptr); Explanation: Parses the string as a signed long integer, stopping at the first non‑numeric character. Equivalent to strtol(nptr, NULL, 10).
Example:
/* Add two large numbers represented as strings */
#include <stdlib.h>
int main(){
char a[] = "1000000000";
char b[] = "234567890";
long c = atol(a) + atol(b);
printf("c=%ld
", c);
}Output:
c=1234567890gcvt – Convert Double to String with Rounding
Header: #include <stdlib.h> Prototype: char *gcvt(double number, size_t ndigits, char *buf); Explanation: Converts a double to an ASCII string with the specified number of significant digits. Unlike ecvt and fcvt, the resulting string includes a decimal point or sign when appropriate. The converted string is stored in the buffer pointed to by buf.
Example:
#include <stdlib.h>
int main(){
double a = 123.45;
double b = -1234.56;
char buf[32];
gcvt(a, 5, buf);
printf("a value=%s
", buf);
gcvt(b, 6, buf);
printf("b value=%s
", buf);
}Output:
a value=123.45
b value=-1234.56strtod – Convert String to Double with End Pointer
Header: #include <stdlib.h> Prototype: double strtod(const char *nptr, char **endptr); Explanation: Parses a string to a double, similar to atof, but also returns a pointer to the first character not used in the conversion via endptr. Supports optional sign, decimal point, and exponent.
Example (different bases):
/* Convert strings in decimal, binary, and hexadecimal */
#include <stdlib.h>
int main(){
char a[] = "1000000000"; // decimal
char b[] = "1000000000"; // binary (base 2)
char c[] = "ffff"; // hexadecimal (base 16)
printf("a=%d
", (int)strtod(a, NULL));
printf("b=%d
", (int)strtod(b, NULL, 2));
printf("c=%d
", (int)strtod(c, NULL, 16));
}Output:
a=1000000000
b=512
c=65535strtol – Convert String to Long Integer with Base
Header: #include <stdlib.h> Prototype: long int strtol(const char *nptr, char **endptr, int base); Explanation: Converts the string to a signed long integer using the specified base (2‑36 or 0). Base 0 selects the base automatically (e.g., “0x” for hex). The function stops at the first invalid character and can return that position via endptr. Errors set errno to ERANGE if the value is out of range.
Example:
/* Convert strings in decimal, binary, and hexadecimal */
#include <stdlib.h>
int main(){
char a[] = "1000000000"; // decimal
char b[] = "1000000000"; // binary (base 2)
char c[] = "ffff"; // hexadecimal (base 16)
printf("a=%ld
", strtol(a, NULL, 10));
printf("b=%ld
", strtol(b, NULL, 2));
printf("c=%ld
", strtol(c, NULL, 16));
}Output:
a=1000000000
b=512
c=65535strtoul – Convert String to Unsigned Long Integer
Header: #include <stdlib.h> Prototype:
unsigned long int strtoul(const char *nptr, char **endptr, int base);Explanation: Same as strtol but returns an unsigned long. Handles bases 2‑36 or 0, with overflow indicated by ERANGE in errno.
Example: identical to the strtol example, using strtoul instead.
toascii – Convert Integer to 7‑bit ASCII
Header: #include <ctype.h> Prototype: int toascii(int c); Explanation: Masks the argument to 7 bits, effectively converting it to a valid ASCII character.
Example:
#include <stdlib.h>
int main(){
int a = 217;
char b;
printf("before toascii(): a=%d(%c)
", a, a);
b = toascii(a);
printf("after toascii(): b=%d(%c)
", b, b);
}Output:
before toascii(): a=217()
after toascii(): b=89(Y)tolower – Convert Uppercase Letter to Lowercase
Header: #include <ctype.h> Prototype: int tolower(int c); Explanation: If c is an uppercase alphabetic character, returns its lowercase equivalent; otherwise returns c unchanged.
Example:
/* Convert all uppercase letters in a string to lowercase */
#include <ctype.h>
int main(){
char s[] = "aBcDeFgH12345;!#$";
printf("before tolower(): %s
", s);
for (int i = 0; i < sizeof(s); ++i) {
s[i] = tolower(s[i]);
}
printf("after tolower(): %s
", s);
}Output:
before tolower(): aBcDeFgH12345;!#$
after tolower(): abcdefgh12345;!#$toupper – Convert Lowercase Letter to Uppercase
Header: #include <ctype.h> Prototype: int toupper(int c); Explanation: If c is a lowercase alphabetic character, returns its uppercase counterpart; otherwise returns c unchanged.
Example:
/* Convert all lowercase letters in a string to uppercase */
#include <ctype.h>
int main(){
char s[] = "aBcDeFgH12345;!#$";
printf("before toupper(): %s
", s);
for (int i = 0; i < sizeof(s); ++i) {
s[i] = toupper(s[i]);
}
printf("after toupper(): %s
", s);
}Output:
before toupper(): aBcDeFgH12345;!#$
after toupper(): ABCDEFGH12345;!#$Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
