From: Kbam7 <kyl...@gm...> - 2015-12-20 08:07:53
|
Hi there, I have been learning C for the past month now and I have managed to set up a local environment to compile and execute basic programs as I go along with the C tutorial I am following. Everything was working fine until yesterday. Suddenly, for a reason unknown to me, the compiler started to give me the following error: < -------------------------------------- ./hello.c: line 3: syntax error near unexpected token '(' ./hello.c: line 3: 'int main ()' -------------------------------------- > The file hello.c contains basic code to print "Hello World!" in the command prompt window. The code is pasted below for your reference: < -------------------------------------- #include <stdio.h> int main() { /* Print "Hello World" on screen */ printf("Hello, World!\n"); return 0; } -------------------------------------- > As you can see there is nothing wrong with the code. So for some reason, something went wrong with MinGW i think. I have done a search on Google and ended up at this post on Stack Exchange <http://stackoverflow.com/questions/17585748/unexpected-syntax-error-from-mingw-compiling-a-simple-c-program> but I did not find a solution there either. One person there suggested the use of Dev-C++ which I downloaded and installed but I have no clue what to do with it. I am using the Windows 8 OS and have installed MinGW and MSYS. When I open MSYS, it opens the command prompt window with MinGW32 in the window title. I then type 'cd /mysources' which takes me to the required directory and the I type 'gcc hello.c -o hello' and then the program is compiled. I then type 'hello' and press enter which executes the hello.exe and should print "Hello World!" in the command prompt window. Could anyone assist me in any way with this problem? Thanks in advance! -- View this message in context: http://mingw-users.1079350.n2.nabble.com/MinGW-syntax-error-near-unexpected-token-tp7583761.html Sent from the MinGW-users mailing list archive at Nabble.com. |
From: Benjamin R. <b.r...@tu...> - 2015-12-20 11:44:10
|
Hi Kbam7, Kbam7 writes: > ./hello.c: line 3: syntax error near unexpected token '(' > ./hello.c: line 3: 'int main ()' > >[...] > > #include <stdio.h> > > int main() > { > /* Print "Hello World" on screen */ > printf("Hello, World!\n"); > > return 0; > } The last thing before the line with the error is the include. Is it possible that you have looked at the compiler headers with your editor and inadvertantly changed something there? I have that problem from time to time. Maybe it's not in the file stdio.h itself, system headers are usually a tangled mess of includes, sub-includes and sub-sub-includes (not a style to be emulated ;-(). benny |
From: Keith M. <kei...@us...> - 2015-12-20 12:46:18
Attachments:
signature.asc
|
On 20/12/15 08:07, Kbam7 wrote: > I have been learning C for the past month now and I have managed to set up a > local environment to compile and execute basic programs as I go along with > the C tutorial I am following. Everything was working fine until yesterday. > Suddenly, for a reason unknown to me, the compiler started to give me the > following error: > > ./hello.c: line 3: syntax error near unexpected token '(' > ./hello.c: line 3: 'int main ()' This doesn't look like an error reported by the compiler; it DOES look EXACTLY like the error the bash shell would produce, if you were to attempt to execute trivial C source as a shell script[*]: $ cat foo.c #include <stdio.h> int main (){ return 0; } $ chmod +x foo.c $ ./foo.c ./foo.c: line 2: syntax error near unexpected token `(' ./foo.c: line 2: `int main (){ return 0; }' [*] This is run in bash shell on Linux, hence the 'chmod +x foo.c' to (bogusly) make the C source APPEAR to be executable. > The file hello.c contains basic code to print "Hello World!" in the command > prompt window. The code is pasted below for your reference: > > < -------------------------------------- > > #include <stdio.h> > > int main() > { > /* Print "Hello World" on screen */ > printf("Hello, World!\n"); > > return 0; > } > > -------------------------------------- > > > As you can see there is nothing wrong with the code. No, there doesn't appear to be. However, my example is a reduced equivalent, insofar as it exhibits identical incorrect behaviour when it is used inappropriately, as I have suggested. You need to paste the EXACT sequence of commands you used, to confirm whether or not you have misused your source in an equivalent manner, in bash shell in MSYS. > So for some reason, something went wrong with MinGW i think. I think not! Why would you think this, (unless YOU have modified your MinGW installation in some detrimental way)? Software isn't subject to mechanical wear-and-tear, which might degrade its behaviour over time; given identical conditions of use, it will produce identically the same results time after time. > I have done a search on Google [finding this] on Stack Exchange > <http://stackoverflow.com/questions/17585748/unexpected-syntax-error-from-mingw-compiling-a-simple-c-program> > but I did not find a solution there either. Be VERY cautious, and indeed sceptical, with advice on Stack Exchange; there may be the occasional pearl of wisdom, but they tend to be bogged down in a quagmire of misconception, ill advised, and often just plain incorrect... > One person there suggested the use of Dev-C++ which I downloaded and > installed but I have no clue what to do with it. ...and this is just typical of the inappropriateness of much of the advice you will find there. No doubt, Dev-C++ may be a good product, but advising you to use it in no way actually addresses the issue at hand, so it most definitely is NOT a "solution". -- Regards, Keith. Public key available from keys.gnupg.net Key fingerprint: C19E C018 1547 DE50 E1D4 8F53 C0AD 36C6 347E 5A3F |