You are currently viewing [How-To] Run C/C++ programs directly from your terminal in Linux

[How-To] Run C/C++ programs directly from your terminal in Linux

In this how-to tutorial we will be learning to run C/C++ programs directly from your terminal in Linux.

To get started you will need to follow these simple steps:

 

Step 1: – Open Terminal by using your shortcut key or by searching it. Once you are done it should look like this:

Step 2: – Now we should create a C/C++ file. Use the below command to create C/C++ files:

touch helloworld.c              //For C

touch helloword.cpp             //For C++ 

Step 3: – Once the file is created, we should write our first C/C++ program using a text editor. Use below command to open the newly created file in text editor:

gedit helloworld.c              //For writing C Program

gedit helloworld.cpp            //For writing C++ Program 

Step 4: – Write your own program in editor or copy and paste the following program for testing:

//C Program

#include <stdio.h>
int main() 
{
   printf("Hello, World!");
   return 0;
}  
//C++ Program

#include <iostream>

int main() {
    std::cout << "Hello, World!";
    return 0;
} 

Step 5: – Now save the file and return back to terminal. Everything is ready, now we need to compile our program. Use below command to compile:

gcc helloworld.c                //For C Program

g++ helloworld.cpp              //For C++ Program 

Step 6: – Our program is successfully compiled and the executable OUT file is generated. To get the final output of program, use the given command:

./a.out 

Hurrah! We have successfully executed our C/C++ programs directly from terminal.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments