Writing Your First Program in C Language


The C programming language is one of the oldest and most influential programming languages ever created. Many modern languages such as C++, Java, and Python have roots in C. If you're just starting your journey in programming, learning C is a great first step because it teaches you the fundamentals such as program structure, memory management, and input/output handling.

Why Choose C?

C is used for a variety of purposes—from developing operating systems, embedded software, to building compilers and developer tools. Its advantages include:

  • Fast and efficient
  • Close to the hardware
  • Widely used in the industry
  • A strong foundation for learning other languages

Now, let’s write our very first C program!

Getting Started

Before writing a program, make sure you’ve installed a C compiler on your computer. Some common compilers include:

  • GCC (GNU Compiler Collection) – Available on Linux, Windows, and FreeBSD
  • Clang – Default compiler on macOS and FreeBSD
  • Turbo C/C++ – Classic, but rarely used nowadays

To write your code, you can use editors like:

  • VS Code
  • Vim / Neovim
  • Emacs
  • Simple text editors (nano, ee, etc.)
  • IDEs like Code::Blocks

Writing Your First C Program: "Hello, World!"

The classic first program in almost every language is printing "Hello, World!" to the screen. Here’s a simple C program to do that:

#include <stdio.h>

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

Code Explanation:

  • #include <stdio.h>: Imports the standard input/output library so we can use the printf() function.
  • int main(): The main function where program execution begins.
  • printf("Hello, World!\n");: Prints text to the screen.
  • return 0;: Indicates the program has finished successfully.

How to Run the Program

On FreeBSD

If you're using FreeBSD, you can use clang or gcc, both available through Ports or pkg:

pkg install gcc

Or use the built-in clang compiler:

  1. Save the code in a file named hello.c
  2. Open the terminal and navigate to the directory where the file is saved
  3. Run:
cc hello.c -o hello
./hello

If using gcc:

gcc hello.c -o hello
./hello

Expected output:

Hello, World!

On Linux / Windows / macOS

The steps are generally similar. On Linux/macOS:

gcc hello.c -o hello
./hello

On Windows (using MinGW):

gcc hello.c -o hello.exe
hello

Try It Yourself

Try modifying the program:

  • Change "Hello, World!" to your own name
  • Add another line to print a math expression:
printf("2 + 3 = %d\n", 2 + 3);

Conclusion

Writing your first program is a big milestone in your journey as a programmer. C may look simple, but it forms a solid foundation for understanding how computers work under the hood.

Once you've successfully printed "Hello, World!", you're ready to explore data structures, control flow, functions, and many more C features. Keep going and enjoy the process!

Post a Comment

0 Comments