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:
[student@localhost 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:
student@localhost 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:
[student@localhost 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
[student@localhost 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

[student@localhost student]$cc first.c
[student@localhost student]$./a.out

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


Hi linux
[student@localhost student]$

Noteif you get a error like this


[student@localhost student]$ cc adi.c
bash: cc: command not found
[student@localhost student]$

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


You may also like

Find out what I am doing by following me on Twitter... Tweet This Post!

Leave a Reply

Comment on the next level:

- Enter your info on the left

- Create an avatar with Gravatar!