C Programming Tutorial 20 – Intro to Data Types – Part 2

Don’t forget to read the last blog on data types! Are you new here? Start at the beginning, Intro to C!

There are other data types you will come across. One example is an array. An array can be used to store multiple values in one data type. So we could make an array of integers, which would be a collection of integers, where each are called elements of the array.

We can also create a character array. A character is just an individual letter, number or symbol from the keyboard.

The Importance of Strings

Now, an important type of data that every programmer needs to know is that of string. A string is a group of characters surrounded by double quotes. “I like cats” is an example of a string. How do we go about storing something like this in a variable in C? Well we actually use an array of characters. So think of the string as a collection of characters in a sequence.

There is something special about storing strings in that you have to end them with what is called a null character. We’ll get into all of the details later on as this is just an intro, but essentially, we have to make the array one element longer to store a fancy \0 character, which is called the null character, or null terminator.

Finally, another extremely important data type category we have are pointers. Pointers are an incredibly powerful tool in C that I hope to discuss with you guys soon.

What is a Constant?

We also have an important term known as constant. A constant is any value that does not need to be evaluated. So 5 is a constant, it’s not some function call or some value that is retrieved from the user.

Another thing you are going to want to be familiar with is how to write constants. Now, the term constant has many meanings, so let me explain. Think of a constant as something that you can write the value of down. Certain constants are going to be interpreted different ways.

When you declare a variable, you have the privilege of giving it a data type, so there is no confusion, but if you just write down a number, C has to interpret what type of data that is.

For example:

3/2 and 3/2.0 produce two entirely different results. That’s because C treats the data a certain way depending on the data type of the constant value.

If you want to make a character constant, you have to single quote it, like ‘k’, but if you want to make a string constant, you use double quotes, like “pi$$a”. You could also do something like “H”, as this is a string with one character, not just a character…THERE’S A DIFFERENCE!

What’s the difference between ‘3’, “3”, and 3? The first is a char, the second is a string constant, and the third is an int.

With numbers it’s a bit more vague because there are actually a lot of different number data types and you don’t have the difference between single or double quotes, because number constants do not use quotes.  

There are ways to be very specific when you want to tell C a constant is of a certain data type. For example, you can end a number with the letter F to say that it is specifically a float. Otherwise C assumes the constant is a double.  

But those terms may be foreign to you, so that’s what we are going to be discussing in the next blog! Check it out!

Leave a Reply

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