Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

syntax-error

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?

like image 397
Doug Stephen Avatar asked Mar 24 '26 10:03

Doug Stephen


1 Answers

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));
    .
    .
}
like image 153
Jason Scheirer Avatar answered Mar 26 '26 00:03

Jason Scheirer