编程札记

C

C库函数 - memcmp()

C库函数 - memcmp() #include <stdio.h> #include <string.h> int main () { char str1[15]; char str2[15]; int ret; memcpy(str1, "abcdef", 6); memcpy(str2, "ABCDEF", 6); ret = memcmp(str1, str2, 5); if(ret > 0) { printf("str2 is less than str1"); } else if(ret < 0) { printf("str1 is less than str2"); } else { printf("str1 is equal to str2"); } return(0); }

阅读全文 ↗
C

C库函数 - memchr()

C库函数 - memchr() #include <stdio.h> #include <string.h> int main () { const char str[] = "http://www.codingdict.com"; const char ch = '.'; char *ret; ret = memchr(str, ch, strlen(str)); printf("String after |%c| is - |%s|\n", ch, ret); return(0); }

阅读全文 ↗
C

C库函数 - wctomb()

C库函数 - wctomb() #include <stdio.h> #include <stdlib.h> int main () { int i; wchar_t wc = L'a'; char *pmbnull = NULL; char *pmb = (char *)malloc(sizeof( char )); printf("Converting wide character:\n"); i = wctomb( pmb, wc ); printf("Characters converted: %u\n", i); printf("Multibyte character: %.1s\n", pmb); printf("Trying to convert when target is NULL:\n"); i = wctomb( pmbnull, wc ); printf("Characters converted: %u\n", i); /* this will not print any value */ printf("Multibyte character: %.1s\n", pmbnull); return(0); }

阅读全文 ↗
C

C库函数 - mbtowc()

C库函数 - mbtowc() #include <stdio.h> #include <stdlib.h> #include <string.h> int main () { char *str = "This is codingdict.com"; wchar_t mb[100]; int len; len = mblen(NULL, MB_CUR_MAX); mbtowc(mb, str, len*strlen(str) ); wprintf(L"%ls \n", mb ); return(0); }

阅读全文 ↗
C

C库函数 - mbstowcs()

C库函数 - mbstowcs() #include<stdio.h> #include<stdlib.h> #include<string.h> int main () { int len; char *pmbnull = NULL; char *pmb = (char *)malloc( MB_CUR_MAX ); wchar_t *pwc = L"Hi"; wchar_t *pwcs = (wchar_t *)malloc( sizeof( wchar_t )); printf("Converting to multibyte string\n"); len = wcstombs( pmb, pwc, MB_CUR_MAX); printf("Characters converted %d\n", len); printf("Hex value of first multibyte character: %#.4x\n", pmb); printf("Converting back to Wide-Character string\n"); len = mbstowcs( pwcs, pmb, MB_CUR_MAX); printf("Characters converted %d\n", len); printf("Hex value of first wide character %#.4x\n\n", pwcs); return(0); }

阅读全文 ↗
C

C库函数 - mblen()

C库函数 - mblen() #include <stdio.h> #include <stdlib.h> #include <string.h> int main () { int len; char *pmbnull = NULL; char *pmb = (char *)malloc( MB_CUR_MAX ); wchar_t *pwc = L"Hi"; wchar_t *pwcs = (wchar_t *)malloc( sizeof( wchar_t )); printf("Converting to multibyte string\n"); len = wcstombs( pmb, pwc, MB_CUR_MAX); printf("Characters converted %d\n", len); printf("Hex value of first multibyte character: %#.4x\n", pmb); len = mblen( pmb, MB_CUR_MAX ); printf( "Length in bytes of multibyte character %x: %u\n", pmb, len ); pmb = NULL; len = mblen( pmb, MB_CUR_MAX ); printf( "Length in bytes of multibyte character %x: %u\n", pmb, len ); return(0); }

阅读全文 ↗
C

C库函数 - srand()

C库函数 - srand() #include <stdio.h> #include <stdlib.h> #include <time.h> int main () { int i, n; time_t t; n = 5; /* Intializes random number generator */ srand((unsigned) time(&t)); /* Print 5 random numbers from 0 to 50 */ for( i = 0 ; i < n ; i++ ) { printf("%d\n", rand() % 50); } return(0); }

阅读全文 ↗
C

C库函数 - rand()

C库函数 - rand() #include <stdio.h> #include <stdlib.h> int main () { int i, n; time_t t; n = 5; /* Intializes random number generator */ srand((unsigned) time(&t)); /* Print 5 random numbers from 0 to 49 */ for( i = 0 ; i < n ; i++ ) { printf("%d\n", rand() % 50); } return(0); }

阅读全文 ↗
C

C库函数 - ldiv()

C库函数 - ldiv() #include <stdio.h> #include <stdlib.h> int main () { ldiv_t output; output = ldiv(100000L, 30000L); printf("Quotient = %ld\n", output.quot); printf("Remainder = %ld\n", output.rem); return(0); }

阅读全文 ↗
C

C库函数 - labs()

C库函数 - labs() #include <stdio.h> #include <stdlib.h> int main () { long int a,b; a = labs(65987L); printf("Value of a = %ld\n", a); b = labs(-1005090L); printf("Value of b = %ld\n", b); return(0); }

阅读全文 ↗
C

C库函数 - div()

C库函数 - div() #include <stdio.h> #include <stdlib.h> int main () { div_t output; output = div(27, 4); printf("Quotient part of (27/ 4) = %d\n", output.quot); printf("Remainder part of (27/4) = %d\n", output.rem); output = div(27, 3); printf("Quotient part of (27/ 3) = %d\n", output.quot); printf("Remainder part of (27/3) = %d\n", output.rem); return(0); }

阅读全文 ↗
C

C库函数 - abs()

C库函数 - abs() #include <stdio.h> #include <stdlib.h> int main () { int a, b; a = abs(5); printf("value of a = %d\n", a); b = abs(-10); printf("value of b = %d\n", b); return(0); }

阅读全文 ↗