LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   A simple question on C (https://www.linuxquestions.org/questions/programming-9/a-simple-question-on-c-227983/)

satimis 09-07-2004 10:39 PM

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

tim1235 09-07-2004 10:50 PM

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)

fenderman11111 09-07-2004 10:52 PM

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

fenderman11111 09-07-2004 11:15 PM

dang you just beat me to that one, tim

satimis 09-07-2004 11:15 PM

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

tim1235 09-07-2004 11:31 PM

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.

kingtas 09-08-2004 11:55 PM

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.

itsme86 09-09-2004 12:47 AM

// is standard for C as of the C99 standard.

satimis 09-11-2004 12:15 AM

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

kahn 09-11-2004 01:00 AM

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.?

satimis 09-11-2004 01:07 AM

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

kahn 09-11-2004 01:15 AM

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

satimis 09-11-2004 01:20 AM

Hi kahn,

Noted with tks

satimis

kahn 09-11-2004 01:26 AM

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?

satimis 09-11-2004 03:13 AM

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


All times are GMT -5. The time now is 09:04 AM.