LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 03-11-2004, 09:54 AM   #1
pkashyap
LQ Newbie
 
Registered: Mar 2004
Location: Bangalore, INDIA
Posts: 2

Rep: Reputation: 0
Question Error for a very simple C program


Hi there,

I am a newbie to Linux. The objective of mine is to learn programming on Linux and get rid of Windows. I hate Windows.

In LINUX Answers section of this forum, a simple program of C is published. The program does just a simple printf of some text and nothing else. I am getting an error in Linux.

I have Linux red hat 9. To cross check if C compiler is present, I did a
" which gcc " command in the terminal. I got a reply /usr/bin/gcc . I assume that this means that the compiler is present on the system.

On writing the simple following program:

# include<stdio.h>

main()
{
printf("Linuxquestions.org\n");
}

I get an error which says :

bash:syntax error near unexpected token '(printf '

I wonder what this means. Help on this regard would be appreciated.

Also, it would be great if people can suggest some good books to buy for programming C on Linux.

Thanks in advance for your help.

Cheers

PK

P.S : I have logged on as localhost : root. Could this be a reason for the error? Just thinking aloud ..................
 
Old 03-11-2004, 10:08 AM   #2
skel
Member
 
Registered: Jan 2004
Distribution: Slackware 10 ; Gentoo ; OpenBSD 3.6
Posts: 101

Rep: Reputation: 15
you have to compile the source code

gcc -o executable filename.c

replace executable with the name you want and filename with the name of the source file.

and then run ./executable

What you are trying to do is run the source code as an executable. C is a compiled language, so you have to run gcc on your source code to turn it into machine language

skel
 
Old 03-11-2004, 11:24 AM   #3
Mega Man X
LQ Guru
 
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Rep: Reputation: 64
Code:
# include<stdio.h>

main()
{
    printf("Linuxquestions.org\n");
}
You do, realize that all functions in C or C++ has to return a value, unless it's declared as void. main() is not an exception.

Code:
# include<stdio.h>

main()
{
    printf("Linuxquestions.org\n");
    return 0;
}
By default, all functions without explicit declaration, will be understood for the compiler as integer. You could declare it as void:
Code:
# include<stdio.h>

void main()
{
    printf("Linuxquestions.org\n");
}
and then you don't need to return any value. It's a good programing style to always declare a function explicitly.

Code:
# include<stdio.h>

int main()
{
    printf("Linuxquestions.org\n");
    return 0;
}
Regards!

Last edited by Mega Man X; 03-11-2004 at 11:26 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Simple C program - error with float J_K9 Programming 2 10-24-2005 12:36 PM
Simple C pow() program, strange error funkymunky Programming 2 06-25-2004 05:20 AM
A simple program on C++ for the linux with an error. Israfel2000 Programming 4 06-16-2004 07:25 PM
simple c program liguorir Linux - Software 4 05-29-2004 07:22 AM
simple program bprasanth_20 Programming 5 10-29-2003 01:55 AM


All times are GMT -5. The time now is 10:50 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration