CS50x Introduction to Computer Science - Lecture 2 (2024) PDF

Summary

This document is lecture notes from the 2024 CS50x Introduction to Computer Science course at Harvard University. It teaches about compiling, debugging, arrays, strings, command-line arguments, and exit status. These lecture notes present concepts related to fundamental programming in C.

Full Transcript

This is CS50 CS50’s Introduction to Computer Science OpenCourseWare Donate  (https://cs50.harvard.edu/donate) David J. Malan (https://cs.harvard.edu/malan/) [email protected]  (https://www.facebook.com/dmalan)  (https://github.com/dmalan)  (https://www.instagram.com/davidjmalan/)  (https://w...

This is CS50 CS50’s Introduction to Computer Science OpenCourseWare Donate  (https://cs50.harvard.edu/donate) David J. Malan (https://cs.harvard.edu/malan/) [email protected]  (https://www.facebook.com/dmalan)  (https://github.com/dmalan)  (https://www.instagram.com/davidjmalan/)  (https://www.linkedin.com/in/malan/)  (https://www.reddit.com/user/davidjmalan)  (https://www.threads.net/@davidjmalan)  (https://twitter.com/davidjmalan) Lecture 2 Welcome! Compiling Debugging Arrays Strings String Length Command-Line Arguments Exit Status Cryptography Summing Up Welcome!  In our previous session, we learned about C, a text-based programming language.  This week, we are going to take a deeper look at additional building-blocks that will support our goals of learning more about programming from the bottom up.  Fundamentally, in addition to the essentials of programming, this course is about problem-solving. Accordingly, we will also focus further on how to approach computer science problems. Compiling  Encryption is the act of hiding plain text from prying eyes. decrypting, then, is the act of taking an encrypted piece of text and returning it to a human-readable form.  An encrypted piece of text may look like the following:  Recall that last week you learned about a compiler, a specialized computer program that converts source code into machine code that can be understood by a computer.  For example, you might have a computer program that looks like this: #include int main(void) { printf("hello, world\n"); }  A compiler will take the above code and turn it into the following machine code:  VS Code, the programming environment provided to you as a CS50 student, utilizes a compiler called clang or c language.  If you were to type make hello , it runs a command that executes clang to create an output file that you can run as a user.  VS Code has been pre-programmed such that make will run numerous command line arguments along with clang for your convenience as a user.  Consider the following code: #include #include int main(void) { string name = get_string("What's your name? "); printf("hello, %s\n", name); }  You can attempt to enter into the terminal window: clang -o hello hello.c. You will be met by an error that indicates that clang does not know where to find the cs50.h library.  Attempting again to compile this code, run the following command in the terminal window: clang -o hello hello.c -lcs50. This will enable the compiler to access the cs50.h library.  Running in the terminal window./hello , your program will run as intended.  While the above is offered as an illustration, such that you can understand more deeply the process and concept of compiling code, using make in CS50 is perfectly fine and the expectation!  Compiling involves major steps, including the following:  First, preprocessing is where the header files in your code, designated by a # (such as #include ) are effectively copied and pasted into your file. During this step, the code from cs50.h is copied into your program. Similarly, just as your code contains #include , code contained within stdio.h somewhere on your computer is copied to your program. This step can be visualized as follows: string get_string(string prompt); int printf(string format,...); int main(void) { string name = get_string("What's your name? "); printf("hello, %s\n", name); }  Second, compiling is where your program is converted into assembly code. This step can be visualized as follows:  Third, assembling involves the compiler converting your assembly code into machine code. This step can be visualized as follows:  Finally, during the linking step, code from your included libraries are converted also into machine code and combined with your code. The final executable file is then outputted. Debugging  Everyone will make mistakes while coding.  Consider the following image from last week:  Further, consider the following code that has a bug purposely inserted within it: #include int main(void) { for (int i = 0; i

Use Quizgecko on...
Browser
Browser