#include<stdio.h>#include<stdlib.h> #include<string.h> #include<ctype.h> int main(){ char *str = "-1111.11"; /** * double atof(const char *nptr); * int atoi(const char *nptr); * long atol(const char *nptr); * long long atoll(const char *nptr); */ printf("atof(%s) = %f\n", str, atof(str)); printf("atoi(%s) = %d\n", str, atoi(str)); printf("atol(%s) = %ld\n", str, atol(str)); printf("atoll(%s) = %lld\n", str, atoll(str)); puts("================"); /** * double strtod(const char *nptr, char **endptr); * float strtof(const char *nptr, char **endptr); * long int strtol(const char *nptr, char **endptr, int base); * unsigned long int strtoul(const char *nptr, char **endptr, int base); */ printf("strtod(%s, NULL) = %f\n", str, strtod(str, NULL)); printf("strtof(%s, NULL) = %f\n", str, strtof(str, NULL)); printf("strtol(%s, NULL, 0) = %ld\n", str, strtol(str, NULL, 0)); printf("strtol(%s, NULL, 2) = %ld\n", str, strtol(str, NULL, 2)); printf("strtol(%s, NULL, 16) = %ld\n", str, strtol(str, NULL, 16)); printf("strtoul(%s, NULL, 0) = %ld\n", str, strtoul(str, NULL, 0)); puts("================"); // int sscanf(const char *str, const char *format, ...); float value; sscanf(str, "%f", &value); printf("scanf(\"%s\", \%%f, &value); value = %f\n", str, value); puts("================"); //================================================== double number = -1234.5678; int decpt; int sign; /** * char *ecvt(double number, int ndigits, int *decpt, int *sign); * char *fcvt(double number, int ndigits, int *decpt, int *sign); * char *gcvt(double number, size_t ndigit, char *buf); * These functions are obsolete. Instead, sprintf() is recommended. */ //when using ecvt and fcvf, the sign and . are removed in the returned string // the sign and . are represented by *sign and &decpt char *result_str = ecvt(number, 6, &decpt, &sign); printf("ecvt: decpt = %d, sign = %d, result_str = %s\n", decpt, sign, result_str); char *rt_str = fcvt(number, 6, &decpt, &sign); printf("fcvt: decpt = %d, sign = %d, result_str = %s\n", decpt, sign, rt_str); char buf[15]; char *g_str = gcvt(number, 10, buf); printf("gcvt, buf = %s, g_str = %s\n", buf, g_str); // int sprintf(char *str, const char *format, ...); char num_str[13]; sprintf(num_str, "%f", number); puts(num_str); //====================================== // int toascii(int c); printf("toascii(%c) = %d\n", 'A', toascii('A')); printf("toascii(%c) = %d\n", 'a', toascii('a')); //====================================== // int toupper(int c); // int tolower(int c); char str_k[] = "ab&%CD*#eF"; int i; for(i = 0; i < strlen(str_k); i++) str_k[i] = toupper(str_k[i]); puts(str_k); for(i = 0; i < strlen(str_k); i++) str_k[i] = tolower(str_k[i]); puts(str_k); return 0; }