It’s CS50, Week 1. I feel slightly jipped that last week doesn’t count.
This is were we begin with Language C.
We’re moving into code, no more safe ol’ Scratch to practice my logic. This includes numbers + letters + instructions. Simply put, patterns of 0 + 1 = numbers, letters, and operations.
The foundations: Source code is the general term for what programmers write. But computers don’t actually understand this code. They speak in binary – the technical term for the root binary language that runs the computer is “machine code”.
Source Code → Compiler Program → Machine Code
Intimidating as of now, but hopefully soon to be clear.
Writing good code is graded on correctness, design, and style.
- Correctness: It does what it’s supposed to
- Design: Technically this is subjective, but actually built on principles. In short, better design is faster and maintainable.
- Style: Code aesthetics matter, not to the computer necessarily but for the human reader. Especially if you ever need to make edits or view someone else’s works.
The CS50 course uses the free text editor, Visual Studio Code. It’s designed to write, format, and run code all in one. VSC in the cloud, is divided and navigated in regions:
- Tabs to write code in various files.
- Terminal window for Command Line Interface (CLI)
- File explorer with hierarchical tree
- Activity bar (a “Goo-ey”)
To build a program type “<name of choice>.c”. in the terminal window. Click back into the terminal window to compile the code.
If you type incorrectly, the program will not run. If executing a command results in no errors, you can proceed.
In the file explorer (on the left) there is a file “<name of choice>.c”.which can be read by the compiler. This is where the code is stored. The other file, same name but without the extension, is an executable file that can be run, but cannot be read by the compiler.
Fun fact: The $ is the standard symbol to let you know you can type commands.
Fun fact 2: The program Automatically highlights syntax that it recognizes.
Functions. how it all works. Functionally, speaking to the computer in small steps and explaining what you are telling it.
In C, the function ‘print’ to display text on the screen.’printf’ is print, but formatted.
Some other intro operational jargon:
- Parentheses tell th machine what the function impacts
- A ; in code is equivalent to “.” in english.
- Escape sequence
- Include is similar to adding a library, also known as a header file
- Capitalization matters
- Placeholders…
- Double quotes for string. Single quotes are for chars.
- Code smell – even if it’s correct it doesn’t mean you should keep doing it = keep code tight or it will be hard to fix, edit, etc.
- Two verticals bar is “or”
Statements of code are closed with a ;.Pro tip: A common error in C programming is the omission of a semicolon.
Variables – Variables can store value. You generally use them to utilize information gathered from input in your output.
Conditionals – if / then, ie conditional logic.
Loops – repeat x3, again, again, again…
Some final bits to tie it all together:
Shockingly not all code uses the same dictionary. For this course the library content (or manual pages) is at manual.cs50.io.
Comments, interestingly, are a part of computer programming. You can leave short explanatory remarks to yourself and others to explain what your plan is. To do this, add // into your code, followed by the comment.
- Abstraction is the art of simplifying code to smaller and smaller problems.
- Operators are the mathematical operations supported by the compiler.
- Types refer to the possible data that can be stored within a variable. Each type has specific limits.
Another fun fact: In coding language, when saying “while true” really means forever. Unless somehow it’s no longer true…
This was a tough week. We started simple, “Hello, world” became <hello, world>. Then we got complicated, Mario Bro. style. I’m not looking forward to the problem sets.
Full disclaimer: The CS50 notes for this class are great! I pulled bits that were particularly helpful, but the full goodness can be found. https://cs50.harvard.edu/x/2023/notes/1/