读书人

关于struct tm的使用有关问题

发布时间: 2012-12-14 10:33:08 作者: rapoo

关于struct tm的使用问题,在线等


#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
#include<limits.h>
#include<stdarg.h>
#include<assert.h>
#include<time.h>

void main(){

int *a=(int*)calloc(4,2);
system("color 4E");
struct tm *k;
//printf("%d",d.tm_sec);
exit(0);
}

上面的就有错误:error C2143: syntax error : missing ';' before 'type'
注销掉system("color 4E");这句就没有错误


#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
#include<limits.h>
#include<stdarg.h>
#include<assert.h>
#include<time.h>

void main(){

int *a=(int*)calloc(4,2);
//system("color 4E");
struct tm *k;
//printf("%d",d.tm_sec);
exit(0);
}


这是怎么了??????
[最优解释]
找到原因了,是因为C语言要求变量在前面声明:

#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
#include<limits.h>
#include<stdarg.h>
#include<assert.h>
#include<time.h>

int main()
{

int* a = (int*)calloc(4, 2);
struct tm* k;

system("color 4E");
time_t t = time(NULL);
k = localtime(&t);
printf("%d\n", k->tm_sec);

return 0;
}


[其他解释]
GCC下编译没有问题啊,你是什么编译器?还是抄上来的代码不对?
[其他解释]

#include <iostream>
#include <iomanip>
#include <ctime>

int main()
{
std::time_t t = std::time(0);
std::tm* p = std::localtime(&t);
printf("%d",p->tm_sec);
return 0;
}

[其他解释]
试试这个在线编译器http://codepad.org
[其他解释]
C89把?
定义必须在开始部分

int *a=(int*)calloc(4,2);
struct tm *k;
system("color 4E");
这样应该就可以了
[其他解释]
引用:
找到原因了,是因为C语言要求变量在前面声明:
C/C++ code

#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
#include<limits.h>
#include<stdarg.h>
#include<assert.h>
#include<time.h>

int main()
{

int* a = (int*……

呜呜呜,一直用c++,突然转回来就懵了。。。。。。。
谢谢了

读书人网 >C语言

热点推荐