Programming in C intro

Today I am going to share what i have learned about the basics of computer programming in C . 

It is a text-based language that does not use a graphical user interface like the scratch application I referred to in my previous post. 

It is more technical than scratch but it is the best way to learn the basics of programming and how you can advance from those levels to higher advanced levels in present-day programming languages like Java and python.

I have learnt that in the C programming language, to display items on the screen, you use the following code;

printf(“hello, world”);

As you can see from the example above you have to use  parentheses to hold the text and then you have to finish each code statement with a semicolon to let the computer know that you have finished the line. You also have to use ” ” within the brackets to contain the message that you want to display. 

In C when you want to use a function; that is simply a set of instructions to do something; you have to include the following line in your code;

#Include <stdio.h>

The above line of code is what holds or contains the set of instructions that can enable the computer to understand what you want to do just as in the code below where we want to display the words “hello world” .

You have to include it so that the computer knows what you have typed in and what you want to do with the set of words that you have written on the command line or console.

There is also another piece to this puzzle before you can run the code successfully. It is called a compiler. A compiler is basically a piece of software that converts source code into machine code. Source code is what you type inside the command window while machine code is the bunch of zeros and ones that your computer understands.

The compiler that we are going to use for the C language is called clang. so you have to use the word clang before you run your program code. In this case you will have to type out;

clang hello.c

To compile the piece of code that you have written and then immediately after;

./a.out

This is to inform the computer to run the file you have just written that is stored in the current folder.

It is funny how the simpler things in life are usually the most powerful. I have just finished learning the very basics of coding with the scratch application by the MIT students.

I have then jumped into the C language which uses a command window or console like CS50 sandbox or other applications that are available online but for now let us just use the CS50 sandbox that is freely available for students. 

It is important to note that this simple interface that is just a black window and you cannot really see what is happening in a graphical way, is more powerful than the type of coding languages where you just click and drag something from one place to another.

Simply put, anything that you can use your mouse and a graphical user interface to execute, you can also use a command line interface to perform. 

As a matter of fact,seasoned programmers hardly ever use a mouse and graphical user interface, they just execute what they want to do using the command lines on the black screen.

What you need to know about data types and variables

The C language uses a lot of data types and variables contrary to modern programming languages like PHP and JavaScript.

In the modern languages you do not have to declare a variable in order to use it. you can just use it the same time you mention it contrary to the C coding language.

Since C is such a basic language, you have to define a variable explicitly before you use it, while in the modern coding languages the compiler figures out the type of variable you wanted to use automatically. 

The first data type present in C is an int. The int data type is used for variables that will store integer values from negative numbers up to positive numbers.

Integers take up four bytes of memory which is a total of 32 bits. This is because one byte is equal to 4 bits.

There are some people who sat down and decided that an integer can take a certain number of values which was split into two. Half of it is allocated to negative numbers and the other half to positive numbers. The range is from roughly -2 billion all the way up to positive 2 billion for the int data type.

There is a way you can double up the positive values of an int by using another data type called unsigned int. This is a way of making use of all the positive values and doing away with the negative values. So in a way you have recreated the data type to store values to approximately 4 billion.

There is also another data type called a Char. This is short for character. Characters take up one byte of memory which is equal to 4 bits.

This means that they can fit values from negative 127 all the way up to positive 128.

The positive numbers from 0 to 127 are the ones used to map letters to the keyboard characters you use on your computer. This is thanks to the ASCII code.

Floating point numbers is the next data type. This will become very useful especially when you want accuracy in your calculations to a certain number of decimal places.

Floating point values are also stored within four bytes of memory. The limitation with the floats is that you can only calculate values up to a certain number of decimal places because this data type is only allocated 32 bits of memory.

To help with this is another data type called double. I think this is why they named it the double because it can literally store double the number of bytes that can be stored in a floating-point value. This is equal to 64 bits. This means that when you are performing calculations, you can be as accurate as far as even up to 30 places after the decimal point.

It is important to note that there is another type called void. Void is not really a data type but it is a return type that basically just generates a null value. You have probably come across it written in functions within brackets. when you see it just know that the function basically returns no value.

There are also other data types called bool and string which are common in modern languages but are not there with the basic version of the C programming language. So if you want to use them, some people have already created the code for them and then put it in a library so that when you are coding, you just call them and use them within your overall code.

When you want to use the variable within your code, you have to define a data type for it, and then give the variable a name or value, and then finish the statement with a semicolon.

For example;

Int 6;

Char h;

Or alternatively you can also assign multiple values to one data type for example ;

int 4,5,6;

char b, g, k;

There is a difference between declaration and assignment. Declaration is sort of naming the data type and assignment is associating a value with it. To show you what I mean you can look at the sample below;

int number; (declaration of a variable)

number=37; (assignment of the variable)

char letter; (declaration)

letter = ‘I’; (assignment)

It is also possible to do the above through a process known as initialisation like so;

int number= 37;

char letter = ‘I’;

Operators

Operators are simply the symbols that you will be using while coding. You have already seen an example of this with the = operator.

They assist in helping you too manipulate variables. The common operators that you are conversant with include the (+) addition, subtraction (-), multiplication (*), and division ( /).

So in the case of;

int b = n +7;

The operation on the right side of the equal sign will be performed first and then stored in the integer b.

