Write and Run Your First C Program

CCBeginner
Practice Now

Introduction

C programming language is a popular choice for beginners to learn computer programming. It provides a solid foundation in structured and procedural programming, which can be helpful when learning other programming languages later on. This lab will guide you through writing and running your first C program on Linux.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/BasicsGroup(["`Basics`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/BasicsGroup -.-> c/data_types("`Data Types`") c/BasicsGroup -.-> c/operators("`Operators`") c/FunctionsGroup -.-> c/function_declaration("`Function Declaration`") subgraph Lab Skills c/output -.-> lab-136074{{"`Write and Run Your First C Program`"}} c/data_types -.-> lab-136074{{"`Write and Run Your First C Program`"}} c/operators -.-> lab-136074{{"`Write and Run Your First C Program`"}} c/function_declaration -.-> lab-136074{{"`Write and Run Your First C Program`"}} end

Write and Run Your First C Program

In this lab, you will learn how to write and run your first C program.

  1. Create a new file and save it with the extension .c, for example first.c.

  2. In the new file, add the following code:

    #include <stdio.h>
    
    int main() {
        printf("Learning C\n\n\n");
        return 0;
    }
  3. Save the file and close the code editor.

  4. Open a terminal and navigate to the directory where you saved the first.c file.

  5. Run the following command to compile the C program:

    gcc first.c -o first_program
    • gcc: the GNU Compiler Collection command for compiling C programs
    • first.c: the name of your C source file
    • -o first_program: the option to specify the desired name for the compiled executable
  6. After the compilation process completes successfully, run the following command to execute the program:

    ./first_program
  7. You should see the output Learning C displayed in the terminal.

Summary

After completing this lab, you will be able to write and run a basic C program. This will serve as a starting point for learning more about C programming and building more complex applications in the future.

Other C Tutorials you may like