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!

tl;dr

Implement a program that prints out a simple greeting to the user, per the below.

$ ./hello
Hello, world!
  • 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

Recall that CS50 IDE is a web-based "integrated development environment" that allows you to program "in the cloud," without installing any software locally. Underneath the hood is a popular operating system, Ubuntu Linux, that’s been "containerized" with open-source software called Docker, that allows multiple users (like you!) to share the operating system’s "kernel" (its nucleus, so to speak) and files, even while having files of their own. 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).

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

Logging In

Head to cs50.io and log into CS50 IDE, selecting edX from the drop-down menu when prompted.

If you typically sign into edX via Facebook, Google, or Microsoft, you’ll first need to create a password for your edX account as follows:

  1. Sign into your edX account at courses.edx.org/login (via Facebook, Google, or Microsoft).

  2. Visit courses.edx.org/account/settings.

  3. Click Reset Your Password (even though you don’t yet have one) under Password.

  4. Check your email for a message from edX. (If it hasn’t arrived after a few minutes, do check your spam folder.) Click the link in that message to reset (i.e., create) your password.

  5. Log into cs50.io!

Upon logging into CS50 IDE for the first time, you may be prompted (again) for your email address. If so, after providing it, click Private under Hosted workspace, then click Create workspace.

You should then be informed that CS50 IDE (aka Cloud9, the software that underlies CS50 IDE) is "creating your workspace" and "creating your container," which might take a moment. You should eventually see your workspace, which should resemble mine from Week 1. If not, do just email the course’s heads to inquire!

Updating

Toward the bottom of CS50 IDE’s UI 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. You should find that its "prompt" resembles the below.

~/workspace/ $

Click inside of that terminal window and then type

update50

followed by Enter to ensure that your workspace is up-to-date. It might take a few minutes for any updates to complete. (Be sure not to close the tab or CS50 IDE itself until they do!).

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. Inside chapter1, add another new folder named hello.

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 hello 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 hello 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 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 both a chapter1 and a hello directory somewhere inside, since we created them 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). Then type

cd hello

followed by Enter to enter into our directory hello. 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 the tab 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).

Specification

Shall we have you write your first program? Inside of your hello folder, create a new file called hello.c, and then open that file in a tab. (Remember how?) Be sure to name the file just as we have, in all lowercase; files' and folders' names in Linux are "case-sensitive." 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.

Do be sure that you type 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 command- or control-s), but don’t quit. Recall that the red dot atop the tab should then disappear. Click anywhere in the terminal window beneath your code, and be sure that you’re inside of ~/workspace/chapter1/hello. (Remember how? If not, type cd and then Enter, followed by cd workspace/chapter1/hello/ and then Enter.) Your prompt should be:

~/workspace/chapter1/hello/ $

Let’s confirm that hello.c is indeed where it should be. Type

ls

followed by Enter, and you should see hello.c? 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. If 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. Type

./hello

at your prompt, followed by Enter, and you should see the below:

Hello, world!

And if you type

ls

followed by Enter, you should see a new file, hello, alongside hello.c. The first of those files, hello, should have an asterisk after its name that, in this context, means it’s "executable," a program that you can execute (i.e., run).

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 just as 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 command- or control-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/hello/ within your terminal window, as your prompt will confirm or deny.) If still seeing errors, try "prepending" help50 to your command like this:

help50 make hello

That’ll pass the output of make hello to a program called help50, which CS50’s staff wrote. If help50 recognizes your error message, it’ll offer some suggestions (in yellow). Just realize help50 is new this year, so odds are it won’t recognize all error messages just yet!

Once you see no more errors, try "executing" (i.e., running) your program by typing

./hello

at your prompt, followed by Enter! Hopefully you now see whatever you told printf to print?

If not, reach out for help! Incidentally, if you find the terminal window too small for your tastes, know that you can open one in a bigger tab by clicking the circled plus (+) icon to the right of your hello.c tab.

Walkthrough

Usage

Your program should behave per the example below. Assumed that the underlined text is what some user has typed.

$ ./hello
Hello, world!

Testing

check50

check50 cs50/2017/ap/hello

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 just for staff (though you’re welcome to visit it).

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).

Know that check50 won’t actually record your scores in CS50’s gradebook. Rather, it 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), CS50’s staff will use check50 to evaluate your work’s correctness officially.

Staff Solution

~cs50/chapter1/hello

How to Submit

Step 1 of 3

Visit cs50.me/account, and follow the link to connect your edX account to your GitHub account.

Step 2 of 3

Head back to the CS50 IDE and ensure that hello.c is in ~/workspace/chapter1/hello, as with:

cd ~/workspace/chapter1/hello
ls

If hello.c is not in ~/workspace/chapter1/hello, move it into that directory, as via mv (or via CS50 IDE’s lefthand file browser).

Step 3 of 3

  • To submit hello, execute

    cd ~/workspace/chapter1/hello
    submit50 cs50/2017/ap/hello

    If you run into any trouble, email sysadmins@cs50.harvard.edu!

You may resubmit any problem as many times as you’d like before the deadline.

Your submission should be graded for correctness within 2 minutes, at which point your score will appear at cs50.me!

Hints

Be sure to re-type (and not just copy and paste!) the code we’ve provided you above to get this program running. It’ll be good to develop this muscle memory starting now!

This was Hello.