LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   hello.c - now what? (https://www.linuxquestions.org/questions/programming-9/hello-c-now-what-504049/)

Micro420 11-22-2006 06:15 PM

hello.c - now what?
 
I am just trying to print a simple "Hello, world!" but it won't work. Here is the content of hello.c
Code:

main()
{
        printf("hello, world\n");
}

I then type cc hello.c and I get some errors. Isn't cc a C compiler?

Also, if I add the \a, is the computer supposed to beep?
Code:

main()
{
        printf("hello, world\n");
        printf("\a");
}


dmail 11-22-2006 07:02 PM

Code:

#include <stdio.h>

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

        printf("hello, world\n");
        printf("\a");
        return 0;
}

if the file is saved as hello_world.c
Quote:

gcc hello_world.c
man gcc for options.

vxc69 11-22-2006 07:02 PM

Try:

Code:

#include <stdio.h>

main()
{
        printf("hello, world\n");
        printf("\a");
}

That should get rid of the warning. Then cc hello.c -o hello
This should make a binary called hello.

Without the "-o" flag it will only make assembler output (a.out) and no binary. You can run a.out like any other binary. I never bothered trying that.



vxc

bigearsbilly 11-23-2006 03:38 AM

you can use

make hello
Code:

billym.primadtpdev>make hello 
gcc    hello.c  -o hello

to do the in-built make rules.

(no you don't need a makefile)

Micro420 11-25-2006 09:20 PM

Great! Adding the #include <stdio.h> did the trick! I can print Hello, world now!

BiThian 11-26-2006 12:26 AM

Even if you don't add #include <stdio.h> you receive at most a warning like this one:
Code:

implicit declaration of printf
but the code still gets compiled.

Micro420 11-26-2006 12:43 PM

Quote:

Originally Posted by BiThian
Even if you don't add #include <stdio.h> you receive at most a warning like this one:
Code:

implicit declaration of printf
but the code still gets compiled.

Thanks! I edited my post so others may not know what you are talking about. I was getting that warning error, but I later installed some build tools on my Ubuntu Linux and that warning message went away. But you are correct in that the binary was still able to build after the warning message. Now it's time to learn more about C! I've ordered tons of books on C and am eager to learn this language.

BiThian 11-26-2006 12:55 PM

I think these two books are enough :p
Kernighan & Ritchie - C Programming Language
Stephen Prata - C Primer Plus (5th Edition) ( a ***** book :) )


All times are GMT -5. The time now is 08:42 PM.