1 00:00:02,240 --> 00:00:07,720 Once we are understood what algorithms are, we will start writing a few simple programs 2 00:00:07,720 --> 00:00:10,170 in the C programming language. 3 00:00:10,170 --> 00:00:16,139 Before we begin, we will give a brief introduction to the process of programming. 4 00:00:16,139 --> 00:00:22,880 When you are programming, you follow typically, what is known as the programming cycle and 5 00:00:22,880 --> 00:00:25,780 this contains three parts. 6 00:00:25,780 --> 00:00:33,020 One is the process where you write the program or edit the program, and after you are done 7 00:00:33,020 --> 00:00:37,550 editing the program, you save it and then you compile your program. 8 00:00:37,550 --> 00:00:40,450 If your compilation succeeds, you are ready to run the program. 9 00:00:40,450 --> 00:00:47,120 If your compilation fails, then you return to the editing step and correct the errors 10 00:00:47,120 --> 00:00:48,530 and compile again. 11 00:00:48,530 --> 00:00:53,310 Once compilation process succeeds, then you can run the program and check whether the 12 00:00:53,310 --> 00:00:54,310 output is correct. 13 00:00:54,310 --> 00:00:58,790 If the output is correct, you are done; if not you go back to the edit process. 14 00:00:58,790 --> 00:01:04,199 So, this is why it is known us the edit, compile, run cycle. 15 00:01:04,199 --> 00:01:08,190 So, you edit the program first, then compile it. 16 00:01:08,190 --> 00:01:14,149 If there are compilation errors you go back and edit it again, otherwise you run the program. 17 00:01:14,149 --> 00:01:19,479 When you run the program, if the logic is correct, then you are done. 18 00:01:19,479 --> 00:01:25,399 If your logic is incorrect, then you go back and make changes to the program, compile it 19 00:01:25,399 --> 00:01:26,780 and run it again. 20 00:01:26,780 --> 00:01:31,869 So, this is the process, that we have to follow in the, when we program. 21 00:01:31,869 --> 00:01:37,130 We look at each of the steps one by one. 22 00:01:37,130 --> 00:01:40,429 In editing, it is typically done in what is known as in editor. 23 00:01:40,429 --> 00:01:48,689 Now, editor is a program that lets you create a text file, make changes to the text file 24 00:01:48,689 --> 00:01:52,240 and update the text file, later save it. 25 00:01:52,240 --> 00:01:58,509 So, in order to create a program, pick up particular editor of your choice. 26 00:01:58,509 --> 00:02:02,709 If you are on Linux, I would recommend a simple editor like G Edit. 27 00:02:02,709 --> 00:02:08,959 If you are on windows, there is free editor called notepad plus plus. 28 00:02:08,959 --> 00:02:14,880 Be careful that this is not the usual notepad that comes along with the system. 29 00:02:14,880 --> 00:02:20,990 Write your code in, in editor of your choice and save it into a file. 30 00:02:20,990 --> 00:02:24,310 Let us call it Sample dot c. 31 00:02:24,310 --> 00:02:29,660 Once your code is saved, you have to compile a program. 32 00:02:29,660 --> 00:02:31,990 Now, why do we have to compile a program? 33 00:02:31,990 --> 00:02:34,660 Why is this step necessary? 34 00:02:34,660 --> 00:02:43,030 The computed does not understand C per say, it cannot execute a C program or the individual 35 00:02:43,030 --> 00:02:50,470 statements in a C, in C language correctly. 36 00:02:50,470 --> 00:02:56,090 For example, let us say that in C you can write g equal to a percentage b. 37 00:02:56,090 --> 00:02:58,730 The percentage operation stands for modulo. 38 00:02:58,730 --> 00:03:05,370 So, this statement says that you take a modulo b and assign it to the variable g. 39 00:03:05,370 --> 00:03:10,340 Now, the microprocessor, the processor in the computer cannot execute this statement 40 00:03:10,340 --> 00:03:15,530 because it does not understand this C programming language. 41 00:03:15,530 --> 00:03:24,840 So, it translates it into an equivalent piece of code consisting of even more basic statements. 42 00:03:24,840 --> 00:03:29,450 For example, a, this is just for the purpose of illustration and it is not important that 43 00:03:29,450 --> 00:03:36,070 you understand exactly what is going on, but in a statement like g equal to a percentage 44 00:03:36,070 --> 00:03:42,940 b, can be translated into bunch of statements saying load data from particular memory location 45 00:03:42,940 --> 00:03:48,430 into particular register, load the second piece of data from another memory location 46 00:03:48,430 --> 00:03:54,130 to the second register, divide the contents of these two registers, store the reminder 47 00:03:54,130 --> 00:04:01,010 in a third register and then finally, take the result and store it into a third memory 48 00:04:01,010 --> 00:04:02,030 location. 49 00:04:02,030 --> 00:04:08,400 So, the simple statement that we wrote, g equal to a percentage b or g equal to a modulo 50 00:04:08,400 --> 00:04:14,600 b, becomes a bunch of basic statements, that the microprocessor can understand and then 51 00:04:14,600 --> 00:04:16,340 it execute these statements. 52 00:04:16,340 --> 00:04:23,590 So, why not program in the microprocessor language or in assembly language? 53 00:04:23,590 --> 00:04:27,940 Writing programs in machine language is very tedious. 54 00:04:27,940 --> 00:04:37,190 One line in a higher programming language like C translates into multiple lines of machine 55 00:04:37,190 --> 00:04:38,580 language. 56 00:04:38,580 --> 00:04:44,449 So, writing machine language code is very long and it is very tedious and is particularly 57 00:04:44,449 --> 00:04:45,490 prone to errors. 58 00:04:45,490 --> 00:04:47,699 Also, they are not portable. 59 00:04:47,699 --> 00:04:53,229 If you write machine code for a particular processor, let us say, you are writing the 60 00:04:53,229 --> 00:04:58,400 code for an Intel processor and you translate it to an AMD machine, it might not work. 61 00:04:58,400 --> 00:05:04,009 Whereas, if you take your C code and compile it in another machine, it will run on the 62 00:05:04,009 --> 00:05:05,009 machine. 63 00:05:05,009 --> 00:05:07,350 So, compilers work as a bridge. 64 00:05:07,350 --> 00:05:13,930 What they do is, take a high level C programming language and translate it into the equivalent 65 00:05:13,930 --> 00:05:15,430 machine code. 66 00:05:15,430 --> 00:05:18,029 So, think of them as a translator. 67 00:05:18,029 --> 00:05:23,520 So, you, the input is a C program and then you give it to a compiler. 68 00:05:23,520 --> 00:05:29,289 The output of the compiler will be the equivalent machine program for whichever machine you 69 00:05:29,289 --> 00:05:30,870 want to run it on. 70 00:05:30,870 --> 00:05:35,860 So, compiler is a translator, which translates from C to machine code. 71 00:05:35,860 --> 00:05:38,169 How do you compile? 72 00:05:38,169 --> 00:05:45,749 We have just seen why we bother with compilation and on UNIX system or Linux systems, you can 73 00:05:45,749 --> 00:05:49,240 compile the program using the gcc compiler. 74 00:05:49,240 --> 00:05:52,620 So, gcc stands for the gnu c compiler. 75 00:05:52,620 --> 00:05:58,860 So, for example, if you have edited and saved your file as a sample dot c, you can just 76 00:05:58,860 --> 00:06:05,419 type on the comment prompt on the terminal gcc sample dot c. 77 00:06:05,419 --> 00:06:13,919 If your code does not have any errors, then the system will silently say, that the compilation 78 00:06:13,919 --> 00:06:16,749 is done and it will show you the prompt. 79 00:06:16,749 --> 00:06:21,889 If there are errors, the system will list the errors and so, you can go back to the 80 00:06:21,889 --> 00:06:27,009 editor, edit you code to correct errors and come back and compile again. 81 00:06:27,009 --> 00:06:32,020 As long as there are compilation errors, there will be no executable file created. 82 00:06:32,020 --> 00:06:38,590 So, the executable file is the code, is the file that you can finally run. 83 00:06:38,590 --> 00:06:44,189 And if there are compilation errors, the compiler will not produce executable code. 84 00:06:44,189 --> 00:06:53,020 So, name your file as whatever you want, let us call it, yourfilename dot c and then gcc 85 00:06:53,020 --> 00:06:54,339 yourfilename dot c. 86 00:06:54,339 --> 00:06:57,159 It will produce the executable file. 87 00:06:57,159 --> 00:07:04,190 If you are on Linux, the executable file that it creates is something called a dot out. 88 00:07:04,190 --> 00:07:08,900 If there are no errors and look at your directory, there will be a new file called a dot out 89 00:07:08,900 --> 00:07:17,139 in your directory and we will explain the directory structures in another session, ok. 90 00:07:17,139 --> 00:07:20,650 Let us look at a very simple C program. 91 00:07:20,650 --> 00:07:24,960 Open your editor depending on which system you are in. 92 00:07:24,960 --> 00:07:27,930 So, let us write a very simple program. 93 00:07:27,930 --> 00:07:30,270 It is, it is very short. 94 00:07:30,270 --> 00:07:36,309 What it has is, are three lines of code and some punctuation. 95 00:07:36,309 --> 00:07:38,740 This is known as the C syntax. 96 00:07:38,740 --> 00:07:41,949 Let us examine this code. 97 00:07:41,949 --> 00:07:50,289 What this code does is, it prints a particular massage, which is, welcome to C. 98 00:07:50,289 --> 00:07:57,909 And it has various components, you type it into an editor as it is, make no punctuation 99 00:07:57,909 --> 00:08:00,110 mistakes, syntax errors. 100 00:08:00,110 --> 00:08:06,979 Now, if you compile the program and you have typed the program correctly, then a new file 101 00:08:06,979 --> 00:08:09,499 called a dot out will be created. 102 00:08:09,499 --> 00:08:16,270 So, if you type, gcc sample dot c and if there are no errors, it will just say nothing. 103 00:08:16,270 --> 00:08:20,809 If there are, if it says something, then there is a compilation error. 104 00:08:20,809 --> 00:08:27,770 Compilation creates an executable a dot out and now you can run the program by typing, 105 00:08:27,770 --> 00:08:33,250 and this is important, dot forward slash a dot out. 106 00:08:33,250 --> 00:08:39,770 So, this syntax is important, what you type is, dot forward slash a dot out and then when 107 00:08:39,770 --> 00:08:47,339 you run the program it will say, welcome dot, Welcome to C, because that is what the program 108 00:08:47,339 --> 00:08:51,040 is supposed to doÉ 109 00:08:51,040 --> 00:08:54,370 Let us look at the program little more carefully. 110 00:08:54,370 --> 00:08:56,459 What are its components? 111 00:08:56,459 --> 00:09:04,970 It had three lines, the first line said hash include stdio dot h. 112 00:09:04,970 --> 00:09:08,210 So, it has multiple components. 113 00:09:08,210 --> 00:09:16,210 One is the first symbol, which is, has the first symbol, which is the hash. 114 00:09:16,210 --> 00:09:17,860 Please do not forget to include that. 115 00:09:17,860 --> 00:09:23,089 And actually, there is no space between the hash and the first i, so there is no space 116 00:09:23,089 --> 00:09:24,089 here. 117 00:09:24,089 --> 00:09:26,660 So, hash include stdio dot h. 118 00:09:26,660 --> 00:09:33,190 This line is supposed to tell C that please include the standard input-output library. 119 00:09:33,190 --> 00:09:39,810 The standard input-output library is what has the print routines, which will print output 120 00:09:39,810 --> 00:09:41,570 messages on to the terminal. 121 00:09:41,570 --> 00:09:48,430 So, if you want to have any input output component of your program, then you should include stdio 122 00:09:48,430 --> 00:09:49,750 dot h. 123 00:09:49,750 --> 00:09:54,529 Include this line routinely in your, in the first line of your C file because in the course 124 00:09:54,529 --> 00:10:01,029 of this class, we will often need scanf and printf statements. 125 00:10:01,029 --> 00:10:05,070 So, we will often need input statement and output statements. 126 00:10:05,070 --> 00:10:07,810 So, include this by default. 127 00:10:07,810 --> 00:10:15,250 Now, if you look at the second line, we will have a function called main. 128 00:10:15,250 --> 00:10:19,740 And again, note the parenthesis here that is also part of the syntax. 129 00:10:19,740 --> 00:10:23,650 So, main is supposed to be a function. 130 00:10:23,650 --> 00:10:30,460 All C programs start by executing the main function and it starts from first statements 131 00:10:30,460 --> 00:10:31,460 of the main function. 132 00:10:31,460 --> 00:10:33,550 Now, what dose the main function have? 133 00:10:33,550 --> 00:10:40,740 It has a single line, which says printf Welcome to C. So, printf is the function called to 134 00:10:40,740 --> 00:10:43,260 output from a C program. 135 00:10:43,260 --> 00:10:49,500 So, to print a particular massage you enclose it in double quotes. 136 00:10:49,500 --> 00:10:53,850 So, whatever is enclosed in the double codes, will be printed. 137 00:10:53,850 --> 00:11:00,899 So, to repeat again, please note the extra punctuation symbols, which tell you, that 138 00:11:00,899 --> 00:11:04,510 these are valid C statements. 139 00:11:04,510 --> 00:11:09,800 So, all the underline statements are, all the underline symbols are important. 140 00:11:09,800 --> 00:11:17,709 So, in the line printf Welcome to C, this is what is known as a statement in C and statements 141 00:11:17,709 --> 00:11:19,350 in C end in a semicolon. 142 00:11:19,350 --> 00:11:25,839 So, this semicolon is also important because it tells you, that this is where the statements 143 00:11:25,839 --> 00:11:32,089 ends, what typical errors do we have when we code in C. 144 00:11:32,089 --> 00:11:37,149 Let us systematically enumerate a few common errors that could happen in even a simple 145 00:11:37,149 --> 00:11:38,520 program like what we have seen. 146 00:11:38,520 --> 00:11:42,050 For example, you could forget to include stdio dot h. 147 00:11:42,050 --> 00:11:48,709 If you do not include the standard input library, then the compiler will give you an error message. 148 00:11:48,709 --> 00:11:54,660 You may forget to include the main function, then also you will get some error message. 149 00:11:54,660 --> 00:12:00,569 You could forget to include the semicolon in the statement, you could forget to include 150 00:12:00,569 --> 00:12:09,790 the braces, the curly braces in main or forget to close the double quote, open or close the 151 00:12:09,790 --> 00:12:12,209 double quote in the printf statement. 152 00:12:12,209 --> 00:12:18,100 So, these are a few errors that you could make even in a simple code like what we have 153 00:12:18,100 --> 00:12:19,100 seen. 154 00:12:19,100 --> 00:12:21,430 We have only three lines, but they could also have errors. 155 00:12:21,430 --> 00:12:27,740 I would advise you to try deliberately making these mistakes in your code, try compiling 156 00:12:27,740 --> 00:12:30,870 them and study the error messages. 157 00:12:30,870 --> 00:12:35,449 Once you are familiar with error massages, this will help you later in your coding, because 158 00:12:35,449 --> 00:12:40,639 when you see the error massages you can guess what errors did you possibly make in your 159 00:12:40,639 --> 00:12:41,639 code. 160 00:12:41,639 --> 00:12:42,700 So, go back to the code and correct it.