Hello
Getting Started
Instructions for Harvard College students
Though CS50âs lectures this summer use CS50 IDE, you will complete your problem sets this summer using a different platform called GitHub Codespaces. Codespaces is a web-based development environment that allows you to program âin the cloud,â without installing any software locally. Indeed, Codespaces provides you with your very own âworkspaceâ (i.e., storage space) for each problem in which you can save your own files and folders (aka directories).
Logging In
- Head to GitHub and, after signing in, accept this assignment on GitHub Classroom.
- After about a minute, refresh the page and click the link to visit your personal GitHub Classroom assignment page.
- On the assignment page, click the green Code button and choose Open with Codespaces.
- (If you donât see an Open with Codespaces button, email heads@cs50.harvard.edu with your GitHub username!)
- Cilck New codespace and then, if prompted, Create codespace.
- Once your Codespace loads, click the
+
button in the bottom section of your window (next to the word âbashâ). You should then see blue text appear that says/workspaces/hello-USERNAME
(whereUSERNAME
is your GitHub username).
Each problem you work on in CS50 will get its own Codespace. The first time you start working on a problem, youâll create a new Codespace. If youâre continuing work on an existing problem, you can click on the existing Codespace to open it.
Once your Codespace loads, you should see that (by default) itâs divided into three parts. Toward the top of your Codespace is your âtext editorâ, where youâll write all of your programs. Toward the bottom of is a âterminal windowâ, a command-line interface (CLI) that allows you to explore your workspaceâs files and directories, compile code, run programs, and even install new software. And on the left is your âfile browserâ (aka âExplorerâ), which shows you all of the files and folders currently in your workspace.
Shall we have you write your first program? In the upper left corner of the window, click the three horizontal lines to open the menu. From the File menu, click New File. Once the file is created, go back to the File menu again and click Save As. Save the file as hello.c
inside of your /workspaces/hello-USERNAME/
directory (where USERNAME
is your GitHub username, which should be the default).
Proceed to write your first program by typing precisely these lines into the file:
#include <stdio.h>
int main(void)
{
printf("hello, world\n");
}
Notice how Codespaces adds âsyntax highlightingâ (i.e., color) as you type, though Codespacesâ choice of colors might differ from this problem setâs. Those colors arenât actually saved inside of the file itself; theyâre just added by Codespaces to make certain syntax stand out. Had you not saved the file as hello.c
from the start, Codespaces wouldnât know (per the filenameâs extension) that youâre writing C code, in which case those colors would be absent.
Listing Files
Next, in your terminal window, immediately to the right of the prompt (which should resemble /workspaces/hello-USERNAME (main) $
), execute
ls
by typing ls
at the prompt and pressing Enter. You should see two files: README.md
(a file with some instructions for startinga nd submitting this problem) and hello.c
(the file you just created). Thatâs because youâve just listed the files in your hello
folder. In particular, you executed (i.e., ran) a command called ls
, which is shorthand for âlist.â (Itâs such a frequently used command that its authors called it just ls
to save keystrokes.) Make sense?
Compiling Programs
Now, before we can execute the hello.c
program, recall that we must compile it with a compiler, translating it from source code into machine code (i.e., zeroes and ones). Execute the command below to do just that:
make hello
And then execute this one again:
ls
This time, you should see not only hello.c
but hello
listed as well? Youâve now translated the source code in hello.c
into machine code in hello
.
Now execute the program itself by executing the below.
./hello
Hello, world, indeed!
Instructions for non-Harvard College students
CS50 IDE is a web-based âintegrated development environmentâ that allows you to program âin the cloud,â without installing any software locally. Indeed, CS50 IDE provides you with your very own âworkspaceâ (i.e., storage space) in which you can save your own files and folders (aka directories).
Logging In
Head to ide.cs50.io and click âSign in with GitHubâ to access your CS50 IDE. Once your IDE loads, you should see that (by default) itâs divided into three parts. Toward the top of CS50 IDE is your âtext editorâ, where youâll write all of your programs. Toward the bottom of is a âterminal windowâ (light blue, by default), a command-line interface (CLI) that allows you to explore your workspaceâs files and directories, compile code, run programs, and even install new software. And on the left is your âfile browserâ, which shows you all of the files and folders currently in your IDE.
Start by clicking inside your terminal window. You should find that its âpromptâ resembles the below.
~/ $
Click inside of that terminal window and then type
mkdir ~/pset1/
followed by Enter in order to make a directory (i.e., folder) called pset1
in your home directory. Take care not to overlook the space between mkdir
and ~/pset1
or any other character for that matter! Keep in mind that ~
denotes your home directory and ~/pset1
denotes a directory called pset1
within ~
.
Here on out, to execute (i.e., run) a command means to type it into a terminal window and then hit Enter. Commands are âcase-sensitive,â so be sure not to type in uppercase when you mean lowercase or vice versa.
Now execute
cd ~/pset1/
to move yourself into (i.e., open) that directory. Your prompt should now resemble the below.
~/pset1/ $
If not, retrace your steps and see if you can determine where you went wrong.
Now execute
mkdir ~/pset1/hello
to create a new directory called hello
inside of your pset1
directory. Then execute
cd ~/pset1/hello
to move yourself into that directory.
Shall we have you write your first program? From the File menu, click New File, and save it (as via the Save option in the File menu) as hello.c
inside of your ~/pset1/hello
directory. Proceed to write your first program by typing precisely these lines into the file:
#include <stdio.h>
int main(void)
{
printf("hello, world\n");
}
Notice how CS50 IDE adds âsyntax highlightingâ (i.e., color) as you type, though CS50 IDEâs choice of colors might differ from this problem setâs. Those colors arenât actually saved inside of the file itself; theyâre just added by CS50 IDE to make certain syntax stand out. Had you not saved the file as hello.c
from the start, CS50 IDE wouldnât know (per the filenameâs extension) that youâre writing C code, in which case those colors would be absent.
Listing Files
Next, in your terminal window, immediately to the right of the prompt (~/pset1/hello/ $
), execute
ls
You should see just hello.c
? Thatâs because youâve just listed the files in your hello
folder. In particular, you executed (i.e., ran) a command called ls
, which is shorthand for âlist.â (Itâs such a frequently used command that its authors called it just ls
to save keystrokes.) Make sense?
Compiling Programs
Now, before we can execute the hello.c
program, recall that we must compile it with a compiler, translating it from source code into machine code (i.e., zeroes and ones). Execute the command below to do just that:
make hello
And then execute this one again:
ls
This time, you should see not only hello.c
but hello
listed as well? Youâve now translated the source code in hello.c
into machine code in hello
.
Now execute the program itself by executing the below.
./hello
Hello, world, indeed!
Getting User Input
Suffice it to say, no matter how you compile or execute this program, it only ever prints hello, world
. Letâs personalize it a bit, just as we did in class.
Modify this program in such a way that it first prompts the user for their name and then prints hello, so-and-so
, where so-and-so
is their actual name.
As before, be sure to compile your program with:
make hello
And be sure to execute your program, testing it a few times with different inputs, with:
./hello
Walkthrough
Hints
Donât recall how to prompt the user for their name?
Recall that you can use get_string
as follows, storing its return value in a variable called name
of type string
.
string name = get_string("What is your name?\n");
Donât recall how to format a string?
Donât recall how to join (i.e., concatenate) the userâs name with a greeting? Recall that you can use printf
not only to print but to format a string (hence, the f
in printf
), a la the below, wherein name
is a string
.
printf("hello, %s\n", name);
Use of undeclared identifier?
Seeing the below, perhaps atop other errors?
error: use of undeclared identifier 'string'; did you mean 'stdin'?
Recall that, to use get_string
, you need to include cs50.h
(in which get_string
is declared) atop a file, as with:
#include <cs50.h>
How to Test Your Code
Execute the below to evaluate the correctness of your code using check50
. But be sure to compile and test it yourself as well!
check50 cs50/problems/2021/summer/hello
Execute the below to evaluate the style of your code using style50
.
style50 hello.c
How to Submit
Instructions for Harvard College students
Harvard College students (those with an @college.harvard.edu email address) should submit this problem via GitHub, not via Gradescope.
In your Codespace, execute the below, replacing USERNAME
with your actual GitHub username.
submit50 classroom50/hello USERNAME
Instructions for non-Harvard College students
- Download your
hello.c
file by control-clicking or right-clicking on the file in CS50 IDEâs file browser and choosing Download. - Go to CS50âs Gradescope page.
- Click âProblem Set 1: Helloâ.
- Drag and drop your
hello.c
file to the area that says âDrag & Dropâ. Be sure it has the correct filename! - Click âUploadâ.
You should see a message that says âProblem Set 1: Hello submitted successfully!â You wonât see a score just yet, but if you see the message then weâve received your submission!