编程札记

C

C库函数 realloc()

C库函数 realloc() #include <stdio.h> #include <stdlib.h> #include <string.h> int main () { char *str; /* Initial memory allocation */ str = (char *) malloc(15); strcpy(str, "codingdict"); printf("String = %s, Address = %u\n", str, str); /* Reallocating memory */ str = (char *) realloc(str, 25); strcat(str, ".com"); printf("String = %s, Address = %u\n", str, str); free(str); return(0); }

阅读全文 ↗
C

C库函数 malloc()

C库函数 malloc() #include <stdio.h> #include <stdlib.h> #include <string.h> int main () { char *str; /* Initial memory allocation */ str = (char *) malloc(15); strcpy(str, "codingdict"); printf("String = %s, Address = %u\n", str, str); /* Reallocating memory */ str = (char *) realloc(str, 25); strcat(str, ".com"); printf("String = %s, Address = %u\n", str, str); free(str); return(0); }

阅读全文 ↗
C

C库函数 free()

C库函数 free() #include <stdio.h> #include <stdlib.h> #include <string.h> int main () { char *str; /* Initial memory allocation */ str = (char *) malloc(15); strcpy(str, "codingdict"); printf("String = %s, Address = %u\n", str, str); /* Reallocating memory */ str = (char *) realloc(str, 25); strcat(str, ".com"); printf("String = %s, Address = %u\n", str, str); /* Deallocate allocated memory */ free(str); return(0); }

阅读全文 ↗
C

C库函数 strtoul()

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

C库函数 strtol()

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

C库函数 strtod()

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

C库函数 atol()

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

C库函数 atoi()

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

C库函数 atof()

C库函数 atof() #include <stdio.h> #include <stdlib.h> #include <string.h> int main () { float val; char str[20]; strcpy(str, "98993489"); val = atof(str); printf("String value = %s, Float value = %f\n", str, val); strcpy(str, "codingdict.com"); val = atof(str); printf("String value = %s, Float value = %f\n", str, val); return(0); }

阅读全文 ↗
C

C库函数 fmod()

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

C库函数 perror()

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); }

阅读全文 ↗
C

C库函数 puts()

C库函数 puts() #include <stdio.h> #include <string.h> int main () { char str1[15]; char str2[15]; strcpy(str1, "codingdict"); strcpy(str2, "compileonline"); puts(str1); puts(str2); return(0); }

阅读全文 ↗