Question: I would like to understand the basics of how to write, compile and execute a C program on Linux OS. Can you explain it with a simple example?
Answer: In this article, let us review very quickly how to write a basic Hello World C program and how to compile *.c program on Linux or Unix OS.
1. Write a Hello World C Program
Create the helloworld.c program using a Vim editor as shown below.
$ vim helloworld.c /* Hello World C Program */ #include<stdio.h> main() { printf("Hello World!"); }
2. Make sure C Compiler (gcc) is installed on your system
Make sure gcc is installed on your system as shown below.
$ whereis cc cc: /usr/bin/cc /usr/share/man/man1/cc.1.gz $ which cc /usr/bin/cc $ dpkg -l | grep gcc ii gcc 4:4.3.3-1ubuntu1 The GNU C compiler ii gcc-4.3 4.3.3-5ubuntu4 The GNU C compiler ii gcc-4.3-base 4.3.3-5ubuntu4 The GNU Compiler Collection (base package) ii gcc-4.3-doc 4.3.3-5ubuntu4 Documentation for the GNU compilers (gcc, go ii gcc-4.3-locales 4.3.3-5ubuntu4 The GNU C compiler (native language support ii gcc-4.3-multilib 4.3.3-5ubuntu4 The GNU C compiler (multilib files) ii lib64gcc1 1:4.3.3-5ubuntu4 GCC support library (64bit) ii libgcc1 1:4.3.3-5ubuntu4 GCC support library
3. Compile the helloworld.c Program
Compile the helloworld.c using cc command as shown below. This will create the a.out file.
$ cc helloworld.c $ ls -l -rw-r--r-- 1 ramesh ramesh 71 2009-08-28 14:06 helloworld.c -rwxr-xr-x 1 ramesh ramesh 9152 2009-08-28 14:07 a.out
4. Execute the C Program (a.out)
You can either execute the a.out to see the output (or) rename it to some other meaningful name and execute it as shown below.
$ ./a.out Hello World! $ mv a.out helloworld $ ./helloworld Hello World!
Comments on this entry are closed.
Do you know why this appear:
$ cc helloworld.c
helloworld.c:3:10: error: #include expects “FILENAME” or
helloworld.c: In function ‘main’:
helloworld.c:7: warning: incompatible implicit declaration of built-in function ‘printf’
?
I guess the compile-error is kind of easter egg from you =).
$ cc helloworld.c
helloworld.c:3:9: error: #include expects “FILENAME” or
helloworld.c: In function ‘main’:
helloworld.c:7: warning: incompatible implicit declaration of built-in function ‘printf
Just to explain the first error (on line 3 of the file): We didn’t tell the #include directive “What” to include. It expects some header file. The one we probably want is stdio.h, for it includes the declaration of the printf function, which we use on line 7 and which is the one the compiler is complaining about in the second error (crying the declaration can’t be found in other words =).
As it is System header file we add it in (don’t know how they’re called in eng =).
I call this “great time to patch your first program” =))
This is the patch file helloworld.patch:
3c3
#include
To apply this simply type:
$ patch helloworld.c < helloworld.patch
Then compile the file again and enjoy =)
I’m not a programmer. I tested your c program which did not compile. I searched google and found this line of yours may be incorrect:
#include
which should be
#include
With “#include” the program compiled.
Rich
Hello again.
I don’t think I mistyped that second #include. I think your web site removes Here it is in parenthesis (studio.h) just in case it is removed in the above sentence.
Rich
Confirmed. Your web site removes angle brackets, aka diamond brackets, aka cone brackets or aka chevrons. I had to look that up too.
By the way, I am using Firefox 3.0.12 on a newly installed Centos 5.3. Perhaps other web browsers will show the angle brackets.
stdio.h doesn’t appear in the listing of helloworld.c, probably interpreted as an html tag.
@All,
I’ve corrected it now. Thanks a lot for pointing out the issue.
You all are right. It’s because of less-than and greater-than symbol in stdio.h did not get displayed properly in the blog post.
hey, that is great!
thank you all!
when I comepile a sourcefile, the error message keeps appearing and says that there is an implicit declaration of function printf. I don’t know how to fix it. Please help!
Thanks! I’ve been looking for a clear, short and meaningful tutorial and at least I found it!
helpful thnx
nice thing to know….
super. very nice!
good work! Thangks.
I rewrite the example as follow to avoide a trival proble:
…
printf(“Hello World!\n”)
/* ^^ */
…
I insert a new line indicator to C.
please let me know diffrent beetween windows and centos C ++ cammand
i an using centos and i am training c ++ in windows when i run any scrip but not working let me know what i do
how to run unix programme
i am not getting difference between turbo and unix .why to use $ and vi editor and so on.
Hey guys !!
If suppose , I want to run a program of c++ .
What am, I suppose to do as if after includinmg the header file”iostream”
it still doesn’t work !!
Can u say why the error segmentation fault(core dumped) error occurs during run time