Week 2 main topic was all about Compilation, Debugging, Array, Functions more on strings, and commnad-line argument.
Cs50 Week 2 Summary
David J.Malan talk about the various steps involve in Compiling our program, he talks about
- Preprocessing – which involves looking at the header file in the program starting with ‘#’ to include its content in our program, so that the program can be successfully executed
- Compiling – this involves converting source code to assembly code that CPU can directly understand,
- Assembling – this involves converting Assemble code to binary instruction( Machine code) by assembling it.
- Linking– this is is the last step, it involves linking previously library like cs50.c to the binary of our program.
Debugging: it involves finding and fixing unintentional mistakes (bug) in our program, we have the….
- help50: which is included in the cs50 file, that helps us find some specific error in our program, we can run it like this: help50 make program-name.
- Printf: We can use print() function to view what is happening in our program by printing it out to detect mistakes or errors.
- Debug50: This is a cs50 ide specific based tool, it is used to step through a program by executing it in real-time. You need to apply a breakpoint in order for you to use it.
- Check50: This help upload and test our program on CS50’s servers, it helps check our program for correctness.
- Style50: This is a program that helps check our program for bad design (like whitespace, missing indentation) and give us a suggestion on how to make it look good.
for better better grouping and smooth running of our program, Doug LLOYD talk more Funtions, Array, Command-line argument, Variable and scope.
Functions is a block of statement, which is used to perform a specific task.
- Function helps for organization, simplicity and reusability of our program
- The first step in writing function is by Declaring it, which contain return-type, name and argument-list
- Then Defining it, this involves telling how the function should work, that is, a predictable behaviour when the function is called with inputts
- The last step is to Call it, we need to pass it appropriate arguments and assign its return value to something of the correct type.
- If we aren’t writing a functions ourselves, we don’t need to know the underlying implementation, for example printf(); functions.
Array: is used to hold values of the same type at contiguous memory location. They are used to organize data of set of values.
- Array first element of array is located at index 0 and while the last index is (n – 1)
- To declare an array, you need to tell The type, which kind of variable each element in the array will be. The name, what name you want to call your array and The Size, how many element will you like your array to contain.
Command-line argument: These are argument passed to the main() function. They are parameters/ argument supply to program when it is invoked.
To see it in action, we need to modify int main(void) to int main (int argc, string argv); Where argc means Argument count and Argv means Argument Vector
Variable and Scope
- Scope is a characteristics of a variable that defines where it can be accessed or used in a program.
- We have the Local Variable and Global Variable.
- Local Variable are variable that can only be accessed within functions in which they are created while Global Variables can be accessed by any function in the program. They are usually created above main();
While Cs50 Week 2 was somehow difficult to understand at first, I had to re-watch Lecture’s video multiple times before I understand it.
Thanks for reading…..