How to write your First C Program Language

Writing your first C program is an exciting step in your journey as a programmer. In C, the traditional first prog

ram is one that prints “Hello, World!” to the screen. Here’s a step-by-step guide to creating and running your first C program:

Step 1: Set Up Your Development Environment

Before you can write and run C programs, you’ll need a C compiler and a text editor or an integrated development environment (IDE). As mentioned earlier, you can use tools like GCC (GNU Compiler Collection) on Linux and macOS or MinGW on Windows. For editing, you can use simple text editors like Notepad (Windows), Vim (Linux/macOS), or code editors like Visual Studio Code or Code::Blocks.

Step 2: Write Your C Code

Open your text editor or IDE and create a new file. In this file, type the following C code:

#include <stdio.h>

int main() {
    printf("Hello, World!\n");
    return 0;
}

Here’s a breakdown of the code:

  • #include <stdio.h>: This line includes the standard input/output library, which provides the printf function for printing text.
  • int main() { ... }: This is the main function where the program execution begins.
  • printf("Hello, World!\n");: This line prints “Hello, World!” followed by a newline character (\n) to the screen.
  • return 0;: The main function returns an integer value (usually 0) to indicate a successful execution.

Step 3: Save Your C File

Save the file with a .c extension, such as hello.c. Make sure you remember where you saved it.

Step 4: Compile Your C Program

Open your command prompt or terminal and navigate to the directory where you saved your hello.c file.

Compile the program using the C compiler. For GCC, you can use the following command on Linux or macOS:

gcc hello.c -o hello

On Windows with MinGW:

gcc hello.c -o hello.exe

This command tells the compiler to compile hello.c and generate an executable file named hello (or hello.exe on Windows).

Step 5: Run Your C Program

After a successful compilation, you can run your program from the command prompt or terminal:

On Linux/macOS:

./hello

On Windows:

hello.exe

You should see the output:

Hello, World!

Discover more from PiEmbSysTech

Subscribe to get the latest posts sent to your email.

Leave a Reply

Scroll to Top

Discover more from PiEmbSysTech

Subscribe now to keep reading and get access to the full archive.

Continue reading