C Tutorials – Print text to console

Let’s print some text to the console!

  • Ensure you have a C compiler and text editor installed on your computer.
  • I recommend GCC Compiler as it has been ported to Windows, OS X, and Unix-based and Unix-like (Linux) systems.
  • My favorite Windows port of GCC is MinGW

 

Gimme some code!

#include <stdio.h>

int main (void) {

printf(“Hello World!”);

return 0;

}

 

Let’s walk through this!

Line 1: #include <stdio.h>

The first line of code tells the compiler that we’re using code from the Standard I/O library. In technical terms, we’re including the stdio.h header file as apart of the program. This is where the printf function is defined. More on printf later.

 

Line 2: int main (void) {

This line of code is the main function. This is the entry point of your program.

 

Line 3: printf(“Hello World!”);

This line of code will print text to the console.

 

Line 4: return 0;

This line of code return code zero. Code zero tells the Operating System that the program did not crash and was successfully executed

 

Please note: Most common C compilers should be able to compile the above code as-is (e.g. GCC, Microsoft Visual Studio, Borland C/C++, etc.)

Advertisements

Comments

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s