-
December 7th, 2001, 04:17 PM
#1
Senior Member
problem compiling c program
With everyone's help I finally got my first linux installation working. I hadn't programmed in c in two years, so anxious to get started. I typed in my starter program in pico.
#include<stdio.h>
void main()
{
printf("hello");
}
I then compiled hello.c:
gcc hello.c
It spewed back an error message, something about main not being int.
What am I doing wrong? Thanks for your help!
-
December 7th, 2001, 07:29 PM
#2
You need to change the line: 'void main()' to 'int main()' (without the quotes). This defines the main() function as returning an integer value.
You should also add the line 'return 0;' (again without the quotes) after 'printf("Hello");
A properly working example is shown below:
/* hello.c */
#include <stdio.h>
int main()
{
printf("Hello");
return 0;
}
/* end hello.c */
This should compile properly under any ASCII compliant C compiler (including gcc).
Hope this helps.
-
December 16th, 2001, 10:24 AM
#3
Junior Member
Compiling C in Linux
Probre in this form:
/* --------------- Begin code ----------------- */
#include<stdio.h>
main()
{
printf("Hello World\n");
}
/* -------------- End code -------------------- */
You also can put at this form
/* --------------- Begin code ----------------- */
#include<stdio.h>
main()
{
system("clear"); /* With this line when the program run first
clean the screen and next put Hello Worl. With the system you can put commands of the OS like ls, cat .... */
printf("Hello World\n");
}
/* -------------- End code -------------------- */
To compile you can put of this form:
gcc -o hello hello.c
The option -o is to put name that you want to have the exit program
Bye and lucky programing bye
Hide your face forever
dream and search forever
night and night you feel nothing
there\'s no way outside of my land
Open your eyes, open your mind ...
-
January 10th, 2002, 04:25 PM
#4
Senior Member
Maybe I'm doing something wrong, but I tried the first approach and got an error message about printf not being recognized.
I tried gcc -c , to stop after compiling, but before linking, and manually linked with ld. That's when I got the error message. The message wouldn't show up
with just gcc. It is quite frustrating. I want to like the thing.
-
January 10th, 2002, 06:27 PM
#5
// little help
#include <stdio.h>
int main()
{
printf ("Hello World\n");
return 0;
}
[shadow]l3aDmOnKeY[/shadow]
-
January 10th, 2002, 07:19 PM
#6
Let's see here, compilers..woo!
#include <stdio.h>
int main() {
printf("Hello world!\n");
}
That's all you need, say, in a foo.c file, of which then we do this:
$ gcc foo.c -o foo
You should have a file called 'foo' now, fully executable and ready to run. That's all that really needs to be done. By the way, the term 'void' means you don't require anything to be returned to you whereas 'int' means the result will be an integer value. Boolean means a true or false, etc etc...
We the willing, led by the unknowing, have been doing the impossible for the ungrateful. We have done so much with so little for so long that we are now qualified to do just about anything with almost nothing.
-
January 12th, 2002, 02:32 PM
#7
Junior Member
Send your code
Send your code to see how is your program and if see some bug i can tell you bye
Hide your face forever
dream and search forever
night and night you feel nothing
there\'s no way outside of my land
Open your eyes, open your mind ...
-
January 17th, 2002, 05:49 PM
#8
Senior Member
OK, this is a REALLY stupid question, but just typing foo at command prompt would run the program? Mine has that color coded thing going on so when the name is green, I should be executable?
-
January 18th, 2002, 12:35 AM
#9
That's ./filename unless the current directory
is on the PATH.
Unlike DOS, Linux doesn't execute programs
located in the current directory by default.
I came in to the world with nothing. I still have most of it.
-
January 18th, 2002, 03:50 AM
#10
Originally posted by owen76
OK, this is a REALLY stupid question, but just typing foo at command prompt would run the program? Mine has that color coded thing going on so when the name is green, I should be executable?
So you're using the "ls --color" option (or equivalent) as an alias?
Just BTW folks, you can use the "php" tags in the forum, which causes auto-formatting and color coding. (The "code" tags preserve original formatting.) For example (from Painshock's Post #3)
PHP Code:
/* --------------- Begin code ----------------- */
#include<stdio.h>
main()
{
system("clear"); /* With this line when the program run first
clean the screen and next put Hello Worl. With the system you can put commands of the OS like ls, cat .... */
printf("Hello World\n");
}
/* -------------- End code -------------------- */
[HvC]Terr: L33T Technical Proficiency
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|