Problem: Hello

Don’t be alarmed by the length of this problem’s specification. Most of it is just instructions for getting your programming environment set up and learning how to navigate within it. Once these steps are completed, you’ll be good to go for the rest of the year in CS50 AP!

Objectives

  • Set up your CS50 programming environment.

  • Get comfortable with Linux.

  • Solve your first problem in C.

  • Pages 1 – 7, 9, and 10 of http://www.howstuffworks.com/c.htm.

  • Chapters 1 – 5, 9, and 11 – 17 of Absolute Beginner’s Guide to C.

  • Chapters 1 – 6 of Programming in C.

Academic Honesty

This course’s philosophy on academic honesty is best stated as "be reasonable." The course recognizes that interactions with classmates and others can facilitate mastery of the course’s material. However, there remains a line between enlisting the help of another and submitting the work of another. This policy characterizes both sides of that line.

The essence of all work that you submit to this course must be your own. Collaboration on problems is not permitted (unless explicitly stated otherwise) except to the extent that you may ask classmates and others for help so long as that help does not reduce to another doing your work for you. Generally speaking, when asking for help, you may show your code or writing to others, but you may not view theirs, so long as you and they respect this policy’s other constraints. Collaboration on quizzes and tests is not permitted at all. Collaboration on the final project is permitted to the extent prescribed by its specification.

Below are rules of thumb that (inexhaustively) characterize acts that the course considers reasonable and not reasonable. If in doubt as to whether some act is reasonable, do not commit it until you solicit and receive approval in writing from your instructor. If a violation of this policy is suspected and confirmed, your instructor reserves the right to impose local sanctions on top of any disciplinary outcome that may include an unsatisfactory or failing grade for work submitted or for the course itself.

Reasonable

  • Communicating with classmates about problems in English (or some other spoken language).

  • Discussing the course’s material with others in order to understand it better.

  • Helping a classmate identify a bug in his or her code, such as by viewing, compiling, or running his or her code, even on your own computer.

  • Incorporating snippets of code that you find online or elsewhere into your own code, provided that those snippets are not themselves solutions to assigned problems and that you cite the snippets' origins.

  • Reviewing past years' quizzes, tests, and solutions thereto.

  • Sending or showing code that you’ve written to someone, possibly a classmate, so that he or she might help you identify and fix a bug.

  • Sharing snippets of your own solutions to problems online so that others might help you identify and fix a bug or other issue.

  • Turning to the web or elsewhere for instruction beyond the course’s own, for references, and for solutions to technical difficulties, but not for outright solutions to problems or your own final project.

  • Whiteboarding solutions to problems with others using diagrams or pseudocode but not actual code.

  • Working with (and even paying) a tutor to help you with the course, provided the tutor does not do your work for you.

Not Reasonable

  • Accessing a solution to some problem prior to (re-)submitting your own.

  • Asking a classmate to see his or her solution to a problem before (re-)submitting your own.

  • Decompiling, deobfuscating, or disassembling the staff’s solutions to problems.

  • Failing to cite (as with comments) the origins of code, writing, or techniques that you discover outside of the course’s own lessons and integrate into your own work, even while respecting this policy’s other constraints.

  • Giving or showing to a classmate a solution to a problem when it is he or she, and not you, who is struggling to solve it.

  • Looking at another individual’s work during a quiz or test.

  • Paying or offering to pay an individual for work that you may submit as (part of) your own.

  • Providing or making available solutions to problems to individuals who might take this course in the future.

  • Searching for, soliciting, or viewing a quiz’s questions or answers prior to taking the quiz.

  • Searching for or soliciting outright solutions to problems online or elsewhere.

  • Splitting a problem’s workload with another individual and combining your work (unless explicitly authorized by the problem itself).

  • Submitting (after possibly modifying) the work of another individual beyond allowed snippets.

  • Submitting the same or similar work to this course that you have submitted or will submit to another.

  • Using resources during a quiz beyond those explicitly allowed in the quiz’s instructions.

  • Viewing another’s solution to a problem and basing your own solution on it.

Assessment

Your work on this problem set will be evaluated along four axes primarily.

Scope

To what extent does your code implement the features required by our specification?

Correctness

To what extent is your code consistent with our specifications and free of bugs?

Design

To what extent is your code written well (i.e., clearly, efficiently, elegantly, and/or logically)?

