Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: problem compiling c program

  1. #1
    Senior Member
    Join Date
    Oct 2001
    Posts
    107

    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!

  2. #2
    AO Antique pwaring's Avatar
    Join Date
    Aug 2001
    Posts
    1,409
    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.
    Paul Waring - Web site design and development.

  3. #3
    Junior Member
    Join Date
    Dec 2001
    Posts
    11

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



  4. #4
    Senior Member
    Join Date
    Oct 2001
    Posts
    107
    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.

  5. #5
    Senior Member
    Join Date
    Sep 2001
    Posts
    193
    // little help

    #include <stdio.h>

    int main()

    {
    printf ("Hello World\n");

    return 0;

    }
    [shadow]l3aDmOnKeY[/shadow]

  6. #6
    PHP/PostgreSQL guy
    Join Date
    Dec 2001
    Posts
    1,164
    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.

  7. #7
    Junior Member
    Join Date
    Dec 2001
    Posts
    11

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



  8. #8
    Senior Member
    Join Date
    Oct 2001
    Posts
    107
    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?

  9. #9
    AO Curmudgeon rcgreen's Avatar
    Join Date
    Nov 2001
    Posts
    2,716

    Thumbs up

    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.

  10. #10
    Old-Fogey:Addicts founder Terr's Avatar
    Join Date
    Aug 2001
    Location
    Seattle, WA
    Posts
    2,007
    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
  •