Lab - Hello World in C

Lab Overview

In this lab, we will write our very first C program. It is customary in programming to name your first program hello world so we will do that here as well.

The general steps we will be following to get our first program running are:

Step 1 - Create the Hello World Program

First clear out and existing code inthe Jdoodle editor by selecting all the code and then press the delete key on your computer.

Once the editor has opened, paste in the following code:

#include <stdio.h>

int main() {
   // printf() displays the string inside quotation
   printf("Hello, World!\n");
   return 0;
}

Step 2 - Compile and Run the Program

When the code is in the editor, execute the program by clicking the Execute button underneath the code editor in the Jdoodle Window.

You should see the following output:

Hello,World!

Step 3 - Make a Small Modification

Now that we have our program running, we can make a small change and rerun the program. Make a simple change to the printf statement by adding your name. You can see the following code as an example.

#include <stdio.h>

int main() {
   // printf() displays the string inside quotation
   printf("Hello, World, my name is Eric!\n");
   return 0;
}

Step 4 - Rerun the Program

When the code is in the editor,we will compile and rerun the program just like we did in the earlier step by clicking the Execute button in the Jdoodle window.

You should see the following output:

Hello, World, my name is Eric!

Step 5 - Change the Return Value

You will notice that the last line of the program returns 0. This
is a standard convention that C program follo that a 0 value indicates that the program exited without any issues. Can the return value to -1 and rerun the program. Your code syoud look like this:

#include <stdio.h>

int main() {
   // printf() displays the string inside quotation
   printf("Hello, World, my name is Eric!\n");
   return -1;
}

You should see the following output:

Hello, World, my name is Eric!

Command exited with non-zero status 255

Step 6 - Reset the Return Value

reset the return valus of you program back to 0 and rerun to make sure everything is ok.

Summary

Congratulations you have written your first C program! Although this is a simple example, you will now see the flow of developing C programs which is:

This lab runs on containers, so as soon as you quit your changes will be lost. You can save your C program by copying and pasting from the Jdoodle editor if you would like to save this program to your computer.

File: labc_hello.md
© Copyright 2023 Destin Learning