Click to See Complete Forum and Search --> : A simple question on C


satimis
09-07-2004, 11:43 PM
Hi folks,

Fedora Core 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;
#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

Fryguy8
09-08-2004, 12:01 AM
don't have a c compiler around right now, but I don't think you can have the leading space in < stdio.h>

duncanbojangles
09-08-2004, 12:13 AM
It's usually safe to assume you'll be using these header files in everything you'll do in C, so you might as well include these:

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

The first one is the standard library, which is just what its name implies: the standard. So include it everytime. The second one handles standard input and output, which you'll probably be using in every program you ever write, so include that one, too. The third is the unix standard, and since you're on Linux, include it too. I

Also, you can't have spaces between the <'s and >'s in the #include statements, so make sure they look like what I posted.

Since I've posted a lot about standards, you should make the function main() have the correct arguments and return type. So, instead, use this:

int main(int argc, char* argv[])

This is used because the shell, bash most likey in your case, sends info to the program when you run it from the command line. argc is a variable of type integer that tells your program how many arguments you passed along to your program from the command line. argv is an array of C strings that hold that information that you pass to your program from the command line.

So, if you type in "./hello foo1 foo2", then argc will be 3, because ./hello is one, foo1 makes 2, and foo2 makes 3 strings on the command line. argv*[0] will contain "./hello", argv*[1] will contain "foo1", and argv*[2] will contain "foo2".

Also, since the return type of your main function is now int, like I wrote in the above paragraphs, you'll need to return a value of type integer. So, to make things simple, here's what you type as the last line before the ending semicolon of your main function:

return 0;

Also, don't forget to put a newline at the end of your hello.c function. To do that, after the last line in your source code, hit enter to leave a blank line. gcc likes it that way.

So, this is what your code should look like:

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

int main(int argc, char* argv[])
{
printf("\nHello, oh helpful and generous world!\n");
printf("\nPosted In Memory of MDWatts.\n");
return 0;
}

Don't forget the newline at the end of the file. And that's it! Somebody probably already posted a one line reply, so I'll end it here.

satimis
09-11-2004, 02:19 AM
Hi folks,

Tks for your advice, particularly,
duncanbojangles's detail one.

Problem solved now.

One thing I can't resolve. I must provide full path to run '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 to 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 folks on the forum please recommend other documents available on Internet?

TIA.

B.R.
satimis

fatTrav
09-11-2004, 02:51 AM
try ./a.out

also the gcc has a -o switch to designate the outfile

gcc something.c -o something.out

HTH

Oh you have Kernigan and Ritchie's C Language book ...that's about all you need to learn C. Just follow the book and work out the examples then just start writing programs in C. Bill Gates once said the best way to become a programmer was to study great programs and to write programs. It could possibly be the only worthwhile thing ever uttered by him regarding computers.

nabis
09-11-2004, 03:05 AM
1) 2) From the same directory ./a.out
3) (gcc sine.c -lm) man gcc :) while in man, do the search:

/^[ ]*-l

I came up with


-llibrary
Use the library named library when linking.
-lobjc You need this special case of the -l option in or-
der to link an Objective C program.

I think you don't need that -lm switch, just the -o one:
gcc hello.c -o hello

I often use a simple bash function (put it in .bashrc):

c ()
{
gcc -Wall "$1" -o "${1%.c}"
}

c hello.c
./hello

http://www.eskimo.com/~scs/
http://users.powernet.co.uk/eton/kandr2/
http://users.powernet.co.uk/eton/index.html
http://www.lysator.liu.se/c/c-www.html (lots of links)

satimis
09-11-2004, 03:39 AM
Hi fatTrav,

]try ./a.out
also the gcc has a -o switch to designate the outfile

gcc something.c -o something.out

Oh you have Kernigan and Ritchie's C Language book ... Noted with thanks,

I haven't got their C Language book. All the document available to me were mentioned in my previous posting.

B.R.
satimis

satimis
09-11-2004, 04:07 AM
Hi nabis,

