1 00:00:00,000 --> 00:00:10,180 >> [MUSIC PLAYING] 2 00:00:10,180 --> 00:00:11,100 >> ZAMYLA CHAN: Hello, world. 3 00:00:11,100 --> 00:00:13,670 Let's make our first C program. 4 00:00:13,670 --> 00:00:17,720 >> To write our programs, we'll be working in the CS50 Appliance. 5 00:00:17,720 --> 00:00:21,565 It's a virtual machine that runs a computer inside your computer. 6 00:00:21,565 --> 00:00:23,240 >> This has a lot of benefits. 7 00:00:23,240 --> 00:00:28,140 For one, it's already set up with a lot of tools and you'll need for CS50. 8 00:00:28,140 --> 00:00:32,560 It also means that whether you have Mac, PC, Linux, everyone's using the 9 00:00:32,560 --> 00:00:33,950 same thing. 10 00:00:33,950 --> 00:00:37,170 If you haven't downloaded the CS50 Appliance yet, go to 11 00:00:37,170 --> 00:00:41,940 manual.cs50.net/appliance. 12 00:00:41,940 --> 00:00:45,280 >> I have my Appliance here full screen, but you can also have it as a floating 13 00:00:45,280 --> 00:00:47,800 window, if you wish. 14 00:00:47,800 --> 00:00:51,190 I highly encourage setting up a dropbox account for all of your CS50 15 00:00:51,190 --> 00:00:52,170 course work. 16 00:00:52,170 --> 00:00:56,400 That way, in case something happens with your computer or the Appliance, 17 00:00:56,400 --> 00:00:58,440 you have a backup in the cloud. 18 00:00:58,440 --> 00:01:00,600 >> Let's open our terminal first. 19 00:01:00,600 --> 00:01:03,860 The terminal is an interface that we can use to navigate around our 20 00:01:03,860 --> 00:01:06,825 computer, write, compile, and run code. 21 00:01:06,825 --> 00:01:10,010 We'll actually write most of our code in gedit, though. 22 00:01:10,010 --> 00:01:11,820 >> So let's bring this up now. 23 00:01:11,820 --> 00:01:14,940 It'll bring us to an unsaved document. 24 00:01:14,940 --> 00:01:17,330 Notice that I start typing my C code-- 25 00:01:17,330 --> 00:01:19,500 there isn't any syntax highlighting. 26 00:01:19,500 --> 00:01:23,670 Syntax highlighting helps us visually by coloring data types, functions, and 27 00:01:23,670 --> 00:01:25,080 variables differently. 28 00:01:25,080 --> 00:01:30,620 >> So let's save this program into a folder, and call it hello.c. 29 00:01:30,620 --> 00:01:33,960 This way, we'll have syntax highlighting as we type. 30 00:01:33,960 --> 00:01:42,660 31 00:01:42,660 --> 00:01:45,120 We'll start by including two libraries-- 32 00:01:45,120 --> 00:01:52,290 include cs50.h and include stdio.h. 33 00:01:52,290 --> 00:01:56,730 This way, our program will have access to the functions inside the CS50 34 00:01:56,730 --> 00:01:58,840 Standard I/O library. 35 00:01:58,840 --> 00:02:02,070 >> Each program needs a main function, so let's write that here-- 36 00:02:02,070 --> 00:02:06,290 main(void) with two curly braces. 37 00:02:06,290 --> 00:02:09,139 And now here is where you'll write your program. 38 00:02:09,139 --> 00:02:15,395 >> For our first program, we'll just have one line printf, hello, world! 39 00:02:15,395 --> 00:02:18,070 40 00:02:18,070 --> 00:02:22,090 With a new line right here for aesthetics. 41 00:02:22,090 --> 00:02:24,930 >> Once I save, I want to run my program. 42 00:02:24,930 --> 00:02:27,200 So I'm going to open my terminal now. 43 00:02:27,200 --> 00:02:32,260 I start go out in my home directory, but to access hello.c, I need to be in 44 00:02:32,260 --> 00:02:34,720 the same directory or folder. 45 00:02:34,720 --> 00:02:38,800 So at this prompt right here, I'm going to type cd-- 46 00:02:38,800 --> 00:02:40,700 change directory-- 47 00:02:40,700 --> 00:02:45,960 Dropbox/2013/walkthroughs. 48 00:02:45,960 --> 00:02:48,220 And now I'm in my walkthroughs folder. 49 00:02:48,220 --> 00:02:49,820 >> Say I forget what my folders are called. 50 00:02:49,820 --> 00:02:54,130 Then I'll type ls, which will list all of the files in 51 00:02:54,130 --> 00:02:55,860 this current directory. 52 00:02:55,860 --> 00:03:03,480 So changing directories a few times more, I find hello.c. 53 00:03:03,480 --> 00:03:07,040 And this process is analogous to navigating through Finder on Macs or 54 00:03:07,040 --> 00:03:08,540 My Computer in Windows. 55 00:03:08,540 --> 00:03:12,100 In fact, I can even make folders or directories right from the terminal 56 00:03:12,100 --> 00:03:16,240 with the Make Directory command, mkdir. 57 00:03:16,240 --> 00:03:20,400 >> If you want to remove a file, you can remove it from the terminal with rn, 58 00:03:20,400 --> 00:03:24,390 followed by the name of the file you want to remove, and typing y for yes 59 00:03:24,390 --> 00:03:28,420 when the terminal asks you to confirm the deletion. 60 00:03:28,420 --> 00:03:29,970 >> Be sure to explore terminal. 61 00:03:29,970 --> 00:03:32,800 There are tons of keyboard shortcuts to help you out. 62 00:03:32,800 --> 00:03:37,060 Also, gedit actually has a small terminal at the bottom here where you 63 00:03:37,060 --> 00:03:40,746 can do the same things we did in our bigger terminal. 64 00:03:40,746 --> 00:03:44,290 >> For now, let's compile our code with the following command-- 65 00:03:44,290 --> 00:03:46,430 make hello. 66 00:03:46,430 --> 00:03:49,050 Now you should see a line with a bunch of commands. 67 00:03:49,050 --> 00:03:52,510 Make is essentially a wrapper that just sends a list of commands to the 68 00:03:52,510 --> 00:03:54,860 terminal when you type in that one word. 69 00:03:54,860 --> 00:03:59,320 In this case, it's running clang, a compiler with a set of instructions-- 70 00:03:59,320 --> 00:04:00,030 flags-- 71 00:04:00,030 --> 00:04:03,310 on how to compile the program. 72 00:04:03,310 --> 00:04:08,460 >> Now that we've compiled hello.c, if we ls, we should see an executable file 73 00:04:08,460 --> 00:04:11,720 called hello in our directory. 74 00:04:11,720 --> 00:04:18,430 We run it by typing ./hello, and there's our program output. 75 00:04:18,430 --> 00:04:19,380 >> Hello, world. 76 00:04:19,380 --> 00:04:21,670 My name is Zamyla, and this is CS50. 77 00:04:21,670 --> 00:04:28,714