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;
}

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

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.

Please post the compiler errors, especially with regard to line numbers where the errors are detected.

This is the compiler error:

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

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.

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.