Another operator that you might have not been conversant with but I’m sure most of you used when you were younger is the modulus (%). This basically just give the remainder after you perform a division.

An illustration of this would be;

6%4 = 2

6 mod 4 is equal to 2. This is because if you divide 6 by 4 the remainder is 2. This is actually a very useful operator because it can help you to write code for generating random numbers or separating odd numbers from even numbers.

Some useful shortcuts can be;

y = y + 5  can be shortened as y+=5

Or 

x = x + 1 can also be represented as x++;

It is also very important to note that there’s a very big difference between the equal sign (=)  in arithmetic and the same operator (=) sign in computer programming.

If you want to represent equality when coding, you use two double equals signs (==).

5= 5; (arithmetic)

5==5; (the way to write the same above statement when coding).

Conditional Statements

These allow you to make different decisions based on several outcomes. Think of it like trying to figure out which fork in the road to take.

The first example of this is the “if” statement;

Written in the form of;

if (place conditional statement here like x<20)
{
Write what should happen here if the statement above is either true or false. If the statement is true, then whatever code to you write here should execute. If it is false, then the program should jump over this area over to the next block of code after the curly braces.
}
else
{
Write the piece of code that should run here if the previous conditions above have not been met.
}

In C, it is possible to write as many if-else chains like that one depending on how many conditions you want to check.

The part that i find interesting about this is that if the first condition is met and the second one is not, the program will not move on to the third “if else” statement.

There are also other types of conditional statements such as the switch case and the use of a ternary operator. Some demonstrations of these can be seen below;

Loops

They allow lines of code to execute over and over again repetitively.

There are 3 major kind of loops in the C programming language;

The first type is an infinite loop;

while (true “this can be replaced by a boolean expression : true or false)
{
The line of code here will always run as long as the condition is true. And true will always be true 😁
}

The While loop is useful when you do not know the number of times you may need to repeat a move, like in a game.

The second type is the” do while” loop.

do
{
}
while (boolean expression) ;

The loop will execute whatever lines of code you have written within the curly braces at least once, before it checks for the boolean expression. If the expression evaluates to true, it will loop over again and again until the boolean expression evaluates to false.

The third type is called the “for” loop. This type is used if you require the number of times your program loops to be defined to a specific number of times.
for (i = 0 ; i <30 ; i++;)
{
The expression here will execute as long as the boolean expression which is the statement in the middle of the semi colon there is still true.
}

The three loops above can be used interchangeably depending on how you want to use them. You can have a look at the video below for a practical approach ;

To get input from the user, you can write;

get_string( “what’s your name?”\n)

This is a way to ask the user to type in their name.

To display the input you have gotten from the user, you have to write it in the following format as you can see from the screenshot below;

The “%s\n” that you can see in that image is simply telling the machine where to place the input the user has typed in.

A string is just a sequence of characters or a phrase that is marked by “”. 

There is a number C command called “make”. When you want the computer to automatically know how to compile a line of code for you without you remembering several other phrases like “clang”, you can just use the word “make” before the name of the file and the computer will compile it for you and run that piece of code that you have written for you automatically in any Unix based environment.

I have placed some screenshots for you below so that you can see the difference between the technical way of compiling in C and the easier way by using the explanation that i have just given.

You can then represent conditional statements in C using the format i have given below. I have shown you a side-by-side comparison of how it would look like in scratch and how it would look like in C. The one on the left is scratch and the one on the right with a black background is how you represent it in C.

Another interesting point is that in programming the = sign is called the assignment operator.

A=3

When i write the statement above, I do not mean that A is equal to 3 in coding terms, rather I mean that  I have assigned the value 3 to A. = Sign when programming is called an assignment operator. 

To represent equal values,you use the == sign. For example if you want to express that A is equal to 3, then you write it as A==3.

They are “for” loops in C. If I want to write the words “hello world” and repeat it 50 times I would express it in the following manner as you can see in the image below;

Apart from the string data type, we also have several others that you can use in coding. As I learn more about them I will be sharing my experience on how I am using them and you can use them too. I have shown some examples below;

There are also other place holders that you can use for various data types. I had used the placeholder for string data type earlier. But there are several others that you can use as you can see in the image below;

I have also learnt today that instead of writing a statement of code; for example if I want to print the word “cough” 3 times; instead of writing;

printf(“cough”) 

printf(“cough”) 

printf(“cough”) 

I can make use of a for loop as shown below;

#include<stdio.h>

int main (void)

{

for (int i=0; i<3;i++)

{

printf(“cough\n”);

}

}

I have also come to learn that there is such a thing as binary addition overflow. Binary overflow refers to the state where if you add numbers or if you double a number by itself and try to store it in a piece of memory on a computer, there’s a limit to how much each data type can store in the computer’s memory.

For example an int data type in C can store up to the number 4 billion. If you try to store extra numbers past that, it will cause a computer error because that is the allocated memory space to store such a data type.

This is what led to the crisis that was found in the year 1999. At that time, computers were limited in memory so they just used two binary places to indicate the date.

So when we were about to enter the new millennium which is the year 2000, there was a crisis and some computers crashed because they had only allocated two digits to indicate time and now because of the binary overflow issue, their required to be a third binary place so that it can indicate the year 2000 and beyond.

So they had to rely on computer programmers to modify the machines and allocate more memory to store those extra numbers.

You can see a detailed explanation of how this works in the video below;