Click the “C Language Chinese Community” above to set the public account as a star to view C language notes at the first time!

10 Great Open Source Projects in C Language
Essential Skills for C Language Development: Multi-file Programming, Understand Thoroughly in One Article
Implementing a Balanced Binary Tree in C Language | Illustrated + Detailed Code
8 Basic and Practical Classic Examples in C Language [Source Code Included]
Commonly used string and number conversions have been collected and organized for future use.
- atof (converts a string to a floating-point number)
- atoi (converts a string to an integer)
- atol (converts a string to a long integer)
- strtod (converts a string to a floating-point number)
- strtol (converts a string to a long integer)
- strtoul (converts a string to an unsigned long integer)
- toascii (converts an integer to a valid ASCII character)
- toupper (converts a lowercase letter to an uppercase letter)
- tolower (converts an uppercase letter to a lowercase letter)
atof (converts a string to a floating-point number)
Related functions: atoi, atol, strtod, strtol, strtoul
Header file: #include <stdlib.h>
Function definition: double atof(const char *nptr);
Function description: atof() scans the input string nptr, skipping leading whitespace characters, and starts conversion when it encounters a number or a sign. It stops converting when it encounters a non-numeric character or the end of the string (‘\0’), returning the result. The input string nptr can include a sign, decimal point, or ‘E’ (e) to indicate the exponent part, such as 123.456 or 123e-2.
Return value: Returns the converted floating-point number.
Additional note: atof() produces the same result as using strtod(nptr, (char**)NULL).
Example
/* Add the numbers after converting strings a and b to numbers */
#include <stdlib.h>
main()
{
char *a = "-100.23";
char *b = "200e-2";
float c;
c = atof(a) + atof(b);
printf("c=%.2f\n", c);
}
Execution: c=-98.23
atoi (converts a string to an integer)
Related functions: atof, atol, strtod, strtol, strtoul
Header file: #include <stdlib.h>
Function definition: int atoi(const char *nptr);
Function description: atoi() scans the input string nptr, skipping leading whitespace characters, and starts conversion when it encounters a number or a sign. It stops converting when it encounters a non-numeric character or the end of the string (‘\0’), returning the result.
Return value: Returns the converted integer.
Additional note: atoi() produces the same result as using strtol(nptr, (char**)NULL, 10);
Search the public account C Language Chinese Community and reply “C Language” to receive 200G of programming resources for free.
Example
/* Add the numbers after converting strings a and b to numbers */
#include <stdlib.h>
mian()
{
char a[] = "-100";
char b[] = "456";
int c;
c = atoi(a) + atoi(b);
printf("c=%d\n", c);
}
Execution: c=356
atol (converts a string to a long integer)
Related functions: atof, atoi, strtod, strtol, strtoul
Header file: #include <stdlib.h>
Function definition: long atol(const char *nptr);
Function description: atol() scans the input string nptr, skipping leading whitespace characters, and starts conversion when it encounters a number or a sign. It stops converting when it encounters a non-numeric character or the end of the string (‘\0’), returning the result.
Return value: Returns the converted long integer.
Additional note: atol() produces the same result as using strtol(nptr, (char**)NULL, 10);
Example
/* Add the numbers after converting strings a and b to numbers */
#include <stdlib.h>
main()
{
char a[] = "1000000000";
char b[] = " 234567890";
long c;
c = atol(a) + atol(b);
printf("c=%d\n", c);
}
Execution: c=1234567890
gcvt (converts a floating-point number to a string, rounding)
Related functions: ecvt, fcvt, sprintf
Header file: #include <stdlib.h>
Function definition: char *gcvt(double number, size_t ndigits, char *buf);
Function description: gcvt() converts the input number to an ASCII string, with ndigits indicating the number of digits to display. Unlike ecvt() and fcvt(), gcvt() includes the decimal point and sign in the converted string. If the conversion is successful, the resulting string is stored in the space pointed to by buf.
Return value: Returns a pointer to the string, which is the address of buf.
Example
#include <stdlib.h>
main()
{
double a = 123.45;
double b = -1234.56;
char *ptr;
int decpt, sign;
gcvt(a, 5, ptr);
printf("a value=%s\n", ptr);
ptr = gcvt(b, 6, ptr);
printf("b value=%s\n", ptr);
}
Execution: a value=123.45
b value=-1234.56
strtod (converts a string to a floating-point number)
Related functions: atoi, atol, strtod, strtol, strtoul
Header file: #include <stdlib.h>
Function definition: double strtod(const char *nptr, char **endptr);
Function description: strtod() scans the input string nptr, skipping leading whitespace characters, and starts conversion when it encounters a number or a sign. It stops converting when it encounters a non-numeric character or the end of the string (‘\0’), returning the result. If endptr is not NULL, it will return a pointer to the character in nptr where the conversion stopped.
Return value: Returns the converted floating-point number.
Additional note: Refer to atof().
Example
/* Convert strings a, b, c to numbers using bases 10, 2, 16 respectively */
#include <stdlib.h>
mian()
{
char a[] = "1000000000";
char b[] = "1000000000";
char c[] = "ffff";
printf("a=%d\n", strtod(a, NULL, 10));
printf("b=%d\n", strtod(b, NULL, 2));
printf("c=%d\n", strtod(c, NULL, 16));
}
Execution
a=1000000000
b=512
c=65535
strtol (converts a string to a long integer)
Related functions: atof, atoi, atol, strtod, strtoul
Header file: #include <stdlib.h>
Function definition: long int strtol(const char *nptr, char **endptr, int base);
Function description: strtol() converts the input string nptr to a long integer based on the specified base. The base can range from 2 to 36, or 0. If base is 10, it uses decimal; if base is 16, it uses hexadecimal, etc. When base is 0, it defaults to decimal but will use hexadecimal if it encounters a ‘0x’ prefix. strtol() scans the input string nptr, skipping leading whitespace characters, and starts conversion when it encounters a number or a sign. It stops converting when it encounters a non-numeric character or the end of the string (‘\0’), returning the result. If endptr is not NULL, it will return a pointer to the character in nptr where the conversion stopped.
Return value: Returns the converted long integer, or returns ERANGE and sets errno if the conversion string is out of range.
Additional note: ERANGE indicates that the conversion string exceeds the valid range.
Example
/* Convert strings a, b, c to numbers using bases 10, 2, 16 respectively */
#include <stdlib.h>
main()
{
char a[] = "1000000000";
char b[] = "1000000000";
char c[] = "ffff";
printf("a=%d\n", strtol(a, NULL, 10));
printf("b=%d\n", strtol(b, NULL, 2));
printf("c=%d\n", strtol(c, NULL, 16));
}
Execution
a=1000000000
b=512
c=65535
strtoul (converts a string to an unsigned long integer)
Related functions: atof, atoi, atol, strtod, strtol
Header file: #include <stdlib.h>
Function definition: unsigned long int strtoul(const char *nptr, char **endptr, int base);
Function description: strtoul() converts the input string nptr to an unsigned long integer based on the specified base. The base can range from 2 to 36, or 0. If base is 10, it uses decimal; if base is 16, it uses hexadecimal, etc. When base is 0, it defaults to decimal but will use hexadecimal if it encounters a ‘0x’ prefix. strtoul() scans the input string nptr, skipping leading whitespace characters, and starts conversion when it encounters a number or a sign. It stops converting when it encounters a non-numeric character or the end of the string (‘\0’), returning the result. If endptr is not NULL, it will return a pointer to the character in nptr where the conversion stopped.
Return value: Returns the converted long integer, or returns ERANGE and sets errno if the conversion string is out of range.
Additional note: ERANGE indicates that the conversion string exceeds the valid range.
Example: Refer to strtol()
toascii (converts an integer to a valid ASCII character)
Related functions: isascii, toupper, tolower
Header file: #include <ctype.h>
Function definition: int toascii(int c)
Function description: toascii() converts the input c to a 7-bit unsigned char value, clearing the eighth bit, resulting in an ASCII character.
Search the public account C Language Chinese Community and reply “C Language” to receive 200G of programming resources for free.
Return value: Returns the ASCII character value if conversion is successful. Example
#include <stdlib.h>
main()
{
int a = 217;
char b;
printf("before toascii(): a value = %d(%c)\n", a, a);
b = toascii(a);
printf("after toascii(): a value = %d(%c)\n", b, b);
}
Execution
before toascii(): a value = 217()
after toascii(): a value = 89(Y)
tolower (converts an uppercase letter to a lowercase letter)
Related functions: isalpha, toupper
Header file: #include <stdlib.h>
Function definition: int tolower(int c);
Function description: If the input c is an uppercase letter, it returns the corresponding lowercase letter.
Return value: Returns the converted lowercase letter; if no conversion is needed, it returns the input value c.
Example
/* Convert uppercase letters in string s to lowercase */
#include <ctype.h>
main()
{
char s[] = "aBcDeFgH12345;!#$";
int i;
printf("before tolower(): %s\n", s);
for(i = 0; i < sizeof(s); i++)
s[i] = tolower(s[i]);
printf("after tolower(): %s\n", s);
}
Execution
before tolower(): aBcDeFgH12345;!#$
after tolower(): abcdefgh12345;!#$
toupper (converts a lowercase letter to an uppercase letter)
Related functions: isalpha, tolower
Header file: #include <ctype.h>
Function definition: int toupper(int c);
Function description: If the input c is a lowercase letter, it returns the corresponding uppercase letter.
Return value: Returns the converted uppercase letter; if no conversion is needed, it returns the input value c.
Example
/* Convert lowercase letters in string s to uppercase */
#include <ctype.h>
main()
{
char s[] = "aBcDeFgH12345;!#$";
int i;
printf("before toupper(): %s\n", s);
for(i = 0; i < sizeof(s); i++)
s[i] = toupper(s[i]);
printf("after toupper(): %s\n", s);
}
Execution
before toupper(): aBcDeFgH12345;!#$
after toupper(): ABCDEFGH12345;!#$
Disclaimer: This article’s material comes from the internet, and the copyright belongs to the original author. If there are any copyright issues, please contact me for removal.
‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧ END ‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