(This is a summary of week 1 from the Harvard CS 50x series. Read about Week 0 and discover my Scratch game here.)
The second lecture of the course and we’re off to a solid start!
This week’s focus is… The Programming Language: C.
I program in JavaScript in my day job, so comparing a high-level language with a lower-level language was fascinating.
Before the course started, the YouTube comments section was flooded with comments like:
“ If you’ve never coded in C before, you’re going to struggle so much in today’s class!”
“This is going to be so difficult…”
I’ve also heard many comments throughout my career as a Software Engineer, that C can be quite complex to comprehend given the nature of the language and how “old” it is. (C was created in 1972, whereas JavaScript was created in 1995 - my birth year!)
So I was definitely a little nervous.
But as the lecture commenced, I was full of excitement and energy. To my surprise, all the concepts discussed in the lecture were relatively easy to follow.
Summary — Week 1
Feel free to scroll down to the Thoughts & Feelings section if you don’t care too much about a summary :)
- Programming Language C
- Functions, Expressions, Booleans
- Writing good code:
1. Correctness
2. Design
3. Style - CD50 IDE (ide.cs50.io)
- Compile code by running “make” then <filename> (without “.c”)
- Say Hello World (Scratch) = printf(“Hello World”)
- Using external libraries (such as cs50.h)
- string answer = get_string(“what is your name?”);
- Equals sign (“=”) is assignment
- In C we always have to declare the type of variable
- Semi-colons are necessary when coding in C (unlike JavaScript)
- %s — format code for interpolation
- printf(“hello, %s”, answer);
- #include for imports of libraries
- Importance of being verbose and naming variables appropriately
- Int main(void) { } → boiler plate code
- Header files (files that end in “.h”) → stdio.h → #include
- Debugging and how to read error messages
- Importance of well-formatted code
- CLI shell commands:
- ls → Read file contents
- rm <filename> → Remove file
- mv <old_filename> <new_filename> → Rename file
- mkdir <filename> → Create new file
- mv <filename> <folder_name/> → Move file into a folder
- cd <folder_name> → Change directory and go into specific file
- mv <filename> .. → Move file to parent folder
- cd .. → Move up to parent folder
- rmdir <folder_name> → Delete a folder
- cp <filename> → Copy file - Types/data types:
- string (%s)
- char (%c)
- int (%i)
- float (%f)
- long (%li)
- boolean
- etc… - Every one of these data types all uses a specific number of bits
- Limits to these data types → (e.g. 32 bits for integers, 64 bits for longs)
- Mathematical operators: + - * /
- Logical bugs with mathematical operators
- Converting ints to floats
- Variables and syntactic sugar
- “+=“ or “++” → Addition assignment
- Conditional Statements
- Single quotes for char data types
- Double quotes for string data types
- “||” for OR operator and “&&” for AND operator (just like JavaScript-ish)
- Infinite and Finite Loops:
- while
- for
- do while - Abstraction and scope
- Prototype + Placing custom functions at the end of a file
- Imprecisions in floats
- Why we’re running out of bits when it comes to time (mind-blowing!)
Thoughts & Feelings
To my surprise, there are a lot of similarities between JavaScript and C! It’s definitely not as scary as I originally thought (though we did only cover the basics of C).
However, I still need to get used to the whole idea of ints, floats, strings, chars, etc and the need to define data types. In JavaScript, this isn’t usually necessary but I now realise I’ve been taking this for granted this entire time! 🥺
Loves:
- Highlighting the prevalence of screwing up
- CS 50 library:
- “help50” to help troubleshoot → “help50 make <filename>”
- “style50” to help improve code formatting → “style50 <filename>”
- “check50” to test code
Dislikes:
None!
Links to Assignments
For this week’s assignment, we were asked to complete Problem Set 1.
As I code on a daily basis, I decided to do the more challenging tasks and I’m glad I did because there were a few new things I learnt about C:
Assignment Learnings
1. You can’t multiply a string by a number in C. So if you’d like to print out a string several times, you have to write a loop 🤯(unlike in JS how we can just write 5 * “hello”)
2. While loops that take 2 arguments need to use OR operators (“||”) instead of an AND operator (“&&”) in order for both arguments to be validated and to escape the loop
3. The whole idea of ints, floats and numbers with a lot of digits still confuse me a little - so the “credit” task from Problem set 1 did take a little bit more time to get my head around.
That’s all for this week. If you found this article helpful/interesting, please do give some claps! 👏🏻