How to write c program in ubuntu

0
34513
How to write and run c or c++ programs in ubuntu 2
How to write and run c or c++ programs in ubuntu

Hello, programmer, We all know that c++ compiler is the old compiler, so we have started doing programming in Ubuntu. Linux was originally developed as a free operating system and is becoming very popular among the programmers. In my opinion, you should also start doing programming in Linux. I thought that I should share the process to write and run c/c++ programs on Ubuntu. Here are examples of Linux C. We will learn how to write c program in Ubuntu/ Linux.

1. Open a terminal window, for this go to Applications> Accessories> Terminal, as shown in below image.

How to write and run c or c++ programs in ubuntu 2
How to write and run c or c++ programs in ubuntu

2. To run c/c++ program you need to install the essential packages. For this enter the command given below in a terminal window.

Now it will ask for the admin password. When you enter the password the installation process will start.

Note: For installing packages, you must be connected to the internet. It will take a few minutes to complete, depending upon your internet speed.

3. Now it’s time to write and run the program. Below I have explained simple steps for both c and c++.

C Programming Ubuntu

How to write c program in Ubuntu

  • Open text editor( gedit, vi). Command: gedit prog.c
  • Write a c program. Example: #include<stdio.h> int main(){ printf(“Hello”); return 0;}
  • Save c program with .c extension. Example: prog.c
  • Compile c cprogram. Command: gcc prog.c -o prog
  • Run/ Execute. Commnd: ./prog

Programmers, Now we will learn it in depth.

  1. Enter the command given below in a terminal window to open a text editor.

    Here prog.c is the name of the program. After entering above code it will ask for the password.

  2. Now write your program, for your convenience, I have given a sample program below, you can copy and paste it into the gedit text editor.

    You can copy the code from Github:
  3. Now save and close the editor.
  4. Compile c program in Ubuntu as below. Gcc compiler will compile our code.
  5. If there will be no error in the program, then nothing will be shown. And if an error occurs, it will be shown. In case you get an error you have to open the text editor again by repeating steps 1 and remove it and again save and close the editor.
  6. Enter the command given below to run the c program.
  7. Now, you will see the output in the terminal window as shown in below image.
How to write and run c or c++ programs in ubuntu 1
How to write and run c or c++ programs in Ubuntu

I have listed out very useful resources to learn c programming language for Linux as shown in below.

How to write c++ program in Ubuntu

  • Open any text editor( gedit, vi). Command: gedit progcpp.cpp
  • Write a c++ program. Example:#include int main(){ cout<<“Hello world”; return 0;}
  • Save c++ program with .cpp extension. Example: progcpp.cpp
  • Compile c cprogram. Command: g++ progcpp.cpp -o progcpp
  • Run/ Execute. Commnd: ./progcpp

We will follow same steps as c program we did.

  1. Enter the command given below in terminal window to open a text editor.
  2. Now you can write your program, I have given an example below.
  3. Now save and close the editor.
  4. To compile the c++ program in Linux. Enter the command given below in terminal window.
  5. Enter the command given below to run the C++ program.

So, You can start to learn c and c++ programming in Linux os. Let me know if you have any errors. You can mail([email protected]) us. We would like to help you to execute c program in Linux environment. You can use best c++ compiler online also.

If you liked our tutorial, Please share it on social media. You can follow us on G+, FB, Twitter.

NO COMMENTS

LEAVE A REPLY