Writing a C/C++ program on ubuntu terminal


Here is a simple 5 step instruction to compile and execute C/C++ program on the linux terminal. I have also included a sample C/C++ example to demonstrate.

In order to write the program in the terminal itself, we need a console (terminal) based  text editor so, I am using Vim in this tutorial since vim is the most popular console text editor, but you are free to write your program in any editor like nano, pico, jed or vim etc.

Now, lets follow the instructions but it would be really helpful if you learn some vim basics here (won’t take much time)  before proceeding to the instructions or its perfectly okay if you are in a hurry πŸ™‚




Instructions

1. Open the terminal and install vim by typing

sudo apt-get install vim
 

2. Now, create a new C/C++ program name with this vim command

(for C program)

vi program.c


(for C++ program)

vi program.cpp
 


3. and press the i key on your keyboard to start writing your program and when you have completed your program press ESC and type  :wq  to save your code and exit vim editor.

C program example-



C++ program example-


4. compile your code by using the following gcc compiler command

(for C program)

cc program.c


(for C++ program)

g++ program.cpp
 

5. on success compilation, the gcc compiler produces an executable file named  ‘a.out’ which you will execute using the following command to view the output of your program.

./a.out
Done!


output of the above program


To open an existing file use the same command which is used to create a new file
vi program.c

for information on vim basics see this link
for vim commands see this link 

Here is a 3 minute video tutorial to understand it more better !

http://www.youtube.com/embed/yXf1fsT4JO4

8 thoughts on “Writing a C/C++ program on ubuntu terminal

  1. Program Name: minmax.c

    Explanation of Problem 1:

    You are required to create a program that can determine the maximum value and

    minimum amount of data. Data may include fractions and / or numbers

    round n. Of a number of these data, determine the maximum value and

    minimum. Make checks (validation) of the n input and make sure that the value of

    n is in the interval 0 <n <= 30.

    Input example:

    The amount of data (n): 8

    Data to 1: 23

    Data for 2: 64

    Data to 3: 14

    Data to 4: 64

    Data for 5: 28

    Data for 6: 51

    Data for 7: 14

    Data for 8: 40

    Example output:

    Maximum value: 64

    Minimum value: 14

    how do we solve it??

    I've tried, but a lot of mistakes,,, be advised beginners

    thanks for the explanation

  2. Program Name: minmax.c
    Explanation of Problem 1:
    You are required to create a program that can determine the maximum value and
    minimum amount of data. Data may include fractions and / or numbers
    round n. Of a number of these data, determine the maximum value and
    minimum. Make checks (validation) of the n input and make sure that the value ofn is in the interval 0 <n <= 30.
    Input example:
    The amount of data (n): 8
    Data to 1: 23
    Data for 2: 64
    Data to 3: 14
    Data to 4: 64
    Data for 5: 28
    Data for 6: 51
    Data for 7: 14
    Data for 8: 40
    Example output:
    Maximum value: 64
    Minimum value: 14
    how do we solve it??
    I've tried, but a lot of mistakes,,, be advised beginners
    thanks for the explanation

Leave a Reply

Your email address will not be published.