Creating Your First C Program: Hello World

Every aspiring software developer begins their journey with a "Hello World" program in their chosen programming language. In this chapter, we will guide you through writing a simple "Hello World" program in C. Make sure your C programming environment is set up with the GCC compiler, a text editor, and ideally, an IDE like CodeBlocks for a smooth coding



C - Hello World

Every aspiring software developer starts with a "Hello World" program in their chosen programming language. In this chapter, we will learn how to write a "Hello World" program in C.

Hello World in C Language

Ensure your C programming environment is set up, including the GCC compiler, a text editor, and preferably an IDE like CodeBlocks.

Example

Open a text editor (e.g., Notepad or Notepad++) and enter the following code. Save it as "hello.c".

Syntax

#include 

int main(){
   /* my first program in C */
   printf("Hello World! \n");
   return 0;
}
Output

Hello World!

Step-by-Step Execution

  1. #include statement: Imports the stdio.h file, which contains the definitions of standard input-output library functions, including printf().
  2. main() function: Prints the "Hello World" message. Comments are ignored by the compiler. Each statement ends with a semicolon (;).
  3. Return value: main() returns an integer (0) to indicate successful execution.

Compiling and Running

To compile the source code ("hello.c") and create an executable:

Windows: Open the command prompt in the folder where "hello.c" is saved and run:

Command

gcc -c hello.c -o hello.o
gcc -o hello.exe hello.o

Run the executable:

Command

hello

Linux: Give the object file executable permission and run it:

Command

chmod a+x hello.o
./hello.o

Using CodeBlocks IDE

CodeBlocks is a popular IDE for C/C++ development. Install and open it. Create a new file, enter the following code, and save it as "hello.c".

Syntax

#include 

int main(){
   /* my first program in C */
   printf("Hello World! \n");
   return 0;
}
Output

Hello World!

Choose "Build and Run" from the Build menu or press F9. The output will display "Hello World!".

Successfully running the "Hello World" program confirms that your C programming environment is working properly.

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.