Quantcast
Channel: C Programming – I'm Programmer
Viewing all articles
Browse latest Browse all 30

How to Write C Program in Ubuntu/ Linux

$
0
0

C Programming Language in Linux - Ubuntu

HOW TO WRITE C PROGRAM IN UBUNTU

  • Open a 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 program. Command: gcc prog.c -o prog
  • Run/ Execute. Command: ./prog

This is the outline for the C Program in Ubuntu/ Linux. Now we will see the entire article for how to write a C Program in Ubuntu/ Linux/ Fedora etc.


Hello, programmer, we all know that the Turbo 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. You can as well check out the examples of Linux C. Now, we will learn how to write C programs in Ubuntu/ Linux.

C Programming Ubuntu

This article is split into three subparts

How to Install C Language on 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 the C / C++ program you need to install the essential packages. For this enter the command given below in a terminal window.

sudo apt-get install build-essential

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++.


How to Write C Program in Ubuntu

  • Open a 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 program. Command: gcc prog.c -o prog
  • Run/ Execute. Command: ./prog

Programmers, Now we will learn it in depth.

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

    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.
    #include<stdio.h>
    int main()
    {
    printf("\nThis is C Programming in Ubuntu\n\n");
    return 0;
    }

    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.
    gcc prog.c -o prog
  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.
    ./prog
  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++ program. Command: g++ progcpp.cpp -o progcpp
  • Run/ Execute. Command: ./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.
    gedit progcpp.cpp
  2. Now you can write your program, I have given an example below.
    #include
    using namespace std;
    int main()
    { 
    cout<<"\nThis is C++ Programming in Ubuntu\n\n";
    return 0;
    }
  3. Now save and close the editor.
  4. To compile the C++ program in Linux. Enter the command given below in terminal window.
    g++ progcpp.cpp -o progcpp
  5. Enter the command given below to run the C++ program.
    ./progcpp

So, You can start to learn C and C++ programming in Linux OS. Let me know if you have any errors. You can email(info@improgrammer.net) 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.

Download Free: C Programming Language in Ubuntu PDF

Let’s have some fun with hello world program using C

Hello World using C Programming Language
Hello World using C Programming Language

Viewing all articles
Browse latest Browse all 30

Trending Articles