For working professionals
For fresh graduates
More
Explore C Tutorials: From Begi…
1. Introduction to C Tutorial
2. Addition of Two Numbers in C
3. Anagram Program in C
4. Armstrong Number in C
5. Array in C
6. Array of Pointers in C
7. Array of Structure in C
8. C Program to Find ASCII Value of a Character
9. Assignment Operator in C
10. Binary Search in C
11. Binary to Decimal in C
12. Bitwise Operators in C
13. Boolean in C
14. C Compiler for Mac
15. C Compiler for Windows
16. C Function Call Stack
17. C Language Download
18. Operators in C
19. C/C++ Preprocessors
20. C Program for Bubble Sort
21. C Program for Factorial
22. C Program for Prime Numbers
23. C Program for String Palindrome
24. C Program to Reverse a Number
25. Reverse a String in C
26. C string declaration
27. String Input Output Functions in C
28. Calculator Program in C
29. Call by Value and Call by Reference in C
30. Ceil Function in C
31. Coding Vs. Programming
32. Command Line Arguments in C/C++
33. Comments in C
34. Compilation process in C
35. Conditional Statements in C
36. Conditional operator in the C
37. Constant Pointer in C
38. Constants in C
39. Dangling Pointer in C
40. Data Structures in C
41. Data Types in C
42. Debugging C Program
43. Convert Decimal to Binary in C
44. Define And include in C
45. Difference Between Arguments And Parameters
46. Difference Between Compiler and Interpreter
47. Difference Between If Else and Switch
48. Do While Loop In C
49. Double In C
50. Dynamic Array in C
51. Dynamic Memory Allocation in C
52. Enumeration (or enum) in C
53. Evaluation of Arithmetic Expression
54. Factorial of A Number in C
55. Features of C Language
56. Fibonacci Series Program in C Using Recursion
57. File Handling in C
58. For Loop in C
59. Format Specifiers in C
60. Functions in C
61. Function Pointer in C
62. goto statement in C
63. C Hello World Program
64. Header Files in C
65. Heap Sort in C Program
66. Hello World Program in C
67. History of C Language
68. How to compile a C program in Linux
Now Reading
69. How to Find a Leap Year Using C Programming
70. Identifiers in C
71. If Else Statement in C
72. If Statement in C
73. Implementation of Queue Using Linked List
74. Increment and decrement operators in c
75. Input and Output Functions in C
76. How To Install C Language In Mac
77. Jump Statements in C
78. Lcm of Two Numbers in C
79. Length of an Array in C
80. Library Function in C
81. Linked list in C
82. Logical Operators in C
83. Macros in C
84. Matrix multiplication in C
85. Nested if else statement in C
86. Nested Loop in C
87. One Dimensional Array in C
88. Operator Precedence and Associativity in C
89. Overflow And Underflow in C
90. Palindrome Program in C
91. Pattern Programs in C
92. Pointer to Pointer in C
93. Pointers in C: A Comprehensive Tutorial
94. Pre-increment And Post-increment
95. Prime Number Program in C
96. Program for Linear Search in C
97. Pseudo-Code In C
98. Random Access Files in C
99. Random Number Generator in C
100. Recursion in C
101. Relational Operators in C
102. Simple interest program in C
103. Square Root in C
104. Stack in C
105. Stack Using Linked List in C
106. Static function in C
107. Stdio.h in C
108. Storage Classes in C
109. strcat() in C
110. Strcmp in C
111. Strcpy in C
112. String Comparison in C
113. String Functions in C
114. String Length in C
115. String Pointer in C
116. strlen() in C
117. Structures in C
118. Structure of C Program
119. Switch Case in C
120. C Ternary Operator
121. Tokens in C
122. Toupper Function in C
123. Transpose of a Matrix in C
124. Two Dimensional Array in C
125. Type Casting in C
126. Types of Error in C
127. Unary Operator in C
128. Use of C Language
129. User Defined Functions in C
130. What is Variables in C
131. Is C language case sensitive
132. Fibonacci Series in C
Wondering how to compile C program in Linux?
Understanding C program compilation in Linux is an essential skill for anyone interested in C programming, whether you're a beginner or an expert. This article will provide a detailed step-by-step tutorial on how to compile a C program in a terminal, in addition to providing a C program in Linux example. We'll also discuss how to compile C++ programs in Linux and how to run a program in Ubuntu terminal.
If you want to download this tutorial in PDF format for further reading: Download Tutorial PDF
C is a popular and powerful programming language known for its flexibility and efficiency. Many computer systems, including Linux, are written primarily in C. Knowing how to compile and run C programs is crucial in learning and using the language effectively. This article aims to demystify the process of compiling and running C programs in Linux.
The build-essential package is a collection of essential software and libraries required for building Debian packages on Debian-based systems like Ubuntu. It includes software like make, gcc and other required libraries necessary for compiling code.
While programming in C or C++, a compiler is needed to turn the human-readable code that you write into machine code that a computer can understand. The most commonly used compiler for C and C++ on Linux is GCC, short for GNU Compiler Collection. This compiler is one of the many tools included in the build-essential package.
The build-essential package includes the GCC compiler and g++ (the C++ compiler), and make. The 'make' utility helps manage and automate the compilation of programs beyond the simple programs you'd write in the beginning. As your program grows, you would split it into multiple files that need to be compiled separately and then linked together. The make utility automates this process. It's able to track which files have been changed and need to be recompiled and can perform the compilation and linking steps in the correct order.
It also includes additional utilities such as dpkg-dev which contains tools necessary for building Debian packages, and libc6-dev, the libraries and header files where standard functions (like printf, scanf, etc.) are defined.
In summary, if you plan on programming or compiling software from source code on Linux, the build-essential package will likely be a necessary addition. Without it, your system wouldn't have the necessary tools for code compilation and software creation, thus, it is an essential step in the process of setting up a C or C++ development environment on a Linux machine.
Follow these steps to install the build-essential package on your Linux machine:
Step 1: Open your terminal.
Step 2: Update your package lists for upgrades for packages that need upgrading, as well as new packages that have just come to the repositories. Run the following command: sudo apt update.
Step 3: Install the build-essential package by executing this command: sudo apt install build-essential.
Step 4: After you've installed the build-essential package, you can compile C programs in your Linux environment.
Once your Linux environment is ready for C programming, you can create your first C program. Use the text editor of your choice, such as nano or vim.
For this example, we'll use nano:
Step 1: Open your terminal.
Step 2: Type nano hello.c to create and open a new C file named myfile.c.
Step 3: In the text editor, write the following program:
#include <stdio.h> |
Step 4: Press Ctrl+O, then Enter to save the file, and Ctrl+X to exit the editor.
To compile the C program you just wrote, use the gcc compiler:
Step 1: Open your terminal, navigate to the directory containing your C file using the cd command.
cd Documents |
Step 2: Type gcc myfile.c -o hello to compile your C file. The -o hello part of the command names the output file "hello".
gcc myfile.c -o hello |
Step 3: Run your program by typing ./myfile.
You should see the output Hello, World! in your terminal, indicating that your C program has successfully compiled and run.
Visual Studio Code (VS Code) is a popular code editor that supports C/C++ programming. It provides a rich environment for coding, debugging, and problem-solving.
Here are the steps to set up Visual Studio Code for C programming in Linux:
Step 1: Download and install Visual Studio Code from the official site.
Step 2: Launch VS Code.
Step 3: Install the C/C++ extension by Microsoft from the Extensions view (Ctrl+Shift+X).
Step 4: Open your C file with VS Code, or create a new one using the New File option.
Step 5: Write your C code in the editor.
\ Use the built-in terminal (Ctrl+`) to compile and run your C program as we described in the previous section.
After learning how to compile a C program in Linux, it's crucial to know how to run C program in Linux online. Specifically, we'll look at how to run C program in Linux Ubuntu terminal, one of the most popular Linux distributions.
1. After writing and saving your C program, open your terminal.
2. Navigate to the directory containing your C file using the cd command.
3. Compile the C program with gcc filename.c -o outputname, replacing "filename" with the name of your file and "outputname" with the name you want your output file to have.
4. Run the compiled program by typing ./outputname into the terminal.
You should see the output of your program displayed in the terminal.
While this guide primarily focuses on C, you might also wonder how to compile C++ program in Linux. The process is similar to compiling a C program, with the primary difference being the use of g++ (the GNU C++ compiler) instead of gcc.
1. Write your C++ program and save it with a .cpp extension.
2. Open your terminal and navigate to the directory containing your C++ file.
3. Compile the program with g++ filename.cpp -o outputname.
4. Run the compiled program by typing ./outputname into the terminal.
The output of your C++ program should now appear in the terminal.
There might be situations where you want to compile and run C programs in Linux online without installing any software on your local machine. Several online platforms, such as Repl.it, JDoodle, and others allow you to do this.
1. Navigate to your preferred online platform (e.g., repl.it) or the JDoodle website (jdoodle.com).
2. Create a new C programming environment.
3. Write your C program in the provided text editor.
4. Compile and run the program using the platform's built-in tools.
You should see the output of your program displayed on the website.
After you've learned how to compile and run a C program in Linux Ubuntu terminal, the next step is understanding the output. The printf function in C is used to output text to the terminal. For example, a printf("Hello, World!"); statement would output the text "Hello, World!" when the program is run.
Remember that the output you see in the terminal is the result of your program's execution. If your program has logic errors, the output might not be what you expect. Debugging tools and careful programming practices can help ensure your program produces the correct output.
Remember to study and understand your C program output in Linux Ubuntu terminal, as it's a critical part of learning and improving as a programmer.
Mastering how to compile a C program in Linux is a fundamental step in your programming journey. Whether you want to learn how to compile C program in terminal or use an integrated development environment like Visual Studio Code, you are enhancing your skill set and deepening your understanding of C programming on a Linux platform.
At upGrad, we offer courses tailored to make you proficient in programming, with practical, hands-on experience and mentorship from industry experts. With courses like Full Stack Software Development Bootcamp, you get expert guidance with an immersive learning experience, imparting in-demand skills to help your career become future-proof!
1. How can I run a C program in Linux online?
Several online platforms provide Linux environments where you can compile and run C programs. Some of these include Repl.it, JDoodle, and others.
2. How can I run a C program in Linux Ubuntu terminal?
First, you need to compile the program using the gcc compiler (gcc filename.c -o outputname). Then you can run it using ./outputname.
3. Can I compile and run C++ programs in Linux?
Yes, you can use the g++ compiler that comes with the build-essential package to compile and run C++ programs in Linux.
4. Can I write and compile C programs in Visual Studio Code on Linux?
Yes, Visual Studio Code supports C programming on Linux. You can install the C/C++ extension to enhance the coding experience with features like Intellisense and debugging.
Take a Free C Programming Quiz
Answer quick questions and assess your C programming knowledge
Author
Start Learning For Free
Explore Our Free Software Tutorials and Elevate your Career.
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.