Type safety governs the usage of the compiler checking if the variable is of a right type. C is very loose on data type safety, for example, this is actually in the ANSI C standards, that states that type promotion will occur for data type char
, an example in this assignment will explain this,
char ch = 32; /* that is a space character accordingly to ASCII */
int n = ch + 3;
Notice how the ch
variable gets 'promoted' to type int
. That is legitimate but warrants closer inspection if that is what you are implying.
Compilers such as the C# compiler will not allow this to happen, that is the very reason why in C, there is a usage of cast's operator for example:
int n = (int)3.1415926535f;
Nit picky aside, that is a pi value, what happens, is that the value of n
will be 3.
The above serves to illustrate the type safety and that C is very loose on this regard.
Type safety in modern languages is more strict, such as Java, C#, in order to constrain the usage and meaning of the variables. PHP is an excellent example of loose typing, where you could do this:
$myvar = 34;
$myvar = $myvar + "foo";
is $myvar
an integer, or is it a floating point or is it a string. The type safety here is not very clear on what is the intention which can lead to bugs and a happy debugging session trying to figure out what is happening.
Hope this helps