LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-07-2004, 11:39 PM   #1
satimis
Senior Member
 
Registered: Apr 2003
Posts: 3,673

Rep: Reputation: 55
A simple question on C


Hi folks,

FedoraCore 2

I just began to learn C and met following problem.


Using 'emacs' to create a file on /home/user/hello.c with following content;
Code:
#include < stdio.h>
void main()
{
       printf("\nHello World\n");
}
Compliling it with gcc

$ gcc /home/user/hello.c

(Also tried as root
#gcc /home/user/hello.c

following warning popup;
/home/user/hello.c:1:20: stdio.h:No such file or directory
/home/user/hello.c: In function `main':
/home/user/hello.c:4 Warning:return type of main' is not `int'

# find / -name a.out

Can't find the executable file 'a.out'

# find/ -name stdio.h

found many /stdio.h
.....

Please advise what is the problem and how to rectify it. TIA

B.R.
satimis

Last edited by satimis; 09-07-2004 at 11:51 PM.
 
Old 09-07-2004, 11:50 PM   #2
tim1235
Member
 
Registered: Aug 2004
Location: Melbourne, Australia
Distribution: fc5/Gentoo
Posts: 57

Rep: Reputation: 15
Code:
#include < stdio.h>
void main()
{
       printf("\nHello World\n");
}
Firstly get rid of the space in < stdio.h> it should be <stdio.h>
That will fix you library problem.

Second change void to int as void is c++ (I could be mistake correct me if I am wrong)
Also add in return 0; at the end for good programming practice (As program will now be expecting a return type of int)
 
Old 09-07-2004, 11:52 PM   #3
fenderman11111
Member
 
Registered: Jun 2004
Location: Denton
Distribution: debian, freebsd
Posts: 121

Rep: Reputation: 15
I'm not a c person really...

i would try using 'int main()' and doing 'return 0;' as your last statement

anyway I think that this is more ... uh ... standard I guess? but its good to return something
 
Old 09-08-2004, 12:15 AM   #4
fenderman11111
Member
 
Registered: Jun 2004
Location: Denton
Distribution: debian, freebsd
Posts: 121

Rep: Reputation: 15
dang you just beat me to that one, tim
 
Old 09-08-2004, 12:15 AM   #5
satimis
Senior Member
 
Registered: Apr 2003
Posts: 3,673

Original Poster
Rep: Reputation: 55
Hi tim1235 and fenderman11111

Tks for your advice.

Please provide more detail as I'm only a biginner. Tks.

Quote:
Firstly get rid of the space in < stdio.h> it should be <stdio.h>
That will fix you library problem.

Noted with thanks.

Quote:
Second change void to int as void is c++ (I could be mistake correct me if I am wrong)
Is it as follow;

int main()

instead of "void main()"

Quote:
Also add in return 0; at the end for good programming practice (As program will now be expecting a return type of int)
Please advise how to make it.

Tks.

B.R.
satimis

Last edited by satimis; 09-08-2004 at 12:16 AM.
 
Old 09-08-2004, 12:31 AM   #6
tim1235
Member
 
Registered: Aug 2004
Location: Melbourne, Australia
Distribution: fc5/Gentoo
Posts: 57

Rep: Reputation: 15
Quote:
Is it as follow;

int main()

instead of "void main()"
You got it!!

Your program should now look something like this:

Code:
#include <stdio.h>
int main()
{
       printf("\nHello World\n");
        return 0;   //(Program finished normally!)
}
The return 0; tell us the the program finished without any errors. This is for debuggin purposes as if an error occurs we can use exit 1; which tell our program to quit and return 1. So if we were using our program in a shell script and the program finishes with no errors (return 0) we can do something or if it returns with errors (return > 0) we can do something else or warn the user.
 
Old 09-09-2004, 12:55 AM   #7
kingtas
Member
 
Registered: Sep 2004
Distribution: RH9
Posts: 36

Rep: Reputation: 15
I'm a Linux newbie and certainly not a pro at C, but looking through these boards took me back to my school days in C.

In C the function would look like this to declare a void return is expected:

int main(void){
printf("\nHello World\n");
return 0;
}


One of my instructors actually deducted 1 point from a project because I used // instead of /* */ for comments because that's C++ and not C. What a guy.
 
Old 09-09-2004, 01:47 AM   #8
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
// is standard for C as of the C99 standard.
 
Old 09-11-2004, 01:15 AM   #9
satimis
Senior Member
 
Registered: Apr 2003
Posts: 3,673

Original Poster
Rep: Reputation: 55
Hi folks,

Tks for your advice.

Both advice of tim1235 and kingtas work with similar result.

One thing I can't resolve. I must provide full path to call 'a.out'

1)
[root@localhost user]# /home/user/a.out

