Program --- C language details 17 (calculate the maximum value of time_t, strlen calculates the length, malloc allocates characters memory details, and switch's default details)
Main Content: calculate the maximum value of time_t, strlen evaluate the length, malloc allocate character memory details, switch default details
# Include
# Include
Int main () {/************************************** * ************************* time_t maximum value test ********** **************************************** * **************/time_t biggest = 0x7fffff; printf ("biggest = % s", ctime (& biggest);/* The ctime () function converts the parameter to the local time. Here is a detail. I have no line breaks in printf, however, the output has a line break. The following test ctime (& biggest) returns the string containing '\ n' indeed */char * test = ctime (& biggest ); while (* test ++) {if (* test = '\ n') printf ("find '\\ N' int test \ n ");} // printf (" biggest = % s \ n ", asctime (gmtime (& biggest ))); // The correct statement is this sentence, the above statement has a time difference problem /********************************* * ******************************* strlen () function Testing ************************************** * **************************/char * a = "abc "; printf ("strlen = % d \ n", strlen (a); // strlen evaluate the string length, excluding the most '\ 0' malloc (strlen ()); // This is not correct, because malloc (strlen (a) + 1) should be used; memory for '\ 0' to be applied /************ **************************************** * ************ Switch test result description: default can be placed anywhere in the switch ********************************* * *******************************/int B = 2; const int c = 2; // Add case c: printf ("c") to the switch. You can test that the c modified by const is not a constant switch (B) {case 0: printf ("case: 0 \ n"); break; case 1: printf ("case: 1 \ n"); break; default: printf ("default \ n "); break; case 2: printf ("case: 2 \ n"); while (B = 2) {printf ("B = 1 \ n"); break; // break; used to jump out of the nearest loop or switch statement, then continue executing} printf ("after break, the program run here! \ N "); break;} return 0 ;}
Output: