My journey to the C 🌊
I have recently started CS50 with Harvard University, in order to get my hands stuck in with other programming languages like C and Python. Also, to go over programming fundamentals like arrays, algorithms and data structures 📈.
We begin the course by learning about the core level of programming and computers…binary, otherwise known as machine code!
The first project to complete is a Scratch project. This is not so much direct coding but it definitely gives a great structure on the thought process needed when implementing functions and achieving a result through code.
The first programme we learn about is C and in this blog, I’ll be talking about what I have learnt so far and the similarities and differences I see with JavaScript. ⚖️
Data types
We will start off with data types. Like every other programming language, C has many data types, such as: char, integer, long integer, float, double etc.
Here is a full list of the different data types and more precisely, the memory they take up.
Knowledge of memory size is important to determine what types of data we want to use and why. For example, if we use int
(referring to an integer) then we only have a range of about 4 billion, spanning from about -2 billion to 2 billion, but anything out of these figures then we must resort to other data types.
Variables
When declaring variables in C, we must state which data type it is, for example:
// Declaring and assigning an integer by using 'int'
int x = 25;// Declaring and assigning a char but 'char'
char letter = 'a';
JavaScript also needs keywords when declaring a variable however, JS does not need to explicitly state the data type but uses certain keywords depending on the declaration and mutability of the variable in its scope.
Firstly, looking at var
:
// 'var' can be re-declared and updated within the its scope
var x = 365;
x = 476;
Secondly, let
:
// 'let' cannot be re-declared but can be updated within its scope
var x = 365;
x = 476; // error: Identifier 'x' has already been declared.
Finally, const
:
// 'const' cannot be re-declared nor updated within its scope
const x = 365;
x = 476; // error: Assignment to constant variable.const x = 365;
const x = 476; // error: Identifier 'x' has already been declared.
Loop
Looping ➰ (for) syntax is very similar to JavaScript, minus a few minor changes:
// JavaScript:
for (let i = 0; i < n; i++) {
// code to iterate.
}// C:
for (int i = 0; i < n; i++)
{
// code to iterate.
}
The difference we see is the placement of the curly braces { }
(more so for convention) and the keyword for declaring i
.
More differences between C and JavaScript
- C is compiled in advance while JavaScript is interpreted and sometimes compiled at runtime.
- C code must be recompiled when moved to a different processor. JavaScript doesn’t need to be.
- C is well known for embedded computers and high-performance applications such as operating systems. JavaScript was first embedded only in web pages, but it is finding a new role in server applications developed through Node.js.
- C offers explicit control of threads, while JavaScript encourages users to juggle multiple jobs by splitting tasks into asynchronous functions that are called when data is ready.
Source: https://techbeacon.com/
My experience with the ‘C’ has been a somewhat smooth sail as the syntax is similar to JavaScript. Also, David Malan and Brian Yu are an amazing duo who explain all aspects of CS50 clearly and in exciting ways. I hope your journey on the ‘C’ is smooth too!
‘C’ what I did there? 😉