Hello World

2)
[root@localhost user]# a.out
-bash: a.out: command not found

Can't work.


Furthermore kindly to help me understand

# gcc sine.c -lm

What are the options '-lm' for???


Besides I follow following 2 documents found on Internet to learn C Language

C Programming
C Language Tutorial
http://www.strath.ac.uk/IT/Docs/Ccourse/

(Remark: I'm using FC2 box to learn C Language. Although I have another FreeBSD box but it runs on a slower machine)

Can you folks please recommend other documents available on Internet?

TIA.

B.R.
satimis
 
Old 09-11-2004, 02:00 AM   #10
kahn
Member
 
Registered: Feb 2004
Location: Seattle
Distribution: FC5
Posts: 109

Rep: Reputation: 15
lol, it doesnt make much sense?

i just started learning c as well... i compiled the hello world program and it compiled without errors, but when i ran helloworld(chosen output file) it said bash not found. now when i read this post, i noted the whole directory...

even when i was in same directory /home/can/source/

it wouldnt run. but when i typed in the whole directory w/ helloworld it ran?

that doesnt make much sense.?
 
Old 09-11-2004, 02:07 AM   #11
satimis
Senior Member
 
Registered: Apr 2003
Posts: 3,673

Original Poster
Rep: Reputation: 55
Quote:
Originally posted by kahn
even when i was in same directory /home/can/source/

it wouldnt run. but when i typed in the whole directory w/ helloworld it ran?

that doesnt make much sense.?
Hi kahn,

I got the querry as yours. I must run 'a.out' with its full path even already in its directory/folder

B.R.
satimis
 
Old 09-11-2004, 02:15 AM   #12
kahn
Member
 
Registered: Feb 2004
Location: Seattle
Distribution: FC5
Posts: 109

Rep: Reputation: 15
you can pick the file you want to compile too...

simply use

gcc helloworld.c -o hello

then run the /home/blah/blah/hello

no more a.out
 
Old 09-11-2004, 02:20 AM   #13
satimis
Senior Member
 
Registered: Apr 2003
Posts: 3,673

Original Poster
Rep: Reputation: 55
Hi kahn,

Noted with tks

satimis
 
Old 09-11-2004, 02:26 AM   #14
kahn
Member
 
Registered: Feb 2004
Location: Seattle
Distribution: FC5
Posts: 109

Rep: Reputation: 15
does anybody know what (no newline at end of file) means?

a lot of my programs give me this warning, but they still compile anyway?
 
Old 09-11-2004, 04:13 AM   #15
satimis
Senior Member
 
Registered: Apr 2003
Posts: 3,673

Original Poster
Rep: Reputation: 55
Hi kahn,

I found answer to following question;

Quote:
... i compiled the hello world program and it compiled without errors, but when i ran helloworld(chosen output file) it said bash not found. now when i read this post, i noted the whole directory...

even when i was in same directory /home/can/source/...
on the same directory run following command

./hello

then it works

B.R.
satimis
 
  


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
Ubuntu Fluxbox simple question, simple answer? generallimptoes Linux - Software 3 09-26-2005 03:03 PM
Installing Programs - A simple question from my simple mind jmp875 Linux - Newbie 6 02-18-2004 10:03 PM
Hopefully a Simple Question Arcane Kidd Linux - Newbie 5 01-17-2003 05:27 PM
Really Simple Question -- Please help! Cia420 Linux - General 3 05-07-2002 07:42 AM
simple question seeking simple answer enzo250gto Linux - Newbie 1 10-27-2001 05:08 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:28 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration