标签:c语言 switch的default time_t最大值 malloc分配字符串
主要内容:求time_t的最大值、strlen求的是长度、malloc分配字符内存细节、switch的中default细节
#include <stdio.h>
#include <time.h>
int main()
{
/*****************************************************************
time_t最大值测试
******************************************************************/
time_t biggest = 0x7fffffff;
printf("biggest = %s", ctime(&biggest)); /* ctime()函数把参数转换为当地时间,这里有一个细节,printf中我没有换行,但是输出却换行了,经下面测试ctime(&biggest)返回字符串中含有'\n'确实*/
char *test = ctime(&biggest);
while(*test++)
{
if (*test == '\n')
printf("find '\\n' int test\n");
}
// printf("biggest = %s \n", asctime(gmtime(&biggest))); // 正确语句是这句,上面语句有个时差问题
/*****************************************************************
strlen()函数测试
******************************************************************/
char *a = "abc";
printf("strlen = %d\n",strlen(a)); // strlen求字符串长度,不包含最有的'\0'
malloc(strlen(a));// 这是不对的,因该用malloc(strlen(a)+1);要申请'\0' 的内存
/*****************************************************************
switch测试
结果说明:default可以放在switch中的任何位置
******************************************************************/
int b = 2;
const int c = 2; // 在switch中添加case c: printf("c"); 可以测试const修饰的c不是常量
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;用于跳出最近的那层循环或者switch语句,在这里跳出循环,然后继续往下执行
}
printf("after break,the program run here!\n ");
break;
}
return 0;
}
程序猿之---C语言细节17(求time_t的最大值、strlen求的是长度、malloc分配字符内存细节、switch的中default细节)
标签:c语言 switch的default time_t最大值 malloc分配字符串
原文地址:http://blog.csdn.net/human_evolution/article/details/41014569