I'm getting a syntax error that's confusing me beyond belief. I'm using MinGW on Windows XP(32-bit), and Notepad++ for a text editor.

"Syntax error before unexpected token '('"

Originally, I thought it may have been something in the code I was trying to compile, so I wrote a simple "hello world" program, but I'm still getting it even though '(' is right where it should be.

#include <stdio.h>
#include <stdlib.h>

int main(void)
    FILE *fp;
    char data;

    if(argc >= 2)
        fp = fopen("hello.txt", "r+");            

    if (fp==NULL) /* error opening file returns NULL */   
        printf("Could not open player file!\n"); /* error message */
        return 1; /* exit with failure */

    while(fgets(data = getchar()) != EOF)
        fgets(data, sizeof(data), fp);

    /* while we're not at end of file */
    while (fgets(data, sizeof(string), fp) != NULL)
        printf("%s", data); /* print the string */    

    fclose(fp); /* close the file */
    return 0; /* success */

//hello world source code below
#include <stdio.h>

int main(void)
    printf("Hello world!");
    return 0;

Edited by jd217: Pop-up blocker needed turned off to submit the code samples.

3 Years
Discussion Span
Last Post by jd217
Featured Replies
  • line 20: fgets() parameters are all wrong. Read the description for that function [here](http://www.cplusplus.com/reference/cstdio/fgets/). I think you should just delete lines 20-23. Read More


line 20: fgets() parameters are all wrong. Read the description for that function here. I think you should just delete lines 20-23.

Edited by Ancient Dragon


Thanks for pointing that out. I changed the parameters, but I'm still getting that same syntax error. I've tried compiling different source files with different code, including the hello world example which compiled using void main(void) but not with int main(void). I wrote a copy of my file I/O code using void main(void), but that didn't fix my issue.

I'm almost ready to uninstall my compiler and reinstall it to see if that helps things any.


This is the compiler error:

hello.c: line 3: syntax error near unexpected token '('
hello.c: line 3: 'int main(void)'

Are you trying to compile that as one file. You got two int mains in there.

Also if you're using arguments don't you have to specify int main as:

int main( int argc, char* argv[] )

Personally, given your example I don't see the point.

Lastly, why the awkward setup? If you're a newbie leave the command line compilation till later.

Consider grabbing yourself an ide like dev-cpp or code blocks.

Edited by iamthwee


No, I'm not, that second int main() is from the source file for a "hello world" program which was also failing to compile. I have since figured out what my issue was. My issue was with some undefined behavior provoked by the fgets() function. At the time I was having the error no code compiled, regardless of whether that function was in it or not, and I have since removed the fgets() function from my source, and after allowing my computer to rest, the compiler now throws normal errors, i.e. initialization makes integer from pointer without a cast, if it throws any at all.

Edited by jd217: Needed to make a clarification.


I'm not a newbie to C, it's just been a few years since I've worked with it, though I'll admit I've never been an expert on the language. I predominantly work with php and ActionScript 3.0 anymore, and a little Java here and there if I need it.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.