Compile And Run C Code in Linux

Method 1: Using CC Compiler

In this method, we will be compiling and executing the C program code using CC Compiler.

Step 1: First, we need to open the text editor and terminal for writing code and executing it through the terminal.

Step 2: In the text editor we need to write any code using a C programming language.

Example Script:

C




#include <stdio.h>
 
int main() {
 
    printf("welcome to geeks for geeks");
    return 0;
}


We have written a simple C program to print the “welcome to geeks for geeks” message.

Step 3: Now, we need to save the file with the .c extension. So in this example, we have saved the file as welcome.c.

Step 4: Now compile and run the C code in the terminal using the commands below.

ls
cc welcome.c
./a.out

cc Compiler

Our written code has been executed and the output is displayed in the above screenshot.

Method 2: Using GCC Compiler

We can also compile and execute the script using the GCC compiler. So, let’s understand the compilation and execution using GCC example below.

Example Script:

C




#include <stdio.h>
 
int main() {
 
    // code
  int a=10,b=20,c;
  c=a+b;
  printf("addition of=%d\n",c);
  return 0;
}


Step 1: Navigate to the directory where the file is been saved. Use the below commands.

In this example we are already in the current directory.

Step 2: Execute the command below for compilation and execution.

cc -o add add.c
./add

GCC compiler

In the above image, we have written a simple C program for the addition of two numbers.

How To Compile And Run a C/C++ Code In Linux

C Programming Language is mainly developed as a system programming language to write kernels or write an operating system. C++ Programming Language is used to develop games, desktop apps, operating systems, browsers, and so on because of its performance. In this article, we will be compiling and executing the C Programming Language codes and also C++ codes with various compilers like CC, GCC, and G++ compilers.

Similar Reads

Compile And Run C Code in Linux

Method 1: Using CC Compiler...

Compile And Run C++ Code in Linux

...

Conclusion

...

Contact Us