<main class="col-md" style="margin-bottom: 387px; margin-top: 0px;"> <h1><a data-id="" href="#hello">Hello</a></h1> <a data-id="" id="getting-started" style="top: 0px;"></a><h2><a data-id="" href="#getting-started">Getting Started</a></h2> <p>Recall that Visual Studio Code (aka VS Code) is a popular “integrated development environment” (IDE) via which you can write code. So that you don’t have to download, install, and configure your own copy of VS Code, we’ll use a cloud-based version instead that has everything you’ll need pre-installed.</p> <p>Log into <a href="https://code.cs50.io/">code.cs50.io</a> using your GitHub account. Once your “codespace” loads, you should see that, by default, VS Code is divided into three regions. Toward the top of VS Code 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 codespace’s files and directories (aka folders), compile code, and run programs. And on the left is your file “explorer,” a graphical user interface (GUI) via which you can also explore your codespace’s files and directories.</p> <p>Start by clicking inside your terminal window, then execute <code class="language-plaintext highlighter-rouge">cd</code> by itself. You should find that its “prompt” resembles the below.</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>$ </code></pre></div></div> <p>Click inside of that terminal window and then type</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>mkdir hello </code></pre></div></div> <p>followed by Enter in order to make a directory called <code class="language-plaintext highlighter-rouge">hello</code> in your codespace. Take care not to overlook the space between <code class="language-plaintext highlighter-rouge">mkdir</code> and <code class="language-plaintext highlighter-rouge">hello</code> or any other character for that matter!</p> <p>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.</p> <p>Now execute</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>cd hello </code></pre></div></div> <p>to move yourself into (i.e., open) that directory. Your prompt should now resemble the below.</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>hello/ $ </code></pre></div></div> <p>If not, retrace your steps and see if you can determine where you went wrong!</p> <p>Shall we have you write your first program? Execute</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>code hello.c </code></pre></div></div> <p>to create a new file called <code class="language-plaintext highlighter-rouge">hello.c</code>, which should open automatically in your codespace’s text editor. As soon as you save the file with command-S (on macOS) or control-S (on Windows), it should also appear in your codespace’s explorer.</p> <p>Proceed to write your first program by typing precisely these lines into <code class="language-plaintext highlighter-rouge">hello.c</code>:</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>#include &lt;stdio.h&gt; int main(void) { printf("hello, world\n"); } </code></pre></div></div> <p>Notice how VS Code adds “syntax highlighting” (i.e., color) as you type, though VS Code’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 VS Code to make certain syntax stand out. Had you not saved the file as <code class="language-plaintext highlighter-rouge">hello.c</code> from the start, VS Code wouldn’t know (per the filename’s extension) that you’re writing C code, in which case those colors would be absent.</p> <a data-id="" id="listing-files" style="top: 0px;"></a><h2><a data-id="" href="#listing-files">Listing Files</a></h2> <p>Next, in your terminal window, immediately to the right of the prompt (<code class="language-plaintext highlighter-rouge">hello/ $</code>), execute</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>ls </code></pre></div></div> <p>You should see just <code class="language-plaintext highlighter-rouge">hello.c</code>? That’s because you’ve just listed the files in your <code class="language-plaintext highlighter-rouge">hello</code> folder. In particular, you executed a command called <code class="language-plaintext highlighter-rouge">ls</code>, which is shorthand for “list.” (It’s such a frequently used command that its authors called it just <code class="language-plaintext highlighter-rouge">ls</code> to save keystrokes.) Make sense?</p> <a data-id="" id="compiling-programs" style="top: 0px;"></a><h2><a data-id="" href="#compiling-programs">Compiling Programs</a></h2> <p>Now, before we can execute the <code class="language-plaintext highlighter-rouge">hello.c</code> program, recall that we must <em>compile</em> it with a <em>compiler</em>, translating it from <em>source code</em> into <em>machine code</em> (i.e., zeroes and ones). Execute the command below to do just that:</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>make hello </code></pre></div></div> <p>And then execute this one again:</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>ls </code></pre></div></div> <p>This time, you should see not only <code class="language-plaintext highlighter-rouge">hello.c</code> but <code class="language-plaintext highlighter-rouge">hello</code> listed as well? You’ve now translated the source code in <code class="language-plaintext highlighter-rouge">hello.c</code> into machine code in <code class="language-plaintext highlighter-rouge">hello</code>.</p> <p>Now execute the program itself by executing the below.</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>./hello </code></pre></div></div> <p>Hello, world, indeed!</p> <a data-id="" id="getting-user-input" style="top: 0px;"></a><h2><a data-id="" href="#getting-user-input">Getting User Input</a></h2> <p>Suffice it to say, no matter how you compile or execute this program, it only ever prints <code class="language-plaintext highlighter-rouge">hello, world</code>. Let’s personalize it a bit, just as we did in class.</p> <p>Modify this program in such a way that it first prompts the user for their name and then prints <code class="language-plaintext highlighter-rouge">hello, so-and-so</code>, where <code class="language-plaintext highlighter-rouge">so-and-so</code> is their actual name.</p> <p>As before, be sure to compile your program with:</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>make hello </code></pre></div></div> <p>And be sure to execute your program, testing it a few times with different inputs, with:</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>./hello </code></pre></div></div> <a data-id="" id="walkthrough" style="top: 0px;"></a><h3><a data-id="" href="#walkthrough">Walkthrough</a></h3> <p>Here’s a “walkthrough” (i.e., tour) of this problem, if you’d like a verbal overview of what to do too!</p> <div class="ratio ratio-16x9" data-video=""><iframe allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen="" class="border" data-video="" src="https://www.youtube.com/embed/wSk1KSDUEYA?modestbranding=0&amp;rel=0&amp;showinfo=0" scrolling="no" id="iFrameResizer0" style="overflow: hidden;"></iframe></div> <a data-id="" id="hints" style="top: 0px;"></a><h3><a data-id="" href="#hints">Hints</a></h3> <a data-id="" id="dont-recall-how-to-prompt-the-user-for-their-name" style="top: 0px;"></a><h4><a data-id="" href="#dont-recall-how-to-prompt-the-user-for-their-name">Don’t recall how to prompt the user for their name?</a></h4> <p>Recall that you can use <code class="language-plaintext highlighter-rouge">get_string</code> as follows, storing its <em>return value</em> in a variable called <code class="language-plaintext highlighter-rouge">name</code> of type <code class="language-plaintext highlighter-rouge">string</code>.</p> <div class="language-c highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">string</span> <span class="n">name</span> <span class="o">=</span> <span class="n">get_string</span><span class="p">(</span><span class="s">"What's your name? "</span><span class="p">);</span> </code></pre></div></div> <a data-id="" id="dont-recall-how-to-format-a-string" style="top: 0px;"></a><h4><a data-id="" href="#dont-recall-how-to-format-a-string">Don’t recall how to format a string?</a></h4> <p>Don’t recall how to join (i.e., concatenate) the user’s name with a greeting? Recall that you can use <code class="language-plaintext highlighter-rouge">printf</code> not only to print but to format a string (hence, the <code class="language-plaintext highlighter-rouge">f</code> in <code class="language-plaintext highlighter-rouge">printf</code>), a la the below, wherein <code class="language-plaintext highlighter-rouge">name</code> is a <code class="language-plaintext highlighter-rouge">string</code>.</p> <div class="language-c highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">printf</span><span class="p">(</span><span class="s">"hello, %s</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">name</span><span class="p">);</span> </code></pre></div></div> <a data-id="" id="use-of-undeclared-identifier" style="top: 0px;"></a><h4><a data-id="" href="#use-of-undeclared-identifier">Use of undeclared identifier?</a></h4> <p>Seeing the below, perhaps atop other errors?</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>error: use of undeclared identifier 'string'; did you mean 'stdin'? </code></pre></div></div> <p>Recall that, to use <code class="language-plaintext highlighter-rouge">get_string</code>, you need to include <code class="language-plaintext highlighter-rouge">cs50.h</code> (in which <code class="language-plaintext highlighter-rouge">get_string</code> is <em>declared</em>) atop a file, as with:</p> <div class="language-c highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="cp">#include</span> <span class="cpf">&lt;cs50.h&gt;</span><span class="cp"> </span></code></pre></div></div> <a data-id="" id="how-to-test-your-code" style="top: 0px;"></a><h3><a data-id="" href="#how-to-test-your-code">How to Test Your Code</a></h3> <p>Execute the below to evaluate the correctness of your code using <code class="language-plaintext highlighter-rouge">check50</code>, a command-line program that will output happy faces whenever your code passes CS50’s automated tests and sad faces whenever it doesn’t! But be sure to compile and test it yourself as well!</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>check50 cs50/problems/2022/x/hello </code></pre></div></div> <p>Execute the below to evaluate the style of your code using <code class="language-plaintext highlighter-rouge">style50</code>, a command-line program that will output additions (in green) and deletions (in red) that you should make to your program in order to improve its style. If you have trouble seeing those colors, <code class="language-plaintext highlighter-rouge">style50</code> supports other <a href="https://cs50.readthedocs.io/style50/">modes</a> too!</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>style50 hello.c </code></pre></div></div> <a data-id="" id="how-to-submit" style="top: 0px;"></a><h2><a data-id="" href="#how-to-submit">How to Submit</a></h2> <p>In your terminal, execute the below to submit your work.</p> <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>submit50 cs50/problems/2022/x/hello </code></pre></div></div> </main>