C in linux/unix-First step

First let's learn how to run a c program in linux/unix.Nearly every distro support's running c programs.

First thing is to type the c program.you can use any editor you want but here i use vi editor which comes with allmost all distro

vi editor


To create a new c file or to open an existing c file,type vi followed by filename.c
ex:
[[email protected] student]$vi adi.c

will create a new file adi.c or will open an exisiting file adi.c if it's present

now press insert key to edit or modify or add new content
after you complete writing the program,press Esc key and type :w (colen w) and press enter to save the file then press :q (colen q) and press enter to exit the vi editor.

Next stop:Compilation

Compilation


To compile a c file enter cc followed by the filename
ex:
[email protected] student]$cc adi.c

if the compilation is successful ,then no message will be displayed.Otherwise a message along with line number where the error is present is displayed.
Next stop:Running the compiled file

Running the compiled file


To run the compiled file,enter ./a.out
ex:
[[email protected] student]$./a.out

the c program will be running.Just work the same way as you work in windows.
note that irrespective of the file name ,you have to always enter "./a.out" to run the c program which you just compiled.

let's see an example program to print hi linux
[[email protected] student]$vi first.c

press insert and type the following program


#include
main()
{
printf("\nHi linux\n");
}

note that i have given just main() not void main()

now press Esc key and type :w to save the file and :q to exit the vi editor

[[email protected] student]$cc first.c
[[email protected] student]$./a.out


if you do these steps correctly, then you should get the output like this


Hi linux
[[email protected] student]$

Noteif you get a error like this


[[email protected] student]$ cc adi.c
bash: cc: command not found
[[email protected] student]$


means there is no gcc package in your distro and you are unable to run c programs in that :sad:

About This Blog

There is something for everyone. !! A tech blog on various topics. Let it be devops, programming etc; we have it all covered here in layman terms :)

Total Pageviews

Back to TOP