Copyright
Computer Science Courses / Course / Chapter

Practical Application for C Programming: ~'Hello World~' Program

Instructor: Martin Gibbs

Martin has 21 years experience in Information Systems and Information Technology, has a PhD in Information Technology Management, and a master's degree in Information Systems Management. He is an adjunct professor of computer science and computer programming.

In this lesson, we are going to write our first C program, called 'Hello World.' This small program highlights the basics of developing a C program and displaying output to the user.

Lesson Overview & Knowledge Required

This is one of the first programs you will write using C! We will be starting from ground level, describing each line and keyword. Therefore, very little background coding knowledge is required.

You will want to create a new C source file for this program. Let's use a free tool called Dev C++. Simply create a new source file for C, name it hello.c, and paste the code into the tool. The first time you execute the code, you will want to compile it, just to check for errors.

Click the compile button:


Dev C++ compile


Click compile and run:


Dev C++ compile and run


Program Code

This is the program code that you will paste into the tool.

//Hello World Program!
#include <stdio.h>
int main() {
  printf("Hello World");
  return 0;
}

The first statement is a comment and will not be run by the program. It is recommended to use comments so that you, or other programmers, know what is happening in the code. For complicated programs, others (and your future self), will thank you for good comments!

The next line imports an input/output ability for the program, the standard input/output library for C. It is called stdio.h. Use the include statement followed by the library name (<stdio.h>), WITHOUT a closing semi-colon!

Within the code labeled int main we have the bulk of our processing. Main is a function, and it is code that performs a task. In this case, we are displaying 'Hello World.' All text values that are not variables need to be enclosed in double quotes.

Next, we have the printf function, which accepts a couple of parameters. For now, we are only going to be sending the string Hello World to the function. Make sure to end all instructions with a semi-colon. Many hours have been spent in frustration looking for a missing semicolon!

Finally, C requires a return statement for the function. Since main is the only function in the program, we don't need to go anywhere after processing. By returning 0, we tell the compiler that the program is done processing.

Now, compile and run the application. The following output should display:


Hello world output


Code Application

So far, our program only displays some text. Let's modify it so that the user can enter their name. We will then display their name on the screen instead of 'Hello World.'

Most programs don't just display static text. Instead, they allow for user input and vary the information displayed based on that information. To do this, we will add a little more complexity to our program, and ask the user to enter their name.

First, you will need to declare a variable for the name. Otherwise, the program won't have anything to read. One of the first lines of code should be something like the following, which creates a character variable to store a user's name. In C, a string is an array of characters, thus the double-brackets. The number 25 indicates that the name can hold up to 25 characters (you can adjust as necessary).

char user_name[25];

To do this you will need to use scanf, the input companion to printf. Here are a few tips for completing this task:

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back

Resources created by teachers for teachers

Over 30,000 video lessons & teaching resources‐all in one place.
Video lessons
Quizzes & Worksheets
Classroom Integration
Lesson Plans

I would definitely recommend Study.com to my colleagues. It’s like a teacher waved a magic wand and did the work for me. I feel like it’s a lifeline.

Jennifer B.
Teacher
Jennifer B.
Create an account to start this course today
Used by over 30 million students worldwide
Create an account