Why exactly do we need to specify the type of variable we're defining?

Can't we just make a "universal" type? Why do we need to reserve a certain amount of space? Can't we just occupy the space when the information or value to the variable is obtained? Why pre-decide how much is needed?

4/9/2017 8:22:35 PM

Jason Joby

24 Answers

New Answer


The short answer is that this is just how C++ was designed. There's no technical limitation; there are languages out there that are able to do what you're suggesting. Generally you've got two ways to allocate memory-- The stack, and the heap. Stack memory is given to your program by the operating system when it first starts. Because it has a chunk of memory to work with up-front, working with it is very quick (the stack is fast for additional reasons, but that's part of it). This is where the benefit of knowing exactly how much space your program needs comes in. Heap memory, on the other hand, can be requested at run-time. Instead of receiving the memory up-front, the program can request memory as it needs it. It sounds like what you want is a language that does this for everything. Allocating memory at run-time is very slow in comparison to using the stack, however. The OS needs to scan the memory for a contiguous section in which it can store the type you want to work with. C++ is focused on speed, so it chooses to use a stack in addition to the heap. If you wanted, you could define your own stack structure and store that on the heap, and dynamically allocate every variable you want to use. You're going to suffer for it, though. As I mentioned earlier, there are languages where types don't need to be specified (e.g., Ruby, PHP). Some languages use what's called dynamic typing, where types are determined at the run-time and can change over the course of the program. This, of course, will almost always require dynamic memory allocation. Finally, C++11 introduced the "auto" keyword which can infer a type from context. For example: auto x = 5; // x is an int This is a very big topic, but one other thing I'll mention is that you lose the safety of compile-time checks with dynamic typing. There's no way to know if your program is valid until you run it.


Some languages do have a var keyword that selects a type once something is assigned (sometimes it has to be assigned immediately), or that can change its type by being assigned something else, but those come with their own problems. Specifically, how do you know what type it's storing at any given moment? Or how do you know what var turned out to be with its given value (an int of what size, a float or a double, etc.)?


honestly i prefer specifying the type rather than using var or something. its easier for reading and maintaining code


in addition to @Squidy 's answer (because really it can't get any better than that), you can in a way do abstract data types. This requires the use of void* but you still have to change the type when trying to use it or the program won't know what to do with it.


C++ requires that variables have a type. You could use the auto keyword as a workaround.


We specify the variable type to tell the compiler how much chunk of memory space shall it reserve. We do this for memory space management. Imagine declaring a char variable and reserving for it a memory space similar to long double's, which is a total waste of precious memory space. More over, if you imagine the size memory space available in older version of computers, it is way too much smaller than the machines of this age and languages like C++ are from the bible age of computation. On another aspect, specifying the variable type during code writing helps speed up program execution since there is an early, compile time, binding of data type unlike other languages which perform binding at execution time, which slightly delays program execution time.


Some languages do let you specify a variable that can hold anything (VBA has a variant type for instance) and although they can be very useful they usually also take up more memory and are slower. One of the problems is that as well as holding a value it also has to know what type of value it's holding so that it doesn't do anything that shouldn't be done such as trying to divide 17 by a filename for example. Also when you declare a variable as a specific type it means that you can't accidentally assign an incorrect value to it. If x is an integer it will tell you that something is wrong if you accidentally try to assign x = "a12" whereas having a universal variable wouldn't give an error and it could be very hard spotting a bug like this in a large project.


c++ and some other block-designed languages (like c and java) are like this, they are compiled, so they need to know how much space each function or object needs before compilation, while other languages you may have worked with are scripting languages, like python, javascript or ruby, they dont compile, they are interpreted line by line and every variable is considered a string. Ans thats why a concept like bignum is so easy in python but a headache in c++.


i agree with you! theres some surious profiling going on in coding! (being silly)


it's a way to manage memory, you specify sizes,to allocate just what you need and also this is why we have dynamic allocation !


use python


Lua, Java Script, and Swift don't need to be defined whether it's an integer or a string or a char, etc. there might be some more languages as well


When we compile a code, the compiler will allocate memory to variables and implement built in functions on them. The problem here is that information exists in different types like integer data, string. data, decimal number data and different set of methods exist for each type. Specifying the data type of a variable makes it easy for the compiler to ensure that only valid operations and methods are applied on the corresponding data. or to understand it more practically, it allows to spot onvalid data operation errors in compile time instead of crashing the entire program in runtime. example: string data has different set of methods like concatenate, substr etc. Note: float data type has an entirely different hardware implementation inside alu.


In C++ variables are declared and can also be assigned at the same time that is how Syntex is built and also we assigned so that any time we want to change the memory space we just change the variable


Consider a scenario, you are creating variable "a=20" without defining its type to "int". Another person reusing your code may reinitialize "a="hello"". then still his program compiles and get executed without error and showing/storing wrong information. Happy Coding...😊


Because based on the type only the memory will be allocated to the variable


The data type of a variable declares the type of data that a variable can store and how much memory is required to store this data. The data type also defines the format in which a data of particular type should be stored. Source : http://www.techcrashcourse.com/2015/05/c-programming-language-data-types.html


In order to make our programme easier for us and the compiler we need to specify data type In this 21st Century​ we can't just leave everything default Learning type casting is very important in this cases. As union concept is outdated so Data type declaration is the only thing that would make you an efficient programmer


in c++ that is...python works without specifying


ntoh..ambo pun hok barghu lg belaja.Dok tahu sangak..