Skip to content Skip to sidebar Skip to footer

How to Make a C Program That Reads a File From Command Line

How practice you program in C on Linux? It is indeed very like shooting fish in a barrel and consists of three unproblematic steps.

Step 1: You write your program and salve the file with a .c extension. For example, my_program.c.

Footstep two: Y'all compile the program and generate the object file using gcc compiler in a terminal like this:

                gcc -o my_program my_program.c              

Pace 3: You run the generated object file to run your C program in Linux:

                ./my_program              
Running C Program Linux

This was simply the quick summary on how to compile and run C program in Linux. If yous are new to either C or Linux, I'll show these steps in item so that yous experience comfortable coding C plan in Linux environment.

In fact, I'll discuss how to run C programs in Linux terminal as well as in code editor.

Method ane: How to run C programs in Linux final

In lodge to run a C program in Linux, y'all need to have a C compiler present on your systems. The near popular compiler is gcc (GNU Compiler Collection).

You can install gcc using your distribution'due south parcel manager. In Debian and Ubuntu-based Linux distributions, use the apt command:

                sudo apt install gcc              

Switch to directory where you take kept your C program (or provide the path) and and then generate the object file by compiling the program:

                gcc -o my_program my_program.c              

Keep in mind that information technology 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 adept considering information technology will exist overwritten for each C programme and y'all won't be able to know which program the a.out object file belongs to.

Once you take your object file generated, run information technology to run the C program. Information technology is already executable. Unproblematic use it similar this:

                ./my_program              

And it volition display the desired output, if your program is correct. As yous can see, this is not very unlike from running C++ programs in Linux.

Every fourth dimension you brand a change in your programme, yous have to compile information technology starting time 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 similar Visual Studio Code

Not everyone is comfortable with command line and terminal and I totally sympathize that.

Yous can use a proper C/C++ IDE like Eclipse or Code Blocks but they are often likewise 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 together-ons to compile and run programs straight from the graphical code editor.

I am using Visual Studio Lawmaking editor in this example. Information technology's a hugely popular open source lawmaking editor from Microsoft.

Start affair starting time, install Visual Studio Lawmaking in Ubuntu from the software heart. For other distributions, please check your Linux distribution'south package manager or software eye. You may as well check the official website for more information.

Get-go Visual Studio Code and open up/create a project and create your C program here. I am using a sample Hello World program.

C Program Visual Studio Code Linux

You must ensure that you have gcc compiler installed on your Linux system.

                sudo apt install gcc              

Adjacent affair you would desire is to utilize an extension that allows you to run the C code. Microsoft may prompt you for installing its ain extension for C/C++ plan simply it is complicated to setup and hence I won't recommend information technology.

Instead, I suggest using the Code Runner extension. Information technology'south a no-nonsense extension and you can run C and C++ code hands without additional configuration.

Go to the Extensions tab and search for 'Code Runner' and install it.

Running C Program In Linux With Visual Studio Code
Install Code Runner extension for running C/C++ programme

Restart Visual Studio Code. Now, you lot should exist able to run the C code by using 1 of the following fashion:

  • Using the shortcut Ctrl+Alt+North.
  • Printing F1 and then select or type Run Code.
  • Right click the text editor and the click Run code from context menu.
Running C Program In Linux With Visual Studio Code
Correct click the program file and choose Run Code

When you lot run the programme, it is compiled automatically and then run. You can see the output in concluding that is opened at the lesser of the editor. What could be better than this?

Run C Program In Linux With Visual Studio Code
Program output is displayed in the bottom section of the editor

Which method do you adopt?

Running a few C programs in Linux command line is okay only using a lawmaking editor is much easier and saves time. Won't y'all agree?

I let you make up one's mind whichever method yous want to use.


pickeringhavoccon.blogspot.com

Source: https://itsfoss.com/run-c-program-linux/

Post a Comment for "How to Make a C Program That Reads a File From Command Line"