Style

To what extent is your code readable (i.e., commented and indented with variables aptly named)?

To obtain a passing grade in this course, all students must ordinarily submit all assigned problems unless granted an exception in writing by the instructor.

Getting Started

One of the hurdles that people getting started with programming frequently encounter is configuring their computer environment for programming. One approach would be to buy a new computer, install a programming-friendly operating system on it (such as Ubuntu Linux) on it, and use that! But that’s a pretty big ask.

Another approach is to do what we’ve done in the past. Several years back, our staff put together something called the CS50 Appliance, a "virtual machine" (running Ubuntu) that you can run inside of a window on your own computer, whether you run Windows, Mac OS, or even Linux itself. To do so, all you need is a "hypervisor" (otherwise known as a "virtual machine monitor"), software that tricks the Appliance into thinking that it’s running on "bare metal."

But why not skip that altogether? After all, wouldn’t you like to get programming right away, without having to download, install, or configure much of anything at all?

We thought you might! So in 2015, several members of our staff, prepared a successor to the CS50 Appliance that’s "cloud-based" (remember what that means?) largely using open-source software developed by a company called Cloud9.

So let’s get you set up with your own instance of the so-called CS50 IDE, which we recommend you use for the majority of your programming work in the course. In order to do so, you’ll first need an edX account if you don’t already have one. Head to https://www.edx.org/ and sign up for an account (if you are 13 or older) by clicking Register atop the page. Any public username that’s available is fine, but take care to remember it and your choice of password.

After you confirm your account by clicking on the link in the email that edX will send, wait a few minutes and then head over to cs50.io and log in using your edX credentials. You should have a CS50 IDE "workspace" automatically created for you (called ide50) on this first visit, and on every subsequent visit to cs50.io, you should be brought right back to this workspace (if not, just click on the colored avatar in the top-right corner after logging in to be brought to your Cloud9 dashboard[1], from which you’ll be able to then open your workspace).

That wasn’t too bad, right?

Updating

Let’s now make sure that you have the most up-to-date version of CS50 IDE. To do so, click in the terminal window (which probably has a tab that looks something like workspace/) at the bottom of the screen, where by default you should see a prompt which reads:

~/workspace/ $

You are now about to work with a command-line (i.e., text-based) interface via which you can navigate your workspace’s files and directories and run programs by typing their names. Updating the CS50 IDE is pretty easy. Just type

update50

at the prompt and then hit Enter on your keyboard. It may take a while to finish executing, but after a minute or two (depending on the speed of your computer and the quality of your Internet connection), you’ll see the terminal window eventually tells you

Update complete!
BE SURE TO CLOSE AND RE-OPEN ANY TERMINAL WINDOWS <3

after which you are free to continue exploring CS50 IDE.

Okay, let’s create a folder (otherwise known as a directory) in which your code for this problem will soon live. On the left side of the screen you should see a file tree listing the current contents of your CS50 IDE workspace (right now it should simply be a folder entitled ~/workspace/). Right-click in the blank space underneath that folder and select New Folder near the bottom of the context menu that pops up. This should create, as expected, a new folder for you entitled New Folder which you can rename now to chapter1. If you accidentally leave it named New Folder, simply right click on the folder, choose Rename from the context menu, and rename it.

Front and center in the CS50 IDE workspace is a window wherein you’ll be able to write your code, using Ace, a web-based text editor. Right now, assuming you haven’t played around with the tabs, that window is blank.

Let’s create a new file to play around with. Right-click on your newly-created chapter1 folder and choose New File from the context menu. Then, double-click on this new file (which should hopefully be called Untitled and which should be nested beneath chapter1 in the file tree), and a blank window should open up in Ace with the tab for Untitled as the active one.

Go ahead and type hello (or the ever-popular asdf) on line 1 of the document, and then notice how the tab’s name in Ace now contains a red dot, indicating that you’ve made changes since the file was first opened. Select File > Save, and that red dot should turn green and then disappear, indicating all of our changes are saved.

Untitled isn’t exactly a useful name for this file though, is it? Let’s fix that! In the file tree on the left, right-click on Untitled and select Rename from the context menu. Enter hello.txt when able, and then hit Enter on your keyboard. You should see the name of the file has changed both in the file tree on the left and in the name of the tab.

Okay, with hello.txt still open in your workspace, notice that beneath your document is that terminal window. Notice that the window’s prompt is, assuming you haven’t otherwise manipulated it

~/workspace/ $

(where ~/workspace/—i.e., inside a folder called workspace which is itself inside your home directory, the shorthand for which is ~—is the directory you are currently in.) If that’s the case, based on the file tree should be a chapter1 directory somewhere inside, since we created it just a few moments ago. Let’s confirm as much.

Click somewhere inside of that terminal window and type

ls

and then Enter. That’s a lowercase L and a lowercase S, which is shorthand notation for "list." Indeed, you should then see a list of the folders inside of your workspace directory, among which is chapter1! Let’s open that folder! Type

cd chapter1

or even

cd ~/workspace/chapter1/

followed by Enter to change your directory to ~/workspace/chapter1/ (ergo, cd). You should find that your prompt changes to

~/workspace/chapter1/ $

confirming that you are indeed now inside of ~/workspace/chapter1/ (i.e., a directory called chapter1 inside of a directory called workspace inside of your home directory). Now type

ls

followed by Enter. You should see hello.txt! Now, you can’t click or double-click on that file’s name there; it’s just text. But that listing does confirm that hello.txt is where we hoped it would be.

Let’s poke around a bit more. Go ahead and type

cd

and then Enter. It turns out that if you don’t provide cd with a "command-line argument" (i.e., a directory’s name), it whisks you back to the ~/workspace/ directory by default.

And indeed, your prompt should now be:

~/workspace/ $

Phew, home sweet home. Remember that everything we create in the CS50 IDE will ultimately live inside of ~/workspace/.

Make sense? If not, no worries; it soon will! It’s in this terminal window that you’ll soon be compiling your first program! For now, though, close Ace by clicking the small "x" in the tab for hello.txt (if you’ve made additional changes, you’ll be asked if you want to save your file before the tab closes).

Hello, C

First, a hello from Zamyla if you’d like a tour of what’s to come, particularly if less comfortable. Note that in this video, she is using the CS50 Appliance, which is somewhat different from the CS50 IDE, but not a problem.

Shall we have you write your first program? You could go ahead create a new file inside of your chapter1 directory and save the file as hello.c (not hello.txt), just as before (remember how?), but we’ll also take this opportunity to show you another way. In the terminal window, type

cd ~/workspace/chapter1/

and then, after confirming that you are in the right spot by looking at your prompt, type

touch hello.c

after a few moments, you should see hello.c has been created inside of your chapter1 directory. Neat! Double-click on that file in the file tree to edit it, as before, then go ahead and write your first program by typing these lines into the file (though you’re welcome to change the words between quotes to whatever you’d like):

#include <stdio.h>

int main(void)
{
    printf("hello, world\n");
}

Notice how Ace adds "syntax highlighting" (i.e., color) as you type. Those colors aren’t actually saved inside of the file itself; they’re just added by Ace to make certain syntax stand out. Had you not saved the file as hello.c from the start, Ace wouldn’t know (per the filename’s extension) that you’re writing C code, in which case those colors would be absent.

Do be sure that you type in this program just right, else you’re about to experience your first bug! In particular, capitalization matters, so don’t accidentally capitalize words (unless they’re between those two quotes). And don’t overlook that one semicolon. C is quite nitpicky!

When done typing, select File > Save (or hit ctrl-s), but don’t close the tab. Recall that the red dot atop the tab should then disappear. Click anywhere in the terminal window beneath your code. The prompt itself should now be

~/workspace/chapter1/ $

If it’s not, navigate yourself there. (Remember how?) hello.c should exist in this folder, assuming everything’s gone according to plan. Let’s confirm that hello.c is there. Type

ls

at the prompt followed by Enter, and you should see both hello.c and hello.txt? If not, no worries; you probably just missed a small step. Best to restart these past several steps or ask for help!

Assuming you indeed see hello.c, let’s try to compile! Cross your fingers and then type

make hello

at the prompt, followed by Enter. (Well, maybe don’t cross your fingers whilst typing.) To be clear, type only hello here, not hello.c. You’ll likely see a line that looks like the following[2] immediately after hitting Enter

clang -ggdb3 -O0 -std=c99 -Wall -Werror   hello.c  -lcs50 -lm -o hello

If after that all that you see is another, identical prompt, that means it worked! Your source code has been translated to object code (0s and 1s) that you can now execute. Cool! Type