Tks for your advice and URLs

On "man gcc'

/^[ ]*-l
-llibrary
-l library
Search the library named library when linking. (The second alter-
native with the library as a separate argument is only for POSIX
compliance and is not recommended.)

It makes a difference where in the command you write this option;
the linker searches and processes libraries and object files in the
order they are specified. Thus, foo.o -lz bar.o searches library z
after file foo.o but before bar.o. If bar.o refers to functions in
z, those functions may not be loaded.
......

-lobjc
You need this special case of the -l option in order to link an
Objective-C program.The output looks something different from yours. What is the action of the command "^[ ]*-l"

I think you don't need that -lm switch, just the -o one:
gcc hello.c -o hello.....
Sorry for not explaining clear on my previous posting. I worked on another example on 'C Language Tutorial' with following command to compile

gcc sine.c -lm

Example:-
The following program, sine.c, computes a table of the sine function for angles between 0 and 360 degrees.
#include <stdio.h>
#include <math.h>

int main()
{
int angle_degree;
double angle_radian, pi, value;

/* Print a header */
printf ("\nCompute a table of the sine function\n\n");

/* obtain pi once for all */
/* or just use pi = M_PI, where
M_PI is defined in math.h */
pi = 4.0*atan(1.0);
printf ( " Value of PI = %f \n\n", pi );

printf ( " angle Sine \n" );

angle_degree=0; /* initial angle value */
/* scan over angle */

while ( angle_degree <= 360 ) /* loop until angle_degree > 360 */
{
angle_radian = pi * angle_degree/180.0 ;
value = sin(angle_radian);
printf ( " %3d %f \n ", angle_degree, value );

angle_degree = angle_degree + 10; /* increment the loop index */
}
}
I did not understand what are the options '-lm' for. Man gcc can't find them.

B.R.
satimis

nabis
09-11-2004, 04:29 AM
O.K., that makes it clearlier, -lm in this case links against the math library (the #include <math.h> in the header), see "man 3 intro" for explanation (you may want also to take a look at "man intro", "man [1,2,3..] intro")

About the /^[ ]*-l syntax, that's regex (regular expression pattern matching) (man grep)
"man" usualy uses the "less" pager to display the manulal pages, the "less" program permits you to search the text using regex, in this case it searches for:


/^[ ]*-l
Braking it apart:
/ <- the "less" "search" command
^ <- start of the line
[ ]* <- one or more empty spaces
-l <- what we are searching for


Hope this helps :)

satimis
09-11-2004, 04:38 AM
Hi nabis,

Tks for your further advice.

Sorry, I'm not quite clear of

/^[ ]*-l
Braking it apart:

[ ]* <- one or more empty spaces
What is it used for?

TIA

B.R.
satimis

nabis
09-11-2004, 04:51 AM
Take a look here, if you like, it explains the basics of regex (you can use it in grep, Perl, Java, even in `less` command):
http://www.linuxfocus.org/English/July1998/article53.html


(later on you could even use it in C, by including the regex.h library).

fatTrav
09-11-2004, 02:11 PM
Originally posted by satimis
I haven't got their C Language book.

My mistake. I thought your fist post indicated you had it. Well, it is a must have for anyone wanting to learn C. There is not better book or guide that I know about.

chrism01
09-13-2004, 06:33 AM
You may find this useful: http://publications.gbdirect.co.uk/c_book/
or this:
http://vergil.chemistry.gatech.edu/resources/programming/
possibly this:
http://www.dwheeler.com/secure-programs/Secure-Programs-HOWTO.html

satimis
09-13-2004, 08:44 AM
Originally posted by chrism01
You may find this useful: http://publications.gbdirect.co.uk/c_book/
or this:
http://vergil.chemistry.gatech.edu/resources/programming/
possibly this:
http://www.dwheeler.com/secure-programs/Secure-Programs-HOWTO.html Hi chrism01,

Tks for your URLs

B.R.
satimis