Reputation: 41
Why does this program produce a syntax error when built in Ubuntu?
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "time.h"
#include "sys/types.h"
#include "sys/stat.h"
int main()
{
time_t st_mtime;
printf("Hello\n");
return 0;
}
Here's what I get when I try to build this:
$ gcc -o test1 test1.c
test1.c: In function ?main?:
test1.c:10: error: expected ?=?, ?,?, ?;?, ?asm? or ?__attribute__? before ?.? token
test1.c:10: error: expected expression before ?.? token
Inspecting the preprocessor output:
$ gcc -E test1.c > test1.d
Shows line 10 as:
time_t st_mtim.tv_sec;
The error occurs only if I include both "sys/stat.h" & "time.h" files.
Upvotes: 4
Views: 1118
Reputation: 13994
If you grep
/usr/include
for st_mtime
, you'll find the following:
$ grep -r st_mtime /usr/include | grep define
/usr/include/x86_64-linux-gnu/bits/stat.h:# define st_mtime st_mtim.tv_sec
So the problem stems from your use of the variable name st_mtime
.
Upvotes: 5