How to convert a C language string to a number?

In the C language, you can use the standard library functions atoi() and atol() to convert strings to integers and long integers, respectively. The specific implementation is as follows:

#include <stdio.h>
#include <stdlib.h>

int main() {
    char str[] = "12345";
    int num = atoi(str);
    long int longNum = atol(str);
    
    printf("String: %s\n", str);
    printf("Integer: %d\n", num);
    printf("Long Integer: %ld\n", longNum);
    
    return 0;
}

In the above code, you can use the atoi() function to convert a string to an integer and the atol() function to convert a string to a long integer. You can choose the appropriate function based on your needs.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds