Using certain functions from stdlib.h or stdio.h causes syntax errors

*爱你&永不变心* 提交于 2020-01-04 01:16:28

问题


I'm working on some C code in Visual Studio 2005 on Win7 Pro x64. The code is correct; it compiles and runs on MinGW under Eclipse. However, using certain functions from the standard C libraries like stdio or stdlib causes the following lines to exhibit syntax errors when the code is built in VS2005. As an example:

#include<time.h>
#include<stdlib.h>
#include<stdio.h>
#include"someOtherHeader.h"

int main(void){
    srand((unsigned int) time(NULL));
    double start;
.
.
.

The following code doesn't matter. VS2005 says that there is a missing ';' before 'type'. Commenting out srand() fixes the problem. Oddly, when rand() is called later, there is no problem. I also noticed the behavior with exit() and fprint(). But not with malloc(). Thoughts?


回答1:


Using C in Visual Studio puts the compiler into strict (old school C) mode. All your declarations have to be at the beginning of your blocks:

#include<time.h>
#include<stdlib.h>
#include<stdio.h>
#include"someOtherHeader.h"

int main(void){
    double start;
    srand((unsigned int) time(NULL));
    .
    .
}



回答2:


Visual Studio supports NOT C99 (just a little bit)



来源:https://stackoverflow.com/questions/3409299/using-certain-functions-from-stdlib-h-or-stdio-h-causes-syntax-errors

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!