- How to Write and Run a C Program in Linux
- Step 1: Install the build-essential packages
- Step 2: Write a simple C program
- Step 3: Compile the C program with gcc Compiler
- Step 4: Run the program
- Karim Buzdar
- How to create, compile & run a C Program in Linux terminal
- Steps to write, run and compile C program in Linux
- 1. Install Compiler and other Dev tools
- 2. Check GCC version
- 3. Open a Text editor on Ubuntu or RHEL
- 4. Write your first C Program in Linux terminal
- 4. Compile with GCC
- 5. Run C program in Ubuntu Terminal
- 6. C program to add numbers in Linux
- How to Write, Compile and Run a C Program in Ubuntu and Other Linux Distributions [Beginner’s Tip]
- Method 1: How to run C programs in Linux terminal
- Method 2: How to run C programs in Linux using a code editor like Visual Studio Code
How to Write and Run a C Program in Linux
Linux is becoming programming heaven for developers, being an open-source and free operating system. Turbo C compiler is already an old approach to compile programs so let us programmers move to Linux for a new programming environment. In this article, we will explain how to write, compile, and run a simple C program. This will serve as a basis for you to move to more complicated and useful C programs that you can write and execute on Linux.
We have run the steps and commands mentioned in this article on a Ubuntu 20.04 LTS system but it will work on other versions like Ubuntu 18.04 or distributions like Debian 10 in the exact same way.
We will be using the Linux command-line tool, the Terminal, in order to compile a simple C program. To open the Terminal, you can use the Ubuntu Dash or the Ctrl+Alt+T shortcut.
Step 1: Install the build-essential packages
In order to compile and execute a C program, you need to have the essential packages installed on your system. Enter the following command as root in your Linux Terminal:
You will be asked to enter the password for root; the installation process will begin after that. Please make sure that you are connected to the internet.
Step 2: Write a simple C program
After installing the essential packages, let us write a simple C program.
Open Ubuntu’s graphical Text Editor and write or copy the following sample program into it:
Then save the file with .c extension. In this example, I am naming my C program as sampleProgram.c
Alternatively, you can write the C program through the Terminal in gedit as follows:
This will create a .c file where you can write and save a program.
Step 3: Compile the C program with gcc Compiler
In your Terminal, enter the following command in order to make an executable version of the program you have written:
Make sure your program is located in your Home folder. Otherwise, you will need to specify appropriate paths in this command.
Step 4: Run the program
The final step is to run the compiled C program. Use the following syntax to do so:
You can see how the program is executed in the above example, displaying the text we wrote to print through it.
Through this article, you have learned how to write, compile and run a simple C program in Linux. All you need is the essential packages and the right skills to make you a programming guru in Linux!
Karim Buzdar
About the Author: Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. You can reach Karim on LinkedIn
Источник
How to create, compile & run a C Program in Linux terminal
The C programming language is still alive because it is simple and can do a lot of things. As we know Turbo C compiler is a discontinued integrated development environment, well, on Linux you don’t need it as there is already GNU Compiler Collection to compile and run C or C++ programs. Therefore, if you know the C language, it is much easier to learn, write programs and run other programming languages on Linux operating systems such as C ++, Java, Perl, or PHP, as the languages have certain similarities. Here we will show the steps to install GCC compiler and how to write, compile and run a C program in Linux.
Now, what is a program?
Let’s say you want to train your cat or puppy how to sit or jump on your commands, then what language you will use. Obviously, your mother tongue whether it is English, German, Chinese, Hindi, or something else. We will not bark or meaau. In a similar way, if we want to tell our computer to perform some specific tasks such as calculations, we need to train it how to do that, with the help of a set of rules. But the problem is Computer only understands the binary language that is 0 or 1, thus we have created Programming languages to create programs that we can understand and after compiling our computer and later can execute the same. Thus, a program is a sequence of actions to achieve a goal.
Steps to write, run and compile C program in Linux
Here we are using Ubuntu 20.04 LTS, however, the steps given here are not only for it. You can also implement on older Ubuntu versions such as 18.04/16.04 including Linux Mint, Debian, Kali, CentOS, RedHat, Fedora Elementary, and more…
1. Install Compiler and other Dev tools
To write and execute a C program on Linux, we need a compiler that will compile the code we have written and give us an executable file for the same. Therefore, for that, if you are on Debian or Ubuntu then install build-essential, and on RHEL based distros go for Development Tools.
For RHEL/Fedora/CentOS
Run system update command, first:
On Ubuntu or Debian systems
You should have sudo or root user access to run the above commands…
2. Check GCC version
GCC is the open-source library, which is an acronym for GNU Compiler Collection available for Linux and acts as a compiling system for Program C and other various programming languages. However, it mainly used to compile C and C++ programs… Thus, after installing the development tools in the first steps, you will also get GCC on your system. To confirm and check its version run:
To get full info:
To know only installed path
For version, only-
3. Open a Text editor on Ubuntu or RHEL
All Linux distros now come with graphical text editors, however, we can use the command terminal to create a text file to write C program codes as well using command line text editors such as nano, VIM, gedit, and more... Here we are using nano.
To install nano, if it is not on your system type:
RHEL/CentOS- sudo yum install nano
Ubuntu/Debian– sudo apt install nano
4. Write your first C Program in Linux terminal
Let’s create a demo C program in which we will include common C program library stdio.h to use various variables including functions for performing input and output. If you are learning C programming then you already familiar with the C libraries that we define in the header of the program to call various functions for performing various tasks.
For example, if you are writing a C program that mathematical functions then we need to declare math.h library, for graphics, we include graphics.h and so on…
Create a file:
Now, let’s add the following lines to create a simple C program that will give an output “Say hello to H2s” when we compile and execute it. To Save the file by pressing Ctrl+X, type Y, and then hit the Enter Key.
The explanation for the above command- In the above command first we have added stdio.h library in the header and then int main(); in this syntax main() is the entry point of any C/C++ program, in short, it tells the compiler to start compiling from this point and int main() function will accept any number of arguments but it returns integer value usually zero. However, that is an old way, developers usually prefer int main(void) this represents a function that expects no arguments or when a program doesn’t require any initial parameters.
After that we use printf function to show or print a text ” Say hello to H2S” and to break the output line or to have a page break, we used \n (Escape Sequences). As we have used the main () function that generally supposed to return 0 value to confirm there is no failure in compiling and the task has been finished successfully, however, if it gets non-zero that means a failure. Nevertheless, declaring return 0; is not compulsory even without the program and the main () function will work in the same way.
4. Compile with GCC
Now, let’s Compile our first C program, we have written. We use GCC compiler
In the above command, we are compiling the demo.c file or program using GCC and saving it in an executable format that is a demo. You can save it with some other name as well.
5. Run C program in Ubuntu Terminal
Let’s Run it. For that simply type:
In the above, example, we have compiled a C program demo.c and saved it an executable called demo. Thus, to run the same, we type:
Output:
6. C program to add numbers in Linux
Let’s go a little step further and write a simple program in C language to add numbers. In it, we will ask to users add two numbers and the program perform the addition and print the result.
Create a new program, let’s say sum.
Add the following code in that and save the file using Ctrl+X, type Y, and then press Enter key.
The explanation for the above code:
With the help int which is an integer variable, we are declaring three variables num1, num2, and sum. The num1 and num2 variables will hold the two numbers that a user will enter to get a result that will store in the sum variable.
int num1, num2, sum;
After that, we are printing a text to ask a user to enter two numbers that you want to add.
printf(«Enter two integers: «);
Then we will use scanf function to take inputs from the user in integer format using another variable %d or % i. %d specifies the type of variable as decimal and %i specifies the type as an integer.
Now, we will use + operator to add num1 and num2 and store the result of the same in sum variable:
sum = number1 + number2;
Finally, print the result of the addition using printf function. Now, what happening here is, first %i read the value stored in num1 and second %i read the value stored in num2, and third %i will read the value stored in sum. \n is just to page break line. After that, it prints all of them together which appears to the frontend user a familiar way to get an answer to its addition query.
printf(«%i + %i = %i \n», num1, num2, sum);
Compile and run your code
Closing thoughts:
Well, this tutorial is just an introduction on how to start with the C program in Linux, however, to go further in deep you have to acquire more knowledge through books on how libraries, functions, variables, and operators work in it.
Источник
How to Write, Compile and Run a C Program in Ubuntu and Other Linux Distributions [Beginner’s Tip]
Last updated October 7, 2021 By Abhishek Prakash 21 Comments
How do you program in C on Linux? It is indeed very easy and consists of three simple steps.
Step 1: You write your program and save the file with a .c extension. For example, my_program.c.
Step 2: You compile the program and generate the object file using gcc compiler in a terminal like this:
Step 3: You run the generated object file to run your C program in Linux:
This was just the quick summary on how to compile and run C program in Linux. If you are new to either C or Linux, I’ll show these steps in detail so that you feel comfortable coding C program in Linux environment.
In fact, I’ll discuss how to run C programs in Linux terminal as well as in code editor.
Method 1: How to run C programs in Linux terminal
In order to run a C program in Linux, you need to have a C compiler present on your systems. The most popular compiler is gcc (GNU Compiler Collection).
You can install gcc using your distribution’s package manager. In Debian and Ubuntu-based Linux distributions, use the apt command:
Switch to directory where you have kept your C program (or provide the path) and then generate the object file by compiling the program:
Keep in mind that it is optional to provide the output object file (-o my_program). If you won’t do that, an object file named a.out will be automatically generated. But this is not good because it will be overwritten for each C program and you won’t be able to know which program the a.out object file belongs to.
Once you have your object file generated, run it to run the C program. It is already executable. Simple use it like this:
And it will display the desired output, if your program is correct. As you can see, this is not very different from running C++ programs in Linux.
Every time you make a change in your program, you have to compile it first and then run the generated object file to run the C program.
Method 2: How to run C programs in Linux using a code editor like Visual Studio Code
Not everyone is comfortable with command line and terminal and I totally understand that.
You can use a proper C/C++ IDE like Eclipse or Code Blocks but they are often too heavy programs and more suitable for large projects.
I recommend using an open source code editor like Visual Studio Code or Atom. These are basically text editors and you can install add-ons to compile and run programs directly from the graphical code editor.
I am using Visual Studio Code editor in this example. It’s a hugely popular open source code editor from Microsoft.
First thing first, install Visual Studio Code in Ubuntu from the software center. For other distributions, please check your Linux distribution’s package manager or software center. You may also check the official website for more information.
Start Visual Studio Code and open/create a project and create your C program here. I am using a sample Hello World program.
You must ensure that you have gcc compiler installed on your Linux system.
Next thing you would want is to use an extension that allows you to run the C code. Microsoft may prompt you for installing its own extension for C/C++ program but it is complicated to setup and hence I won’t recommend it.
Instead, I suggest using the Code Runner extension. It’s a no-nonsense extension and you can run C and C++ code easily without additional configuration.
Go to the Extensions tab and search for ‘Code Runner’ and install it.
Restart Visual Studio Code. Now, you should be able to run the C code by using one of the following way:
- Using the shortcut Ctrl+Alt+N.
- Press F1 and then select or type Run Code.
- Right click the text editor and the click Run code from context menu.
When you run the program, it is compiled automatically and then run. You can see the output in terminal that is opened at the bottom of the editor. What could be better than this?
Which method do you prefer?
Running a few C programs in Linux command line is okay but using a code editor is much easier and saves time. Won’t you agree?
I let you decide whichever method you want to use.
Like what you read? Please share it with others.
Источник