编程札记

C

C库函数 time()

C库函数 time() #include <stdio.h> #include <time.h> int main () { time_t seconds; seconds = time(NULL); printf("Hours since January 1, 1970 = %ld\n", seconds/3600); return(0); }

阅读全文 ↗
C

C库函数 strftime()

C库函数 strftime() #include <stdio.h> #include <time.h> int main () { time_t rawtime; struct tm *info; char buffer[80]; time( &rawtime ); info = localtime( &rawtime ); strftime(buffer,80,"%x - %I:%M%p", info); printf("Formatted date & time : |%s|\n", buffer ); return(0); }

阅读全文 ↗
C

C库函数 mktime()

C库函数 mktime() #include <stdio.h> #include <time.h> int main () { int ret; struct tm info; char buffer[80]; info.tm_year = 2001 - 1900; info.tm_mon = 7 - 1; info.tm_mday = 4; info.tm_hour = 0; info.tm_min = 0; info.tm_sec = 1; info.tm_isdst = -1; ret = mktime(&info); if( ret == -1 ) { printf("Error: unable to make time using mktime\n"); } else { strftime(buffer, sizeof(buffer), "%c", &info ); printf(buffer); } return(0); }

阅读全文 ↗
C

C库函数 localtime()

C库函数 localtime() #include <stdio.h> #include <time.h> int main () { time_t rawtime; struct tm *info; char buffer[80]; time( &rawtime ); info = localtime( &rawtime ); printf("Current local time and date: %s", asctime(info)); return(0); }

阅读全文 ↗
C

C库函数 gmtime()

C库函数 gmtime() #include <stdio.h> #include <time.h> #define BST (+1) #define CCT (+8) int main () { time_t rawtime; struct tm *info; time(&rawtime); /* Get GMT time */ info = gmtime(&rawtime ); printf("Current world clock:\n"); printf("London : %2d:%02d\n", (info->tm_hour+BST)%24, info->tm_min); printf("China : %2d:%02d\n", (info->tm_hour+CCT)%24, info->tm_min); return(0); }

阅读全文 ↗
C

C库函数 difftime()

C库函数 difftime() #include <stdio.h> #include <time.h> int main () { time_t start_t, end_t; double diff_t; printf("Starting of the program...\n"); time(&start_t); printf("Sleeping for 5 seconds...\n"); sleep(5); time(&end_t); diff_t = difftime(end_t, start_t); printf("Execution time = %f\n", diff_t); printf("Exiting of the program...\n"); return(0); }

阅读全文 ↗
C

C库函数 ctime()

C库函数 ctime() #include <stdio.h> #include <time.h> int main () { time_t curtime; time(&curtime); printf("Current time = %s", ctime(&curtime)); return(0); }

阅读全文 ↗
C

C库函数 clock()

C库函数 clock() #include <time.h> #include <stdio.h> int main () { clock_t start_t, end_t, total_t; int i; start_t = clock(); printf("Starting of the program, start_t = %ld\n", start_t); printf("Going to scan a big loop, start_t = %ld\n", start_t); for(i=0; i< 10000000; i++) { } end_t = clock(); printf("End of the big loop, end_t = %ld\n", end_t); total_t = (double)(end_t - start_t) / CLOCKS_PER_SEC; printf("Total time taken by CPU: %f\n", total_t ); printf("Exiting of the program...\n"); return(0); }

阅读全文 ↗
C

C库函数 asctime()

C库函数 asctime() #include <stdio.h> #include <string.h> #include <time.h> int main () { struct tm t; t.tm_sec = 10; t.tm_min = 10; t.tm_hour = 6; t.tm_mday = 25; t.tm_mon = 2; t.tm_year = 89; t.tm_wday = 6; puts(asctime(&t)); return(0); }

阅读全文 ↗
C

C库函数 strxfrm()

C库函数 strxfrm() #include <stdio.h> #include <string.h> int main () { char dest[20]; char src[20]; int len; strcpy(src, "Tutorials Point"); len = strxfrm(dest, src, 20); printf("Length of string |%s| is: |%d|", dest, len); return(0); }

阅读全文 ↗
C

C库函数 strtok()

C库函数 strtok() #include <string.h> #include <stdio.h> int main () { char str[80] = "This is - www.codingdict.com - website"; const char s[2] = "-"; char *token; /* get the first token */ token = strtok(str, s); /* walk through other tokens */ while( token != NULL ) { printf( " %s\n", token ); token = strtok(NULL, s); } return(0); }

阅读全文 ↗
C

C库函数 strstr()

C库函数 strstr() #include <stdio.h> #include <string.h> int main () { const char haystack[20] = "CodingDict"; const char needle[10] = "Point"; char *ret; ret = strstr(haystack, needle); printf("The substring is: %s\n", ret); return(0); }

阅读全文 ↗