benlund.co.uk

Business and newbie guide to programming


Data Types and an example in casino games

Programming a roulette gameIn a previous article I described what a variable is and why it is so important in programming. In this article I will go through the Basic Data Types, which is also very important to understand.

Why Data Types are important

Data Types define what type a data that will be stored in a variable and how much memory that will be allocated to store the data. You can say that you prepare the computer for how the variable will be used and what data it will contain.

Defining the Data Type

In the programming language C the data type has to be defined before you can use the variable. But in many other languages the data type can be defined after the variable is declared. And in some languages you do not need to define the Data Type at all.

Some more modern programming languages like Python or PHP are more forgiving. In a compiling language like C you need to be very careful when you use a variable, what data you can store and how. But in PHP you can one second store a integer and in next moment a string.

One simple examples is how strings in C are way more complicated than in PHP. For example if you want to store a word in a variable in C you have to use char, which stores each characters in the word in a array. In PHP you can store the word in a single string variable, with a arbitrary length, and not as a array.

Basic Data Types in C

There are many types of Data Types available to use in most programming languages. In C the Basic Data Types are int, float, double and char. Int are short for integers, which says it all. Float, floating point numbers, are used for numbers which has decimals. Double stores float numbers but allocates more memory and bigger numbers can be stored. Char is used for storing characters, as previous mentioned.

For example if you are programming a roulette casino game you do not need to set a variable as a integer if when you are going to check if the ball ends on a black or red number, not as a float. In that way you will end up with a better code that do not use more memory than is needed.