How to program "Hello, World!" in C Language

Learn how to write "Hello, World!" in C programming language!

Getting Started

General Requirements

Text Editors

A few recommendations of mine are Notepad++, Sublime, Atom, Terminal or Visual Studio. Each of these are sufficient to complete the objectives of this course. You must have access to a working computer.

Assumptions

You should have a basic understanding of text editors (or IDE's) and compilation of executable programs. This purpose of this course is not to show you how to use a text editor or execute C language programs, but how to write proper executable C language. Writing proper language within a text editor, allows you to execute (run, compile) the file within an allowable environment like iMac's built in terminal.

Will this course show you how to use a text editor?

  • Yes
  • No

Will you be executing the code you learn how to write?

  • Yes
  • No

Choosing the Right Library

About C Libraries

C Libraries

The C programming language is widely popular among systems and hardware engineering. Compared to Javascript, Python, and many other modern programming languages, it is a low-level language. This means it is closer to the machine language of a computer or even the assembly language. Because C is a low level language it offers several libraries that may not be necessary. For the purpose of printing "Hello, World!" to the screen, we use the most basic and necessary library of C. 

#include <stdio.h>

The pound sign means "directive," so we are including a directive called "<stdio.h>." Any library, stated in the angular brackets is a C source directive, and with any directive comes C packages and options that allow programming to go above and beyond simple input and output. "<stdio.h>" contains the standard "scanf" and "printf" functions as well as other standard input and output functions. We will learn about "printf" later.

Final Thoughts

Without going too far in depth with what the functions do and how to use them in C, you only need to use "#include <stdio.h>" in order to print "Hello, World!" in C. And, this is because <stdio.h> contains everything you need to get those magic words on the screen.

In order to use printf, we do not need to include any specific library.

  • False, we need to use the <stdio.h> library.
  • True, there are other libraries that include printf other than <stdio.h>

Creating the Main Function

What is the Purpose of the Main Function?

Functions, Generally

A function in programming is a method to complete a task within code. Whether that task is to perform algebra or sort names in a file, the purpose of a function can vary widely.

The main( ) Function

The main( ) function is a requirement of every C code. Other than that, it's not a special function. In fact, as you progress in your C programming, you will use several functions along with your main( ) function.

What are those Parentheses for, after the "main"?

For the purpose of this course, we won't be writing anything other than "( )" following the "main." However, as you learn C, more advanced methods of functions use arguments within those parentheses, and these arguments are used within that function. Particularly, for the main( ) function, we have the option to pass in command line arguments in the form of (int argc, char** argv).

Using the main( ) function

While advancing with the C language, you'll notice many different methods of how programmers use the main( ) function. It is good practice to not place all of your code within the main( ) function as this can become unclear as to the purpose of the program. But, as for the purpose of this course, simply writing "Hello, World!" fits cleanly in the main( ) function.

Writing the Outline

The Type of the Main() Function

Each function written in C must have a return type. The main function's return type will return a value to the command line. We will create our main function to be of type "int," and the return type will be set to '0.' So, each function with a return type of "int" must have "return 0;" just before the end of the scope of the function.

The Scope of a Function

The scope of a function defines the bounds for which the variables and methods defined in it, are valid. The scope of a function begins with '{' and ends with '}'. Every function must have a scope or the program will not compile correctly.

The Main Function Outline

int main( ){


return 0;

}

Choose the correct outline of the main function

Formatting the Correct Output

The "printf" Function

What is printf?

To print "Hello, World!" to the screen, we will use a function that is included in our library <stdio.h>. This function is printf, which is short for print formatted string. A string is a collection of characters, for example " 'h', 'e', 'l', 'l', 'o' ". So, if we want to print "Hello, World!" to the screen, then we need to use printf.

The Associated Syntax

The general outline to use printf is:

printf(" ");

The message we want to print to the screen must be placed inside the double quotation marks, and the semicolon denotes the end of a C statement.

How will we use it?

printf("Hello, World!");

What is printf?

  • A function that prints a string (collection of characters)
  • A return type commonly associated with functions
  • Syntax that denotes the end of a statement

A Final Test

Which library do you need to use for this course?

  • #include<string.h>
  • #include "stdio.h"
  • #include<fstream>
  • #include<stdio.h>

What is the type of our main function?

  • char
  • string
  • int

How do you put together the proper printf statement for this course? (Fill in the blanks from the dropdown)

printf( "Hello, World!); 

Which of these images illustrate the proper C language of "Hello, World!"