Harvard CS50 Week 1: My First Hello World Program in C

Brooklin Myers
3 min readJan 5, 2020

--

After about 2 years of Industry experience, I decided it was time to seek out a more well-rounded education through the Harvard CS50 Online course and other resources. I’m a self-taught developer and a CodeCore Bootcamp graduate. While that was a great path for me that led to many amazing opportunities It also means that I’ve also focused on a narrower skill set that I hope to expand. Admittedly I was partially motivated by imposter syndrome. As a Bootcamp graduate, it’s easy to feel like you don’t measure up to those who spent 4+ years developing their expertise through university education.

This is my very first post, so it seemed appropriate for it to be about Hello World.

Hello World in C

I was really happy to see that the CS50 course starts us off with writing hello world. Even after a couple of years of professional experience I still start learning most new languages by creating a simple hello world to learn the basic syntax, so I appreciate the familiarity.

At this point, I had a few questions about C syntax in general.

Why is `main` convention?

Let me admit I’m not an expert on how C works under the hood.

As I currently understand it, it’s the convention to have a single main function in your file in order for the OS to know where it should hand over control to.

Out of curiosity, I tried naming the main function something else and it resulted in an error implicit entry/start for main executable.

Why use `int` for the return type?

Interestingly if you substitute void for int in the function, you will get a warning change return type to ‘int’ but it will still compile and run.

I don’t have a great answer for why you should use int yet, but the best explanation I found is that the return value of a program is used under the hood as the exit code for the process. For now, I’m going to accept this as a dogmatic standard and hope that I’ll learn something later that will clear this up for me.

If you have any useful information on this please consider commenting — it would really help me out!

Why specify `void as the parameter for the `main` function?

If you call main without void, the program will still compile and run.

So here’s why you should still pass void as a parameter, even though the above does compile and work:

int main() means that main is a function that takes an unspecified number of parameters. So using int main(void) communicates better that the function isn’t supposed to take any parameters. If you call main with any parameters your program will error, letting you know that you are using it incorrectly.

I’m used to Javascript where this doesn’t matter, and I think C++ works the same way, so I’m not fully convinced this is a convention that “matters”. However, I’m not a fan of breaking convention unless you have a good reason so, for now, this explanation is sufficient for me.

Conclusion

This is only the first post, so I don’t want to over-promise here — but so far I’m really enjoying the CS50 course and I’m looking forward to continuing it.

We’ve just entered 2020. I’m not one for New Years Resolutions, but this year I’m looking forward to dedicating more energy to expanding my skill set and hope to continue to write articles like this to share what I learn and document the process. If you have any advice on how I can improve as either a writer or a developer please feel free to comment.

So for the very first time let me just say:

Hello World!

--

--

Brooklin Myers
Brooklin Myers

Written by Brooklin Myers

Software Engineer. I create educational content focused on technology for mobile and web applications.

Responses (1)