C Programming Tutorial 37 – Strongly Typed Vs Loosely Typed Languages

Did you read the last blog on Operator Precedence? Are you new here? Start at the beginning, Intro to C!

This blog is going to be an introduction to how programming languages consider data types. As we know, there are many data types in C programming. C is an example of a strongly typed programming language. Strongly typed means that every piece of data has a specific type.

Concatenation

Imagine if we stored everything in strings. This would become a problem because the computer would never know how to work with certain data. For example, let’s say we have “5” and “6” and we try to add them. Should the computer add the numbers to be “11”, or should it combine the strings to be “56” (The process of combining two strings like this is known as concatenation).

Concatenation doesn’t work quite this way in C, but in general, these are the kinds of problems that come up when we do not have things strongly typed. The opposite of a strongly typed language is called a loosely typed language. Now, a loosely typed language is not quite this loose.

A loosely typed language will usually have a few general types such as numbers, characters, or strings, but they are not nearly as strict with the typing.

What is a good example of this?

In C, if we do something like this: 1/3, the result is going to be zero. That’s because the type of data being used are all integers. A loosely typed language is probably not going to be that obnoxious about types.

The benefit of using a strongly typed language is that it teaches you to be very discrete about what you want the computer to do. I think of a loosely typed language like water: it’s flowy and forgiving. I imagine a strongly typed language as very strict.

Using a loosely typed language can be more simple starting out because you don’t have to worry about all of the different data types, but there is less protection as we will see soon.

For example, in a popular programming language known as JavaScript the type system is loosely typed. This means instead of having data types like integer, double, float, signed and unsigned, we just have numbers.

Some people call the way JavaScript works with data types as duck typing…. Which is weird, but basically it says something like “if it looks like a duck, if it talks like a duck, it is a duck.”

Let’s say you write a function (which is just a section of code you can call multiple times) which expects a number.  Whoever uses that function can pass in whatever they want, or pass in nothing at all.

Because JavaScript is not strongly typed, you have to do extra work inside of the function to make sure the function responds appropriately. What happens if somebody throws in an array? What happens if somebody throws in a string? What happens if somebody throws in a potato?

With a strongly typed language, you often have to pay extra attention to data types, but you have an extra layer of protection, too. Different people like different things.

In C, it is not easy to flow between data types. Because of this, we need something called type casting. That is what we will be discussing in the next video.

In the next blog we are going to be discussing all of the Type Casting. Check it out!

Leave a Reply

Your email address will not be published. Required fields are marked *