Hello World!

#include <stdio.h>

int main(void) {
    printf("Hello, world!\n");

    return 0;
}

CodeExplanation
#include <stdio.h>Load the I/O library

CodeExplanation
int main(void) { ... }Code entry-point

CodeExplanation
printf("Hello, world!\n");Displays the line “Hello, world!”

CodeExplanation
return 0;Return from the function
(with a value of 0)

Syntax We See

#include <stdio.h>

int main(void) {
    printf("Hello, world!\n");

    return 0;
}
TypeExample
Entry-pointint main { ... }
Strings"Hello, world!\n"
Functionsprintf(...)
Semicolons;

EXERCISE
Write a C program to print out “Hello”, followed by your name.

Wait, how do I write the code?

Read: Setting up your Programming Environment

Write a C program to print out “Hello”, followed by your name.

Step One

Open a code editor

Write a C program to print out “Hello”, followed by your name.

Step Two

Load the C library for input and output.

#include <stdio.h>

Write a C program to print out “Hello”, followed by your name.

Step Three

Create the entry-point code

#include <stdio.h>

int main(void) {
    return 0;
};

Write a C program to print out “Hello”, followed by your name.

Step Four

Write a string that says “Hello”, and your name.

Hint: Use printf()
Don’t forget your semicolons!

#include <stdio.h>

int main(void) {
    printf("Hello, Andrew!");

    return 0;
}

Write a C program to print out “Hello”, followed by your name.

Step Five

Save the source code file.

Name it whatever you like, but end it with .c

e.g. hello.c

Write a C program to print out “Hello”, followed by your name.

Step Six

Compile the file.

In a terminal, navigate to the file and compile it.

e.g. gcc hello.c -o hello

Write a C program to print out “Hello”, followed by your name.

Step Seven

Execute the file!

$> ./hello

Hello, Andrew!
Home