Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > C? Just getting started, need help

C? Just getting started, need help
Thread Tools
Fresh-Faced Recruit
Join Date: Dec 2001
Location: Texas
Status: Offline
Reply With Quote
Jun 13, 2002, 01:24 AM
 
I need some help getting started with C programming. I am trying to teach myself C programming using the "Practical C Programming" book published by O'Reilly press. Unfortunately I can not get past the very first step. When I follow the steps for the "Hello World" program I get a different result than the book. The book does not go into detail about compilers, but I assume my problem is that my compiler setup is different that what the book assumes. In the books example you end up with an executable that you run by typing in "hello <Enter>". I end up with an a.out file and the error message "hello: Command not found." when I type in "hello <Enter>".

I am using OS 10.1.5
I am using the Dec 2001 Dev Tools.
I am using BBEdit Lite as my text editor.
I am compiling and running from ~/hello directory.
The compile syntax I used was cc -g -Wall -ohello hello.c

I know this may seem like a very trivial problem to anyone who has been programming, but I need help making my very first baby step.

Thank you,

Jesse
     
Mac Enthusiast
Join Date: Nov 2001
Status: Offline
Reply With Quote
Jun 13, 2002, 02:02 AM
 
Navigate to the built product's folder, then type </font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">./hello</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">and enter at the command line. It's just the shell not recoginizing your program, because you didn't install it.
     
Junior Member
Join Date: May 2002
Location: Australia
Status: Offline
Reply With Quote
Jun 13, 2002, 02:07 AM
 
to compile you do this

cc -o nameofprogramme csourcecode.c

cc = to the compiler being used.
-o = says that you want to give the binary outputed a specific name.
csourcecode.c = is the source file you are trying to compile.

You will then end up with a binary called nameofprogramme you need to then type:

./nameofprogramme

to run the program, the ./ tell the shell that the program is in the current directory you are in. Or you could give it the full path to your program like this.

/User/jcopeland/Desktop/nameofprogramme

## a practical example

% cc -o HelloWorld helloworld.c
% ./HelloWorld
% Hello World
     
Dedicated MacNNer
Join Date: Oct 2000
Status: Offline
Reply With Quote
Jun 13, 2002, 02:20 AM
 
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">Originally posted by jcopeland:
<strong>The compile syntax I used was cc -g -Wall -ohello hello.c
</strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">Do you have a space between "-o" and "hello"?

cc -g -Wall -o hello hello.c

Does your program run correctly if you type "./a.out" and Enter?

The cc compiler will generate "a.out" executable file by default. As previously mentioned, the "-o" option will allow you to name the executable file with the name of your choice.

If you are short of storage space, if you let the computer use "a.out" every time, the file will be overwritten each time you successfully run the compiler and you won't keep generating executable files and fill up your hard drive.
     
Fresh-Faced Recruit
Join Date: Dec 2001
Location: Texas
Status: Offline
Reply With Quote
Jun 13, 2002, 09:41 AM
 
Thank you, everyone for the replies. It appears I had two problems.

1. I did not know about the ./ before typing in the program name.

2. I did need a space after the -o switch. I typed it exactly as the book showed it. My fault for following instructions.

Once again thank you,

Jesse
     
Dedicated MacNNer
Join Date: Oct 2000
Status: Offline
Reply With Quote
Jun 13, 2002, 11:55 AM
 
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">Originally posted by jcopeland:
<strong>1. I did not know about the ./ before typing in the program name.
</strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">"." refers to your current directory. ".." refers to its parent directory.
"/" is the delimiter for a directory.

I am currently taking a (mandatory) introductory C++ class, and yesterday they taught that the "./" is not necessary. I believe that assumes that you have a path set up in the initialization file for your shell, but I'm not sure how to do that now. (Maybe I'll ask the question today and have an answer tomorrow. Also, I'll try on the University's system and see if it works there.)

Followup: I've telnetted onto the University's system and got a program to run without the "./". It is a Sun Solaris 5.8 system and I didn't see anything in the shell initialization file.

<small>[ 06-13-2002, 01:40 PM: Message edited by: skipjack ]</small>
     
Professional Poster
Join Date: Apr 2001
Location: Seattle, WA
Status: Offline
Reply With Quote
Jun 13, 2002, 12:59 PM
 
Skipjack, I've noticed it too. I've chalked it up to a difference between Solaris and BSD. things like that, they work the same way, but have different syntax.

Jessie, an advance word of warning... you're using command line compiling right now. I'd suggest you stick with it, instead of switching to Project Builder. PB does NOT play nicely with C. It has a tweak where even if all your code is correct, it won't output anything until you input all the values. for example:

#include &lt;stdio.h&gt;

main()
{
float i;
fprint("Hello, give a value for i: ");
fscan("%f",&i);
fprint("you typed %f",i);
}

your output in command line will besomething like

% Hello, give a value for i: 4.2
% you typed 4.2

where in Project builder, the exact same code would appear to do nothing, until you punched in a number, like this

&gt;4.2Hello, give a value for i:
&gt;you typed 4.2

::shrug:: this little bug drove me crazy, so I thought I'd give another newbie heads up on it!

(if my code's wrong, it's cuz i haven't actually coded in C in over a month, and I just woke up, so apologies! )
The short shall inherit the earth. Just you wait. You won't see us coming. We'll pop out from under tables, beds, and closets in hordes. So you're tall, huh? You won't be so tall when I chew off your ankles. Mofo
     
Forum Regular
Join Date: Mar 2002
Location: Düsseldorf, Germany, Europe, Earth
Status: Offline
Reply With Quote
Jun 13, 2002, 02:55 PM
 
Checked out ssh'ing into Solaris, and I also had the working directory in the executable path (i.e. you don't need ./ to run your new programs). This would be specified somewhere deep down in a default file; if you want to get the same effect in OS X add the line
set path = ( $path . )
to your .tcshrc file. This adds ., which denotes the working directory, to the list of directories where the shell looks for executable files. I'm sure you can find lengthy discussions on why this would be good/bad on alt.linux.totallysadgeeks or similar places.

</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">I know this may seem like a very trivial problem</font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">These are the ones that are the worst to solve. If you have a proper problem, you can think about it for a long time and find a solution. This approach doesn't work for trivial problems. I promise that at some point you will spend hours looking for a bug that turns out to be caused by a '=' instead of a '=='. It's a completely trivial and utterly stupid mistake, but everyone makes it at some point.
     
   
Thread Tools
Forum Links
Forum 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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 01:23 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2