Linux有一些C語言的用法並不是標準C語言,其定義在下面文件
https://gcc.gnu.org/onlinedocs/gcc/C-Extensions.html
6.29 Designated Initializers
https://gcc.gnu.org/onlinedocs/gcc/Designated-Inits.html
#include <stdio.h> #include <string.h> #include <stdlib.h> int main(int argc, char **argv) { const char * const str = "MAC-:AA-BB-CC-DD-EE-FF"; const char * const delimitation = "-:"; char * const dupstr = strdup(str); char *saveptr = NULL, *substr = NULL; int i = 0; printf("original: %s (%p)\n", dupstr, dupstr); substr = strtok_r(dupstr, delimitation, &saveptr); do { printf("#%d after parsing: %s (%p)\n", i++, substr, substr); substr = strtok_r(NULL, delimitation, &saveptr); } while (substr); } /* [Result] original: MAC-:AA-BB-CC-DD-EE-FF (0x55f73e1e42a0) #0 after parsing: MAC (0x55f73e1e42a0) #1 after parsing: AA (0x55f73e1e42a5) #2 after parsing: BB (0x55f73e1e42a8) #3 after parsing: CC (0x55f73e1e42ab) #4 after parsing: DD (0x55f73e1e42ae) #5 after parsing: EE (0x55f73e1e42b1) #6 after parsing: FF (0x55f73e1e42b4) */
#include <stdio.h> #include <string.h> #include <stdlib.h> int main() { int day, year; char weekday[20], month[20], tmp[100]; strcpy(tmp, "Saturday March aaa 25 1999"); /*"%*s" means ignore aaa*/ sscanf(tmp, "%s %s %*s %d %d", weekday, month, &day, &year); printf("%s %d %d %s", month, day, year, weekday); return(0); } /* [Result] March 25 1999 Saturday */