./hello

at your prompt, followed by Enter, and you should see whatever message you wrote between quotes in your code! Indeed, if you type

ls

followed by Enter, you should see a new file, hello, alongside hello.c and hello.txt.

If, though, upon running make, you instead see some error(s), it’s time to debug! (If the terminal window’s too small to see everything, click and drag its top border upward to increase its height.) If you see an error like expected declaration or something no less mysterious, odds are you made a syntax error (i.e., typo) by omitting some character or adding something in the wrong place. Scour your code for any differences vis-à-vis the template above. It’s easy to miss the slightest of things when learning to program, so do compare your code against ours character by character; odds are the mistake(s) will jump out! Anytime you make changes to your own code, just remember to re-save via File > Save (or ctrl-s), then re-click inside of the terminal window, and then re-type

make hello

at your prompt, followed by Enter. (Just be sure that you are inside of ~/workspace/chapter1/ within your terminal window, as your prompt will confirm or deny.) If you see no more errors, try running your program by typing

./hello

at your prompt, followed by Enter! Hopefully you now see precisely the below?

hello, world

If not, reach out for help!

Woo hoo! You’ve begun to program!

CS50 Check

Now let’s see if the program you just wrote is correct! Included in the CS50 IDE is check50, a command-line program with which you can check the correctness of (some of) your programs.

If not already there, navigate your way to ~/workspace/chapter1/ (Remember how?)

If you then execute

ls

you should see, at least, hello.c. Be sure it’s indeed spelled hello.c and not Hello.c, hello.C, or the like, as filenames on CS50 IDE and indeed most Linux operating systems are case-sensitive. If it’s not, know that you can rename a file in the terminal (as opposed to right-clicking in the file tree) by executing

mv source destination

where source is the file’s current name, and destination is the file’s new name. For instance, if you accidentally named your program Hello.c, you could fix it as follows.

mv Hello.c hello.c

Okay, assuming your file’s name is definitely spelled hello.c now, go ahead and execute the below. Note that 1516.unit1.hello is just a unique identifier for this problem’s checks. We’ll always specify exactly the unique identifier you should use to check your programs' correctness.

check50 1617.chapter1.hello hello.c

Assuming your program is correct, you should then see output like

:) hello.c exists
:) hello.c compiles
:) prints "hello, world\n"

where each green smiley means your program passed a check (i.e., test). You may also see a URL at the bottom of check50's output, but that’s something your teacher might use when checking your code (though you’re welcome to visit it nonetheless).

If you instead see yellow or red smileys, it means your code isn’t correct! For instance, suppose you instead see the below.

:( hello.c exists
  \ expected hello.c to exist
:| hello.c compiles
  \ can't check until a frown turns upside down
:| prints "hello, world\n"
  \ can't check until a frown turns upside down

Because check50 doesn’t think hello.c exists, as per the red smiley, odds are you uploaded the wrong file or misnamed your file. The other smileys, meanwhile, are yellow because those checks are dependent on hello.c existing, and so they weren’t even run.

Suppose instead you see the below.

:) hello.c exists
:) hello.c compiles
:( prints "hello, world\n"
  \ expected output, but not "hello, world"

Odds are, in this case, you printed something other than hello, world\n verbatim, per the spec’s expectations. In particular, the above suggests you printed hello, world, without a trailing newline (\n). See why?

check50 lets you check your work’s correctness before you submit your work. Once you actually submit your work (per the directions at this spec’s end), your teacher will use check50 to evaluate your work’s correctness officially.

CS50 Style

In addition to check50, the CS50 IDE comes with style50, a tool with which you can evaluate your code’s style vis-à-vis CS50’s style guide. To run it on, say, hello.c, execute the below:

style50 hello.c

You should see zero or more lines of suggestions. Yellow smileys indicate warnings that you should consider addressing. Red smileys indicate errors that you should definitely address.

Note that style50 is still a work in progress (a "beta" version, so to speak), so best to consult CS50’s style guide for official guidance.

And how about that? You’ve written your first C program and configured your CS50 environment! Not too shabby.

This was Hello.


1. Incidentally, the first time you visit your Cloud9 dashboard in this way, you may be prompted once again for your email address.
2. Later in the course, we’ll pick apart what all this stuff is, but for now try not to worry too much about it.