printf (‘Introduction to CS, Lecture 1.1’);
All the content showcased in this article is derived from the CS50 Harvard University Course taught by David J. Malan.
In this second part, David J. Malan teaches:
- Create new files and transform source code into machine code.
- What is clang?
- How to get answers — adding placeholders.
- Conditions I.
#include <stdio.h> → ‘hey computer, look in a file called stdio.h to access the funtion below:’
int. (main void) → similar to the green flag in scratch that tells the program ‘start!’
{
printf(‘hello, world’); → prinf represents the comand to tell the computer: ‘write something on the screen’. Everything in between the brackets you need to put double quotation and end with semi colon.
}
To take a more hands on approach, David J. Malan recommends to access a cloud based tool called sandbox that uses C, our first programming language. You can access it at: sandbox.CS50.io
We create a new file. To name it, we can name it as ___.c given that we are learning the programming language ‘C’.
We add all our lines of code created above.
To get from source code to machine code so that the computer udnerstands, we need something in between called a compiler. We run programs by double cliking, and this happens thanks to the latter.
In the CS50 sandbox there is a bottom box where we will also type.
Do you know what ‘clang’ is?
It is a program to compile code. We use it to convert source code into machine code.
$ → type something here. It is a prompt waiting for another comand.
$ clang hello.c → this will convert our previous lines of code into machine code and stored it in another file called a.out. However, our program does not work yet… let’s continue.
$ ./a.out → this is telling the ocmputer run the program ‘a.out’ in my current folder. After this, when pressing enter, this will appear:
hello, world$ → the dollar sign appears because the computer wasn’t told to move the cursor. In order to do that, we need to go to our 4th line of code above and add \n which stands for ‘new line’. printf (‘’hello, world\n’’);
Before doing anything else, we need to recompile the code to run the new version of our code. To do this, we type in the bottom box:
$ clang hello.c
$ ./a.out → after clicking enter this will appear.
-o means output, if Iwere to add $ clang -o hello hello.c then this will be another file called hello. In this case, instead of $ ./a.out you’d type $ ./hello
The ls comand displays a list of all files in that comand or directory. After pressing enter, you’ll see a list of all your files with an *, which means that there is machine code there — it is executable and can be run by the computer. If it doesn’t have the *, then it’s source code.
rm → is the comand for remove. We can type $ rm a.out to remove the file and it will ask me if I’m sure about moving forward. Type y for ‘yes’ and click enter. If nothing happens, you’re good!
We can ask, but how does the computer answer?
The closest function to ‘ask’ in C language is get_string
‘String’ is the term for a word, phrase, or sentence as opposed to numbers. Every string must have double quotes.
Another definition could be: A string is a sequence of zero or more characters — alfabetical letters — in double quotes.
get_string(‘what’s your name?\n’’);
answer = → the line of code above to tell the program that this has an answer. However, we need to tell the computer that what it needs to store is a string value. The final code would be:
string answer= get_string(‘what’s your name?\n’);
The equal sign does not represent an equation but rather ‘asign’. It goes from right to left. Move from the right something to the left. In this case, we are telling the computer: ‘ask the user for their name, and put that name into a variable’.
The function to say something on the screen is printf( ) We need oto join the user’s name with their input. Whenever we don’t know the value in advance we put a place holder.
%s → is a place holder for strings. We can also tell the computer the name of the value we can plug in by adding a coma. We just need one palce holder for all our variables we want to plug in and they go from right to left.
printf(‘hello, %s\n, answer’);
After having written the above lines of code — those in bold — , we can go to our bottom box in sandbox and write:
If we were to write $ clang -o string string.c , it means that we want our machine code output (-o) to be in a file called string that comes from by input source code called string.c.
However, there’s no such a thing called ‘string’ in C language. This is something provided by CS50 in a file called cs50. For that reason, our very first line of code above all — literally, the first one — should be: #include <cs50.h> and then #include <stdio.h> — the latter contains all other comands as for example printf.
If we do not provide the computer with a file that has the string value and its meaning, then it will not know what we are talking about! It’s as if someone tells you to do something in a foreign language… no clue, right?
So, to compile our code:
Step 1: go to the source code — text box on top — and type #include <cs50.h>
Step 2: go to the bottom text box and type: $ clang -o string string.c and tell the computer a special instruction to link it with the cs50 file: -lcs50 where -l means ‘link’. This tells the computer to combine my code with cs50 code into one program that I can actually run.
We’ve got this. Now go to the bottom box and write:
$ clang -o string string.c -lcs50
If we were to compile our code in a simpler way, we can tell the computer to make your program: $ make string and press enter.
The magic behind that is that it looks for a file called string.c — the source code — and if it finds it then it will make it machine code. Make is a program that comes with Linux, an operating system.
$ make hello
$ .\hello → to run the code
Now we have already coded the program to say hello, world. We have asked for the person’s name and added it as an answer, and now we are going to set a counter.
We’re now saying: ‘Hey, computer I want to store a number’. A number in C is called ‘int’.
int counter=0; → Hey computer, give me a variable whose type is int, call that variable ‘ counter’ and store that value 0 in that variable.
Remeber! Everything here happens from right to left.
But wait… our counter is at 0, hmm… What if I want it at 1?
In C you’d do something like this: counter = counter + 1; → this comes from right to left. At this point we assume the computer knows what counter means and that we’ve used the int counter=0; has already been used.
However, you can also say: counter +=1; or counter++; → it does the same thing as counter = counter+1;
Do you know what a condition is?
A condition tells you ‘If something is true, then do this’.
In C language, this is as follows:
if (x<y)
{
printf(‘x is less than y\n’) → With these lines of code we’re telling the computer: If you see that x is less than y, then say it! Put on the screen: ‘x is less than y’.
}
else
{
printf(‘x is not less than y\n’)
} → these curly braces mean that they’re ready to encapsulate our code.
ATTENTION: You never end the conditions with a semicolon, just functions or lines involving functions with semicolons.
For the sake of the document’s layout I will not use the curly braces after each line of code but keep in mind that these need to be there.
if (x<y)
printf(‘x is less than y\n’);
else if (x>y)
printf(‘x is greater than y\n’);
else if (x==y) → This double = sign is the equality operator. The one that goes alone is the ‘asign’ operator.
printf(‘x is equal to y\n’);
Now our computer will do x<y , x>y , or x==y. HOWEVER, this can be simplified. Why? Because if something is not bigger nor smaller than something else, then it has to be equal!
so we would change the ‘else if (x==y)’ row to: else
We should aim to simplify the code as much as possible so that we take the least amount of space in the CPU.
You can access the course here: https://online-learning.harvard.edu/course/cs50-introduction-computer-science?delta=0
You can follow me on LinkedIn here: https://www.linkedin.com/in/nur-younis-aa79a9183/