LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-12-2009, 09:48 AM   #1
rmnature
LQ Newbie
 
Registered: Feb 2009
Posts: 5

Rep: Reputation: 0
Question syntax error near unexpected token -what does this mean?


Hi all,

I tried running the simplest c-programme(filename is firstc.c)
as follows

#include<stdio.h>

main()

{ printf("abcd");
}

and I compiled it using command gcc -o firstc firstc.c,it gets compiled and when i run it i get the following error,i don't understand,how could a simple programme such as this be erraneous,someone help sort this out.

The Error Message is:
./firstc.c: line 5: syntax error near unexpected token `"abcd"'
./firstc.c: line 5: `{ printf("abcd");'

Last edited by rmnature; 02-13-2009 at 02:05 AM.
 
Old 02-12-2009, 10:05 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Such a simple code cannot have errors! You are trying to execute the source code, not the executable. The error message is a typical error of the SHELL not of the C programming language. You have to do
Code:
./first
and not
Code:
./first.c
Anyway... welcome to LQ!
 
Old 02-13-2009, 02:25 AM   #3
rmnature
LQ Newbie
 
Registered: Feb 2009
Posts: 5

Original Poster
Rep: Reputation: 0
Thanks Colucix

Thanks,now it works,but could you explain what that error message actually conveys,what does it mean?
 
Old 02-13-2009, 02:32 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Code:
syntax error near unexpected token `"abcd"'
Due to the syntax rules of BASH your program is interpreted in the following way:
Code:
#include<stdio.h>       <----- This is a comment in bash

main()                  <----- This is a function definition

{ printf("abcd");       <----- This is the printf command with erroneous syntax
                               since you cannot put parentheses
}
The shell expected something after printf, but found a parenthesis followed by "abcd"... that is an unexpected token.
 
Old 02-14-2009, 07:58 AM   #5
rmnature
LQ Newbie
 
Registered: Feb 2009
Posts: 5

Original Poster
Rep: Reputation: 0
Thanks,but you told me that there is no problem with the code,and how could putting parantheses near printf cause syntax error?
 
Old 02-14-2009, 08:19 AM   #6
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
If you read the answer from colucix, you will see that he has explained that when you tried to execute a C source file with BASH, it was unable to accept the syntax. That is because C source files are not intended to be interpreted by BASH, but by a C compiler. The C compiler converts the source code into binary object code which BASH loads and passes execution to. When you compile the source code, the C compiler accepts the syntax without complaint because it is valid C code.
--- rod.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
syntax error near unexpected token Aigarzs Linux - Newbie 3 01-12-2008 03:39 PM
syntax error near unexpected token deskjockey39 Linux - General 2 01-16-2007 08:47 AM
syntax error for unexpected token `(' Steve Spurr Linux - Newbie 6 09-22-2006 08:19 AM
syntax error near unexpected token `else' josedias Programming 3 09-11-2006 07:09 PM
syntax error near unexpected token ` mattyspatty Programming 8 05-07-2006 05:19 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:18 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration