"Hello, world!" in C

The "Hello, World!" program is a simple yet essential first step when learning a new programming language. In this tutorial, we will guide you through the process of writing and executing the "Hello, World!" program in C on Windows, Linux, and Mac operating systems. This tutorial assumes you have a basic understanding of programming concepts and have a C compiler installed on your system.

Let's get started!

C in a nutshell

C is a general-purpose programming language that was developed in the early 1970s by Dennis Ritchie at Bell Laboratories. It is one of the most widely used programming languages and has had a significant influence on the development of many other languages.

C was originally designed as a systems programming language for developing operating systems, but its simplicity and efficiency made it popular for a wide range of applications. It provides low-level access to memory and hardware, allowing developers to write code that executes quickly and efficiently. C's syntax is relatively simple and straightforward, making it easy to learn and understand.

One of the key features of C is its ability to manipulate memory directly through the use of pointers. Pointers allow programmers to work with memory addresses, enabling efficient memory management and the implementation of data structures and algorithms.

C is a procedural language, which means that programs are structured around functions that perform specific tasks. It supports a rich set of built-in operators and functions for performing arithmetic, logical, and bitwise operations, as well as input and output operations.

Another important feature of C is its portability. C programs can be compiled to run on a wide range of platforms and operating systems, making it a versatile choice for cross-platform development.

Over the years, C has been standardized by organizations such as the International Organization for Standardization (ISO) and the American National Standards Institute (ANSI). The most widely used version of the language is known as C89 or C90, which sets the basic features and syntax of the language. Subsequent versions, such as C99 and C11, introduced additional features and improvements.

C has a large and active community of developers, and there are numerous libraries and frameworks available for various purposes. It is commonly used in areas such as embedded systems, device drivers, game development, and high-performance computing.

Despite its age, C remains a popular choice for many programmers due to its efficiency, flexibility, and widespread adoption. It provides a solid foundation for learning programming concepts and serves as a building block for more advanced languages and technologies.

Step 1: Writing the Code

Open a text editor of your choice and create a new file. Save it with a ".c" extension, such as hello.c. In this file, enter the following code:

#include <stdio.h>

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

The code includes the standard input/output library <stdio.h>, defines the main() function as the program's entry point, and uses the printf() function to display the "Hello, World!" message on the console. The return 0 statement indicates that the program executed successfully.

Step 2: Compiling and Executing in Windows

For Windows, we recommend using the MinGW-w64 compiler, which provides a Windows port of the GNU Compiler Collection (GCC). Follow these steps to compile and execute the program:

  1. Open the Command Prompt: Press Win + R, type "cmd", and hit Enter.
  2. Navigate to the directory where you saved the hello.c file using the cd command.
  3. Compile the code by entering the following command: gcc hello.c -o hello.exe
  4. If the compilation is successful, run the program with the command: hello.exe
  5. You should see the "Hello, World!" message displayed on the console.

Step 3: Compiling and Executing in Linux and Mac

Both Linux and Mac systems come with the GCC compiler pre-installed, allowing you to compile and execute C programs from the terminal. Here's how:

  1. Open a terminal.
  2. Navigate to the directory where you saved the hello.c file using the cd command.
  3. Compile the code by entering the following command: gcc hello.c -o hello
  4. If the compilation is successful, run the program with the command: ./hello
  5. The "Hello, World!" message should be displayed on the terminal.

Conclusion

Congratulations! You have successfully written and executed the "Hello, World!" program in C on Windows, Linux, and Mac. This program serves as a fundamental building block for learning C programming and serves as a starting point for more complex applications. Experiment with the code and explore the vast possibilities offered by the C programming language. Happy coding!

Explore other "Hello, World!" examples