Finding something worth knowing…

Technology

How to write your first program in C using the classic Hello World method

Brian Kernighan, a legendary figure in computer science, famously popularized the Hello World program. By writing this simple code, you begin to build the essential muscle memory required for programming. Follow this guide to set up your environment, write your first lines of C, and verify your code works correctly.

The Hello World program is a foundational rite of passage for programmers, implemented in hundreds of languages worldwide. Its simplicity belies its importance as a starting point for understanding how source code is transformed into machine code. Brian Kernighan, a professor at Princeton University and a co-author of the seminal K&R book, has long championed this approach to learning. In 2010, he returned to Harvard University's CS50 course to discuss numeric self-defense, reflecting his deep involvement in the intellectual enterprise of computer science education.

To begin, you must organize your workspace by creating a directory and a specific file. In a terminal environment, you create a folder named world and then generate a file called hello.c. Within this file, you write the C code that instructs the computer to print the phrase hello, world to the screen. This process is not merely about the output; it is about the physical act of typing the syntax, which helps the brain develop the necessary muscle memory for future coding tasks.

Once the code is written, the next phase involves compilation and testing. You use a compiler to translate your source code into executable machine code. If errors occur, tools like help50 provide guidance on how to resolve them. After successful compilation, you can run the program directly. Further validation is possible through automated tools like check50, which verifies correctness, and style50, which offers suggestions to improve the readability and structure of your code. This systematic approach ensures that even the simplest program is built upon a solid foundation of testing and refinement.

Source: CS50 Lecture by Brian Kernighan

More in Technology · All topics