DEV Community

Emilie Ma
Emilie Ma

Posted on • Updated on • Originally published at kewbish.github.io

CS50 Week 1 - Learning C

This post is edited from my original blog post. Find it, and other posts by me on kewbish.github.io/blog.

Introduction

Well, it's been a week since I muddled my way through Scratch and the first week of CS50, and I've just about finished Week 1's problem set. This week, I was introduced to the scary world of C, and the basics of how to use it. As well, we went over how to translate common Scratch recipes to C, and some basic information regarding memory and imprecision, as well as some C examples. I'm a complete beginner to these relatively low-level languages, so this was an incredibly eye-opening week.

This week was where I actually started paying attention to things - while the first couple weeks seem pretty easy, I know they're going to become the most useful for laying a proper foundation for the more difficult weeks ahead. So, I sat through the entire hour and forty-six minutes, and delved into the world of C.

Skip to my thoughts below.

Notes

Below are my notes:

  • So what - we've established that C is extremely verbose compared to my beloved Python, but now what?
    • There are brackets everywhere
    • Don't forget semicolons
    • style50 demands this terrible bracket style where the bracket is on its own line[^2]
  • You initialize variables with a specific type
    • C is statically-typed, unlike Python
    • can't randomly change types midway through the program
    • causes a lot of 'filler' variables to be made, as well as loops to convert between types, like ints to strings
  • C doesn't have list comprehension, use for-loops
  • By the way, their for-loops are like 'normal' for-loops
    • Completely different from Python for-loops
    • less intuitive for looping over objects, but easier for array elements
  • Can't dynamically append to an array
    • need to make a counter of some type to find the length of things
    • alternatively, for strings, use strlen()
    • arrays need to be initialized with a certain size, memory allocation things
  • We input things with get_string(), which is part of the CS50 header library
    • lots of functionality and sanitization in the CS50 library
  • And also, headers are included, not imported
    • #include something
    • if it's in "" quotes, it's a relative import
    • if in <> brackets, it's somewhere in the depths of the C library compiler
  • Scratch did come in handy, liked the translation examples
  • String formatting is extremely weird
    • Need to specify the type, and use % formatting
    • remember what type the variable is before formatting it
  • Imprecision is one of those things that I always forget
    • In cash, remember that float imprecision exists
  • Make is pretty awesome, complete with commands and all those cool things
    • I'm trying to set a custom cs50make command in my bashrc instead, but I can't seem to figure it out quite yet
  • For problem sets - do your own research
    • I get starting blocks, but after, I'm kind of on my own

Problem Sets

In PSET1, you have the option of doing Mario Less or More, and Cash or Credit, based on your experience and comfort with C and programming. I, personally, found the 'more challenging' problems more fun to complete, and found that they taught me more of C's language features, like arrays (even tho we haven't even been introduced to those).

I would recommend doing both the less and more versions of the problem sets. Usually, (like Mario) the 'more' problem sets build off the 'less' sets, so they were more or less required to do anyhow. As well, you get more practise, and I find that practise extremely useful, especially when learning such a new language.

CS50 IDE

For some reason, I just don't like the IDE. Maybe it's just because I'm used to Intellisense and the extension environment that VSCode provides. One example? VSCode allows you to surround things in brackets and quickly use a bunch of keyboard shortcuts to navigate, whereas in the CS50 IDE, doing the same commands just delete and do nothing, respectively.

Setting CS50's CLIs and header files up on WSL were pretty simple, and they're useful for running check50 and other things up on my own system without having to navigate back to the cloud IDE.

So anyway: VSCode, WSL, and Git for the win - CS50 IDE nay. I like how it makes getting started easy for less technically-focused people, and saves a lot of time regarding getting set up, though.

Teaching Style + Misc.

To be honest, I'd kind of skipped over the shorts for Week 0, but for Week 1, they really came in handy. Going through those as well as rereading notes / listening to audio in the background while doing problem sets was how I managed to figure out a bunch of things regarding the problem sets.

They never mention it, but checking out some of the source behind the CS50 libraries and doing a couple Google searches was equally useful. Of course, this probably wouldn't help for people who don't know where to find it, but stalking various GitHub repos was a fun waste of time / help.

And finally, it's time to extol Malan again. Extremely clear, though there's a bit of fluff at times. I love how CS50's staff take their time to make the online CS50x just as wholesome and inclusive as the IRL CS50, even taking the next step to build in a snack chooser in their viewer. (Which, by the way, has a bunch of amazing screenview / normal view, shortcuts, and tonnes of other features. I love it - a great example of a super UX!)

