Recommended Posts

Thank you all of you guys for expanding my knowledge in computer architect field.Actually,i am undergrad student.Now i think i have get enough knowldge in field of computer architecture.Now ,i have been moving to programming field.I want to ask you below one question and give answer which would be understandable for me

⬇️⬇️⬇️

Why data type is important in programming language??why can't we just run our programme without inclusion of data type

Share on other sites

Simple examples

A very important and commonly used technique in programming is iteration (repeating an action perhaps many times)

You obviously need to keep count of the number of repeats (iterations) in some way (how is not important to your question).

If you only had one type of number - a full real number accurate to the maximum accuracy of the computer - then your operations would be incredibly wasteful of computer capacity.

So we do not count by 1.000000000000000000000000000 + 1.000000000000000000000000000 = 2.000000000000000000000000000

but use integers, ie 1+ 1 = 2 iterations.

There are actually other quite a few numeric data types in use eg for money, date, rounding etc.

We use constants and variables in normal mathematics, the same happens in computing so that expression can be evaluated.

This leads on to strings and character variables to enable databases to be searched for a specific word eg London.

Edited by studiot

Share on other sites
2 hours ago, Netra77 said:

Why data type is important in programming language??why can't we just run our programme without inclusion of data type

There are languages that have little or no concept of types.

For example, in Perl, you can assign the value "123" or the value 123 to a variable and then treat that variable as a string or as a number (variables are prefixed with '$' in Perl): $s="123"; # A string
$i=123; # An integer # Do arithemtic on both print$s + 1, "\n";
print $i + 1, "\n"; # Do string operations on both print substr($s, 1),  "\n";
print substr($i, 1), "\n"; This program will print: 124 124 23 23 So it treats strings and integers identically. Now that looks really convenient. Why worry about types? Well, one reason is security (finding bugs). After all, you can pass a string to a function like sqrt (the square root function). Which is fine if you say$x="4"; sqrt(\$x), that returns 2. But what if the variable has got set to some non-numeric value by a bug in the program? What is the square root of "banana"? (According to Perl, it is zero).

So maybe it would be nice to have the program check that you aren't doing something silly / dangerous like trying to take the square root of a string. That is where type checking comes in.

Some languages are a bit stricter than Perl. So Python doesn't make you say what type your variables are, but it doesn't allow you to pass a string to a function that expects a number.

Some languages are a bit stricter. So C / C++ require you declare every variable with its type. Others are even stricter still.

It may be quicker to write a short program without worrying about types. But by the time you have to write (or, even worse, maintain) a long piece of code, you will be wishing for strong type checking. Which is why Python has added it as an optional extension.

Another benefit of types, is that it allows the compiler to make some optimizations it might not be able to otherwise. For example, if you say that a variable is of type "byte", then the compiler knows it only needs 8 bits of storage. If you define a variable as "float" or "double" then the compiler knows they need 32 and 64 bits, respectively.

Similarly, adding two values that the compiler knows to be integers can just require a simple "add" instruction. If it knows they are floating point variables, then it knows it has to do a "fpadd" instruction. If they are string variables then it knows it can needs to append one sequence of bytes onto the other.

Share on other sites

Remember my 'layer model'  ?

There is a layer heirarchy in programming as well.

Starting at the bottom with the binary that is loaded into the electronic hardware (for electronic engineers)

All the way up to database languages like Access/Visual Basic for semi technical humans.

Such humans would not want to spout a load of binary to ask the question

"How many arch bridges are there with three or more spans, made of steel, carrying a railway and a pipeline across a river in my database ?"

Share on other sites

Good point. Abstraction is another benefit of data types.

For example, C has no real concept of a string type. So to manipulate strings you either have to deal with individual bytes (or larger characters) in memory or your all library functions do do operations on strings.

Other languages have support for strings built in, so you can simply add two strings to concatenate them, and so on.

Some languages have built in support fo complex structures such as lists or dictionaries. Low level languages like C have to use pointers and functions, making the code harder to understand and more prone to bugs.

Share on other sites
On 4/23/2020 at 11:43 AM, Netra77 said:

Why data type is important in programming language??why can't we just run our programme without inclusion of data type

Did you mean declaration of data type of variable or constant by programmer?

Declaration of data type informs compiler or interpreter what is data type, so there is no need to guess it at run-time, and there is no need to use generic data type.

There are existing computer languages which don't require declaration of data type by programmer, but result is that they are much much more slower.

For instance some such computer languages treat the all variables and constants as strings. You can store integer, float, double, boolean and string, just using string. If we add two strings they are concatenated. If you add two integers/floats/doubles which are strings internally, computer must convert string to integer/float/double first, add them together, then convert them back to string. Much much more slower code.

Quote

Why data type is important in programming language?

The main reason: it is optimization. To have fast code.

Create an account

Register a new account