C库函数 strtoul() #include <stdio.h> #include <stdlib.h> int main () { char str[30] = "2030300 This is test"; char *ptr; long ret; ret = strtoul(str, &ptr, 10); printf("The number(unsigned long integer) is %lu\n", ret); printf("String part is |%s|", ptr); return(0); }
C库函数 strtol() #include <stdio.h> #include <stdlib.h> int main () { char str[30] = "2030300 This is test"; char *ptr; long ret; ret = strtol(str, &ptr, 10); printf("The number(unsigned long integer) is %ld\n", ret); printf("String part is |%s|", ptr); return(0); }
C库函数 strtod() #include <stdio.h> #include <stdlib.h> int main () { char str[30] = "20.30300 This is test"; char *ptr; double ret; ret = strtod(str, &ptr); printf("The number(double) is %lf\n", ret); printf("String part is |%s|", ptr); return(0); }
C库函数 atol() #include <stdio.h> #include <stdlib.h> #include <string.h> int main () { long val; char str[20]; strcpy(str, "98993489"); val = atol(str); printf("String value = %s, Long value = %ld\n", str, val); strcpy(str, "codingdict.com"); val = atol(str); printf("String value = %s, Long value = %ld\n", str, val); return(0); }
C库函数 atoi() #include <stdio.h> #include <stdlib.h> #include <string.h> int main () { int val; char str[20]; strcpy(str, "98993489"); val = atoi(str); printf("String value = %s, Int value = %d\n", str, val); strcpy(str, "codingdict.com"); val = atoi(str); printf("String value = %s, Int value = %d\n", str, val); return(0); }
C库函数 fmod() #include <stdio.h> #include <math.h> int main () { float a, b; int c; a = 9.2; b = 3.7; c = 2; printf("Remainder of %f / %d is %lf\n", a, c, fmod(a,c)); printf("Remainder of %f / %f is %lf\n", a, b, fmod(a,b)); return(0); }
C库函数 perror() #include <stdio.h> int main () { FILE *fp; /* first rename if there is any file */ rename("file.txt", "newfile.txt"); /* now let's try to open same file */ fp = fopen("file.txt", "r"); if( fp == NULL ) { perror("Error: "); return(-1); } fclose(fp); return(0); }