Conclusion

It'll be a fun couple weeks - it takes longer than I thought to properly write up notes, listen to lectures, do problem sets, and write the words you are reading right now. It's a little repetitive, so maybe I'll take a week to speedrun some problem sets, and then write a batch of blog posts at once. Anyway, I'll find a more efficient way to do things, and share my experiences here!

If you've learned C, what are your opinions on it?

Latest comments (15)

Collapse
 
bradwellsb profile image
Bradley Wells

C has its place, and it's super fast for those things it should be used for, like numerical analysis (estimating the solution for a partial differential equation, for example). I wouldn't want to attempt that using a higher-level language!

Collapse
 
kewbish profile image
Emilie Ma

Definitely true!

Collapse
 
paramsiddharth profile image
Param Siddharth • Edited

I did the course a few weeks ago. It was one of the best ones I ever did, and I miss it now that it is finished. :) It was a great learning opportunity, but sadly, I don't have a PC, so I did it all on my Android phone using Termux and Acode.

I just love the idea of C! I perhaps have a long way to learn more of it, but the very realization that beautiful languages like Python and Java were written in C intrigues me. C is very powerful.

P. S. It is known that the creator of Python, Guido van Rossum, didn't add pointers in Python because he found them quite difficult to handle and believed that they made coding difficult for the programmer.

Collapse
 
kewbish profile image
Emilie Ma

That's crazy that you managed to do it all on a phone - props!
I definitely agree about the wonder that Python is written in C, looking further into C has made me appreciate Python a lot more.

Collapse
 
pmpn8ez profile image
pmpn8ez

I blew through this course myself recently, very well done. I agree with you on the IDE was easy and handy for submitting/checking but not as keyboard friendly as visual studio/vscode.
Decent examples, not too boring (unless you do the web path....) made it a great use of time. I took about 2ish weeks to do the whole course including the android/ios and game tracks. Currently working on the follow up game design course

Collapse
 
kewbish profile image
Emilie Ma

I was planning on doing the web path actually - what did you find boring?

Collapse
 
pmpn8ez profile image
pmpn8ez

I found the whole make a "homepage" a little outdated and had little interest in writing a stock market app. I liked the instructor the least as well out of the 3. I watched and followed along with the lessons but did not do the assignments.

Thread Thread
 
kewbish profile image
Emilie Ma

Ah, I'll keep that in mind!

Collapse
 
monicamakes profile image
Monica

This course is so fun (and challenging!), enjoy the ride.

Collapse
 
kewbish profile image
Emilie Ma

Definitely will!

Collapse
 
natyekennedy profile image
Kennedy Tariah

Hello there, thanks for this piece.
How did you get C to run in VScode?

Collapse
 
kewbish profile image
Emilie Ma

There's a C/C++ extension that I installed for linting and some useful snippets. Aside from that, I just set up a bash function with WSL to run clang and ran my binaries from WSL as well.

Collapse
 
pentacular profile image
pentacular

C is all about fixed length arrays accessed by indexes called pointers.

// Variables are effectively stored in arrays of length one.
int i = 1;

// Let's get a pointer to the array element that i is stored in.
int *a = &i;

// Now we can modify i through the pointer into the array it is stored in.
a[0] = 2;

// Just like with an explicit array.
int q[4];
// But the array evaluates to a pointer to its first element.
int *r = q;
// But the elements are modified in the same way.
r[0] = 1;
// Since the array evaluates to a pointer to its first element we can skip using r.
q[0] = 2;
Enter fullscreen mode Exit fullscreen mode

C is reasonably simple once you understand that it is:

  1. All about arrays.
  2. A primitive language designed to make mediocre compilers easy to implement.

Point 2 is why C is so wide-spread -- writing a shoddy C compiler is very easy, so there are shoddy C compilers for pretty much everything.

Writing a good optimizing C compiler is quite difficult, so it took considerable time for C implementations to become competitive with assembly.

Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️ • Edited

Writing a good optimizing C compiler is quite difficult

Still infinitely easier than for something like C++ though, which means there's not only more bad compilers but also better good compilers.

Also, to expand a bit on the array thing: that's actually a huge benefit when comparing to other languages, since arrays (unpartitioned, sequential bytes in memory) play very nice with caching, which has led to arrays performing better than other data structures even where it's counter-intuitive, like deleting elements faster than linked lists.

Collapse
 
kewbish profile image
Emilie Ma

Thank you for the in-depth comment - I'll be sure to keep this in mind in the coming weeks 😄