{"id":22791,"date":"2023-10-28T08:36:42","date_gmt":"2023-10-28T08:36:42","guid":{"rendered":"https:\/\/linuxways.net\/?p=22791"},"modified":"2023-10-28T08:37:52","modified_gmt":"2023-10-28T08:37:52","slug":"how-to-run-a-c-program-in-ubuntu-linux","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/ubuntu\/how-to-run-a-c-program-in-ubuntu-linux\/","title":{"rendered":"How to Run a C Program in Ubuntu Linux?"},"content":{"rendered":"<p><strong>C<\/strong> Language is one of the most popular and old programming languages used widely by Network and System Administrators in Embedded Systems. With C Programming, the efficiency and performance of systems can be increased as it is statically typed, and rich in built-in Operators. The Linux Kernel is Written in C language which makes the Applications written in C more compatible.<\/p>\n<p>This article explains how you can run a C Program in Ubuntu.<\/p>\n<h2><strong>How to Run a C Program in Ubuntu Linux?<\/strong><\/h2>\n<p>To run a C Program in Ubuntu, you need to install the \u201c<strong>gcc<\/strong>\u201d compiler as it is not pre-installed in Ubuntu. Once you add the \u201cgcc\u201d Compiler, you can use either the Terminal to run the C Program or you can download any code editor and run the C Program through the Ubuntu GUI. Both the methods for running a C Program in Ubuntu are discussed below.<\/p>\n<h2><strong>Method 1: Running a C Program in Ubuntu Using Terminal<\/strong><\/h2>\n<p>Running a C Program from the Terminal is easier for users as it is more flexible and efficient. C Programs can be run anywhere in the Terminal with more efficiency than running the program in a code editor. Below are the steps required to run a C Program in Ubuntu using the Terminal.<\/p>\n<h3><strong>Step 1: Update Existing Packages<\/strong><\/h3>\n<p>Before running a C program in Ubuntu, ensure that the \u201cgcc\u201d compiler is installed on your system. In our case, it is not installed. To install the \u201cgcc\u201d compiler, update the existing packages in the Ubuntu Repository with the command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">sudo<\/span> apt update<\/div><\/div>\n<p>Wait for the packages to update:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"728\" height=\"327\" class=\"wp-image-22792\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-1.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-1.png 728w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-1-300x135.png 300w\" sizes=\"auto, (max-width: 728px) 100vw, 728px\" \/><\/p>\n<p>Once the packages are updated, upgrade those packages with the following command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">sudo<\/span> apt upgrade<\/div><\/div>\n<p>It will ask for confirmation in between the upgrade process. Enter \u201cY\u201d to continue with the installation process:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"942\" height=\"485\" class=\"wp-image-22793\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-2.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-2.png 942w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-2-300x154.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-2-768x395.png 768w\" sizes=\"auto, (max-width: 942px) 100vw, 942px\" \/><\/p>\n<h3><strong>Step 2: Install Build Essential<\/strong><\/h3>\n<p>You need to install the necessary packages for compiling software. The \u201cbuild-essential\u201d is a useful package for compiling software that includes a debugger, a \u201cgcc\u201d compiler, and the GNU Compiler collection. To install the \u201cbuild-essential\u201d package, use the below-stated command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">sudo<\/span> apt <span class=\"kw2\">install<\/span> build-essential<\/div><\/div>\n<p>It will ask for confirmation as necessary packages will be added to the system. Enter \u201cY\u201d to continue with the installation process:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"936\" height=\"509\" class=\"wp-image-22794\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-3.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-3.png 936w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-3-300x163.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-3-768x418.png 768w\" sizes=\"auto, (max-width: 936px) 100vw, 936px\" \/><\/p>\n<h3><strong>Step 3: Verify the GCC Compiler<\/strong><\/h3>\n<p>Once the \u201cbuild-essential\u201d packages are added, the \u201cgcc\u201d Compiler will be added to your Ubuntu System. Verify the addition of \u201cgcc\u201d packages by checking the \u201cgcc\u201d compiler version with the following command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">gcc<\/span> <span class=\"re5\">--version<\/span><\/div><\/div>\n<p>In our case, the \u201c11.4.0\u201d version of \u201cgcc\u201d compiler is added which affirms the addition of \u201cgcc\u201d packages on our Ubuntu System:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"936\" height=\"130\" class=\"wp-image-22795\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-4.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-4.png 936w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-4-300x42.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-4-768x107.png 768w\" sizes=\"auto, (max-width: 936px) 100vw, 936px\" \/><\/p>\n<h3><strong>Step 4: Create a C File<\/strong><\/h3>\n<p>To create a C File, navigate to the directory you want the file in, and then use the \u201ctouch\u201d command to create a new file. Ensure the file has the \u201c.c\u201d extension:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">touch<\/span> <span class=\"sy0\">&lt;<\/span>fileName<span class=\"sy0\">&gt;<\/span>.c<\/div><\/div>\n<p>In our case, we created a new C File with the \u201ccubuntu\u201d File Name in the default user directory with the \u201ctouch\u201d command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">sudo<\/span> <span class=\"kw2\">touch<\/span> cubuntu.c<\/div><\/div>\n<p>The Command line moves to the next line indicating the file was created:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"936\" height=\"150\" class=\"wp-image-22796\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-5.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-5.png 936w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-5-300x48.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-5-768x123.png 768w\" sizes=\"auto, (max-width: 936px) 100vw, 936px\" \/><\/p>\n<p>To verify the creation of the \u201ccubuntu\u201d file, list all the files in the Terminal using the ls command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">ls<\/span><\/div><\/div>\n<p>In the Terminal, the \u201ccubuntu.c\u201d file can be seen:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"942\" height=\"254\" class=\"wp-image-22797\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-6.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-6.png 942w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-6-300x81.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-6-768x207.png 768w\" sizes=\"auto, (max-width: 942px) 100vw, 942px\" \/><\/p>\n<h3><strong>Step 5: Adding Code Snippet in File<\/strong><\/h3>\n<p>Now we have to add a Program or Code snippet inside the \u201ccubuntu.c\u201d file which can later be executed. To open the \u201ccubuntu.c\u201d file in the nano editor, the \u201cnano\u201d command can be used:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">sudo<\/span> <span class=\"kw2\">nano<\/span> <span class=\"sy0\">&lt;<\/span>fileName<span class=\"sy0\">&gt;<\/span><\/div><\/div>\n<p>In our case, the filename is \u201ccubuntu.c\u201d, so the nano command will be executed as follows:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">sudo<\/span> <span class=\"kw2\">nano<\/span> cubuntu.c<\/div><\/div>\n<p>After running the command, the text editor will open:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"728\" height=\"479\" class=\"wp-image-22798\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-7.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-7.png 728w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-7-300x197.png 300w\" sizes=\"auto, (max-width: 728px) 100vw, 728px\" \/><\/p>\n<p>In the Code Editor, add the C program according to your own choice. In our case, we will specify the code below:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co0\">#include &lt;stdio.h&gt;<\/span><br \/>\n<br \/>\nint main<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n<br \/>\n<span class=\"kw3\">printf<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;Hello from LinuxWays! <span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"br0\">&#41;<\/span>;<br \/>\n<br \/>\n<span class=\"kw3\">return<\/span> <span class=\"nu0\">0<\/span>;<br \/>\n<br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>Once added, press the \u201cctrl+o\u201d shortcut key to write the content to the file and then press the \u201cctrl+x\u201d shortcut key to exit the file:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"732\" height=\"480\" class=\"wp-image-22799\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-8.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-8.png 732w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-8-300x197.png 300w\" sizes=\"auto, (max-width: 732px) 100vw, 732px\" \/><\/p>\n<h3><strong>Step 6: Compile the C Program<\/strong><\/h3>\n<p>Before running a C Program File, you have to compile it with the \u201cgcc\u201d compiler using the following command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">gcc<\/span> <span class=\"sy0\">&lt;<\/span>fileName<span class=\"sy0\">&gt;<\/span>.c <span class=\"re5\">-o<\/span> <span class=\"sy0\">&lt;<\/span>fileName<span class=\"sy0\">&gt;<\/span><\/div><\/div>\n<p>In our case, the filename is \u201ccubuntu.c\u201d. Thus to compile the file, the \u201cgcc\u201d command will be:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">gcc<\/span> cubuntu.c <span class=\"re5\">-o<\/span> cubuntu<\/div><\/div>\n<p>The command line moves to the next line indicating that the \u201ccubuntu\u201d file is compiled:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"725\" height=\"42\" class=\"wp-image-22800\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-9.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-9.png 725w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-9-300x17.png 300w\" sizes=\"auto, (max-width: 725px) 100vw, 725px\" \/><\/p>\n<h3><strong>Step 7: Running the C File<\/strong><\/h3>\n<p>Now, you can run the C file through the Terminal using the following syntax:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">.<span class=\"sy0\">\/&lt;<\/span>fileName<span class=\"sy0\">&gt;<\/span><\/div><\/div>\n<p>In our case, the executable C file name is \u201ccubuntu\u201d:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">.<span class=\"sy0\">\/<\/span>cubuntu<\/div><\/div>\n<p>The C Program runs and the output is displayed on the Terminal. In our case the output \u201cHello from LinuxWays!\u201d can be seen in the Terminal:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"714\" height=\"79\" class=\"wp-image-22801\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-10.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-10.png 714w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-10-300x33.png 300w\" sizes=\"auto, (max-width: 714px) 100vw, 714px\" \/><\/p>\n<p>This is how you can run a C Program in Ubuntu through the Terminal.<\/p>\n<h2><strong>Bonus Example<\/strong><\/h2>\n<p>To Run another C Program in the Terminal, create the File first with the \u201ctouch\u201d Command. In our case, we created the \u201ccexample.c\u201d file using the \u201ctouch\u201d command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">sudo<\/span> <span class=\"kw2\">touch<\/span> cexample.c<\/div><\/div>\n<p>The Command line moves to the following line indicating the file was created:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"721\" height=\"58\" class=\"wp-image-22803\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-11.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-11.png 721w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-11-300x24.png 300w\" sizes=\"auto, (max-width: 721px) 100vw, 721px\" \/><\/p>\n<p>Now to add the C Program inside the File, use the \u201cnano\u201d command to open the file:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">sudo<\/span> <span class=\"kw2\">nano<\/span> cexample.c<\/div><\/div>\n<p>The text editor will appear where you can add the C Program:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"734\" height=\"481\" class=\"wp-image-22804\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-12.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-12.png 734w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-12-300x197.png 300w\" sizes=\"auto, (max-width: 734px) 100vw, 734px\" \/><\/p>\n<p>Now, add the below C Program Code in the \u201ccexample.c\u201d File:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co0\">#include&lt;stdio.h&gt;<\/span><br \/>\n<br \/>\nint main<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<br \/>\n<span class=\"br0\">&#123;<\/span><br \/>\n<br \/>\nint x, y, prod;<br \/>\n<br \/>\n<span class=\"kw3\">printf<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;Enter any Two Numbers: &quot;<\/span><span class=\"br0\">&#41;<\/span>;<br \/>\n<br \/>\nscanf<span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;%d%d&quot;<\/span>, <span class=\"sy0\">&amp;<\/span>x, <span class=\"sy0\">&amp;<\/span>y<span class=\"br0\">&#41;<\/span>;<br \/>\n<br \/>\nprod = x <span class=\"sy0\">*<\/span> y;<br \/>\n<br \/>\n<span class=\"kw3\">printf<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;Product of %d and %d: %d&quot;<\/span>, x, y, prod<span class=\"br0\">&#41;<\/span>;<br \/>\n<br \/>\n<span class=\"kw3\">return<\/span> <span class=\"nu0\">0<\/span>;<br \/>\n<br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>After adding the code, press the \u201cctrl+o\u201d shortcut key to write the content to the file and then press the \u201cctrl+x\u201d to exit the file:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"731\" height=\"479\" class=\"wp-image-22805\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-13.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-13.png 731w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-13-300x197.png 300w\" sizes=\"auto, (max-width: 731px) 100vw, 731px\" \/><\/p>\n<p>Now to run the C Program, compile it first with the \u201cgcc\u201d command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">gcc<\/span> cexample.c <span class=\"re5\">-o<\/span> cexample<\/div><\/div>\n<p>The Command Line moves to the following line indicating the \u201cgcc\u201d compiles and creates a new executable file \u201ccexample\u201d.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"720\" height=\"47\" class=\"wp-image-22806\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-14.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-14.png 720w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-14-300x20.png 300w\" sizes=\"auto, (max-width: 720px) 100vw, 720px\" \/><\/p>\n<p>To Run the \u201ccexample\u201d file now, use the command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">.<span class=\"sy0\">\/<\/span>cexample<\/div><\/div>\n<p>The Terminal will Run the C Program and will ask you to enter any Two Numbers. Once the numbers are added, it will display the Product, i.e. The output in the Terminal:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"728\" height=\"106\" class=\"wp-image-22807\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-15.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-15.png 728w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-15-300x44.png 300w\" sizes=\"auto, (max-width: 728px) 100vw, 728px\" \/><\/p>\n<h2><strong>Method 2: Running a C Program in Ubuntu Using a Code Editor<\/strong><\/h2>\n<p>The C Program in Ubuntu can also be executed and run in a Code Editor. In our case, we will use the popular Visual Studio Code (VS Code) Editor. Visual Studio Code is not pre-installed in Ubuntu. Follow the steps below to install Visual Studio Code:<\/p>\n<h3><strong>Step 1: Installing Dependencies<\/strong><\/h3>\n<p>Install the \u201csoftware-properties-common\u201d and \u201capt-transport-https\u201d packages first before installing Visual Studio Code:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">sudo<\/span> apt <span class=\"kw2\">install<\/span> software-properties-common apt-transport-https <span class=\"kw2\">wget<\/span> <span class=\"re5\">-y<\/span><\/div><\/div>\n<p>The \u201csoftware-properties-common\u201d packages ensure the easy distribution of packages whereas the \u201capt-transport-https\u201d ensures the required repositories are accessed. In our case, both packages are installed to the newest version:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"921\" height=\"324\" class=\"wp-image-22808\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-16.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-16.png 921w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-16-300x106.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-16-768x270.png 768w\" sizes=\"auto, (max-width: 921px) 100vw, 921px\" \/><\/p>\n<h3><strong>Step 2: Downloading VS Code With Snap<\/strong><\/h3>\n<p>Once the packages are added, install the Visual Studio Code application with the \u201csnap\u201d command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">sudo<\/span> snap <span class=\"kw2\">install<\/span> <span class=\"re5\">--classic<\/span> code<\/div><\/div>\n<p>Executing the \u201c<strong>snap install<\/strong>\u201d command will start downloading \u201cVisual Studio Code\u201d:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"937\" height=\"481\" class=\"wp-image-22809\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-17.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-17.png 937w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-17-300x154.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-17-768x394.png 768w\" sizes=\"auto, (max-width: 937px) 100vw, 937px\" \/><\/p>\n<p>The Visual Studio Code once downloaded, will be installed as well and after the installation completes, the Terminal informs about the installation:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"924\" height=\"58\" class=\"wp-image-22810\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-18.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-18.png 924w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-18-300x19.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-18-768x48.png 768w\" sizes=\"auto, (max-width: 924px) 100vw, 924px\" \/><\/p>\n<p>After the Visual Studio Code is installed successfully, use the \u201ccode\u201d command to start VS Code from Terminal:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">code<\/div><\/div>\n<p>The \u201ccode\u201d command will start Visual Studio Code:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1920\" height=\"1080\" class=\"wp-image-22811\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-19.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-19.png 1920w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-19-300x169.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-19-1024x576.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-19-768x432.png 768w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-19-1536x864.png 1536w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/p>\n<h3><strong>Step 3: Configuring VS Code<\/strong><\/h3>\n<p>You will have to configure your Visual Studio Code now before running a C Program in it. In the \u201cGet Started with VS Code\u201d section, select the themes according to your choice and then click on the next section until you are done with the layout, themes fonts, etc. of your Visual Studio Code:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1920\" height=\"1080\" class=\"wp-image-22812\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-20.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-20.png 1920w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-20-300x169.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-20-1024x576.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-20-768x432.png 768w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-20-1536x864.png 1536w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/p>\n<p>Once you are done with the Themes, the \u201cStart\u201d section will appear. Click on the \u201cNew File\u201d option to create a New File in Visual Studio Code:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1562\" height=\"646\" class=\"wp-image-22813\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-21.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-21.png 1562w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-21-300x124.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-21-1024x423.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-21-768x318.png 768w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-21-1536x635.png 1536w\" sizes=\"auto, (max-width: 1562px) 100vw, 1562px\" \/><\/p>\n<p>A new page will open inside the Visual Studio WorkSpace. Click on the \u201cSelect a Language\u201d option:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1280\" height=\"200\" class=\"wp-image-22814\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-22.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-22.png 1280w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-22-300x47.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-22-1024x160.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-22-768x120.png 768w\" sizes=\"auto, (max-width: 1280px) 100vw, 1280px\" \/><\/p>\n<p>A drop-down menu appears:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"726\" height=\"485\" class=\"wp-image-22815\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-23.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-23.png 726w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-23-300x200.png 300w\" sizes=\"auto, (max-width: 726px) 100vw, 726px\" \/><\/p>\n<p>In the Drop-down menu, click on the language you want to select. In our case, we will select the \u201cC\u201d option which is for C Programming Language:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"669\" height=\"499\" class=\"wp-image-22816\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-24.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-24.png 669w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-24-300x224.png 300w\" sizes=\"auto, (max-width: 669px) 100vw, 669px\" \/><\/p>\n<p>Now before writing the C Program, ensure that the C Program Extensions are added to the Visual Studio Code WorkSpace. In the left navigation menu of Visual Studio Code, Click on the Extensions Option:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"302\" height=\"271\" class=\"wp-image-22817\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-25.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-25.png 302w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-25-300x269.png 300w\" sizes=\"auto, (max-width: 302px) 100vw, 302px\" \/><\/p>\n<p>The Extensions Menu will appear where you can see the installed extensions on your Visual Studio Code. At the top of the Extensions Menu, click on the \u201csearch bar\u201d:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"293\" height=\"107\" class=\"wp-image-22818\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-26.png\" \/><\/p>\n<p>Now type in the extension name you want to add to your Visual Studio Code. In our case, we will type C\/C++ and the filtered extensions will appear. Click on the first extension option from the Menu and then click on \u201cInstall\u201d to start installing the Extension:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1802\" height=\"334\" class=\"wp-image-22819\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-27.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-27.png 1802w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-27-300x56.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-27-1024x190.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-27-768x142.png 768w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-27-1536x285.png 1536w\" sizes=\"auto, (max-width: 1802px) 100vw, 1802px\" \/><\/p>\n<p>After the installation of the extension completes, the extension will automatically be added to your Visual Studio Code.<\/p>\n<h3><strong>Step 4: Running a C Program<\/strong><\/h3>\n<p>Once you are done with the Visual Studio Configuration, you can now create a C Program in it. To add a C Program, open the file and add the code below:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"co0\">#include &lt;stdio.h&gt;<\/span><br \/>\n<br \/>\nint main<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<br \/>\n<span class=\"br0\">&#123;<\/span><br \/>\n<br \/>\nint x, y, prod;<br \/>\n<br \/>\n<span class=\"kw3\">printf<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;Enter 2 numbers for multiplication<span class=\"es1\">\\n<\/span>&quot;<\/span><span class=\"br0\">&#41;<\/span>;<br \/>\n<br \/>\nscanf<span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;%d %d&quot;<\/span>, <span class=\"sy0\">&amp;<\/span>x, <span class=\"sy0\">&amp;<\/span>y<span class=\"br0\">&#41;<\/span>;<br \/>\n<br \/>\nprod = x <span class=\"sy0\">*<\/span> y;<br \/>\n<br \/>\n<span class=\"kw3\">printf<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;Multiplication of %d and %d is %d<span class=\"es1\">\\n<\/span>&quot;<\/span>, x, y, prod<span class=\"br0\">&#41;<\/span>;<br \/>\n<br \/>\n<span class=\"kw3\">return<\/span> <span class=\"nu0\">0<\/span>;<br \/>\n<br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>After adding the code, in the top navigation menu of Visual Studio Code, click on the \u201cRun\u201d option:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"967\" height=\"416\" class=\"wp-image-22820\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-28.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-28.png 967w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-28-300x129.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-28-768x330.png 768w\" sizes=\"auto, (max-width: 967px) 100vw, 967px\" \/><\/p>\n<p>A drop-down menu will appear where you can start debugging your program. Click on the \u201cStart Debugging\u201d option from the Menu:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"449\" height=\"530\" class=\"wp-image-22821\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-29.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-29.png 449w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-29-254x300.png 254w\" sizes=\"auto, (max-width: 449px) 100vw, 449px\" \/><\/p>\n<p>The Visual Studio Code will now ask for the Configuration in which the program will be compiled and executed. Among the list, click on the \u201cC\/C++: gcc build and debug active files\u201d:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1022\" height=\"172\" class=\"wp-image-22822\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-30.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-30.png 1022w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-30-300x50.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-30-768x129.png 768w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-30-1020x172.png 1020w\" sizes=\"auto, (max-width: 1022px) 100vw, 1022px\" \/><\/p>\n<p>After selecting the Configuration, your C Program will execute in the Visual Studio Code Terminal seen at the bottom of the VS Code Application:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1077\" height=\"251\" class=\"wp-image-22823\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-31.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-31.png 1077w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-31-300x70.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-31-1024x239.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-31-768x179.png 768w\" sizes=\"auto, (max-width: 1077px) 100vw, 1077px\" \/><\/p>\n<p>In our program, the user is asked to enter two numbers for multiplication:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"814\" height=\"201\" class=\"wp-image-22824\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-32.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-32.png 814w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-32-300x74.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-32-768x190.png 768w\" sizes=\"auto, (max-width: 814px) 100vw, 814px\" \/><\/p>\n<p>After adding both the numbers and hitting \u201cEnter\u201d, the Output of the C Program can be seen in the VS Code Terminal:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"912\" height=\"197\" class=\"wp-image-22825\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-33.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-33.png 912w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-33-300x65.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/10\/word-image-22791-33-768x166.png 768w\" sizes=\"auto, (max-width: 912px) 100vw, 912px\" \/><\/p>\n<p>This is how you can Run a C Program in Ubuntu using a Code Editor like Visual Studio Code.<\/p>\n<h2><strong>Conclusion<\/strong><\/h2>\n<p>C Program in Ubuntu can be run either in the Terminal with the \u201cgcc\u201d command or can be run using any code editor like Visual Studio Code. Both the methods to run a C Program in Ubuntu are discussed in detail in this article.<\/p>","protected":false},"excerpt":{"rendered":"<p>C Program in Ubuntu can be run either in the Terminal with the \u201cgcc\u201d command or can be run using any code editor like Visual Studio Code. <\/p>","protected":false},"author":110,"featured_media":22802,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-22791","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ubuntu"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/22791","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/110"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=22791"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/22791\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/22802"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=22791"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=22791"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=22791"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}