Home > OS >  Why do we need to specify the data type of variable before assigning a value?
Why do we need to specify the data type of variable before assigning a value?

Time:08-01

Let’s say we want to define a variable that stores the age of a certain individual;

int main(void) {
 int age=5;
 printf(“%d\n”,age);
}

Here, before assigning the variable, we specify the type of data it would store. Does we define it just to tell the compiler/interpreter how many bytes the value will occupy in memory. No other reason?

CodePudding user response:

Variables usually change their value (that's why they are called variables). The initial value of a variable usually doesn't say much about how the variable is used, and what it's for.

If you want to implement an algorithm which calculates an average age, you might need your variable which holds the age to be non-integer, like float or double. You usually want to initialize it to 0, which is an integer, so the compiler can't come up with a correct suggestion for its type.

int main(void)
{
    int age=5;
    printf("%d\n",age);
    // Now print the age in decades (units of 10 years)
    printf("%d\n", age / 10); // prints 0 - incorrect answer

    double correct_age = 5;
    printf("%f\n", correct_age);
    // Now print the age in decades (units of 10 years)
    printf("%f\n", correct_age / 10); // prints 0.5 - correct answer
}

C has support for type inference: auto age = 5 declares age as an integer, while auto age = 5.0 declares it as double. This support was not deemed important when C was created, and it's still not as important in C as in C , because in C types sometimes have long names.

CodePudding user response:

A variable's type determines the values that the variable can have and the operations that can be performed on it.

CodePudding user response:

Programming languages like C, C , and Java are statically-typed programming languages. The word "static" here means we need to determine the types at compile time. One way to do that is to just specify the type directly in the code. It makes it easier to implement compilers and for developers to read the code. Another way I know is type inference. So instead of specifying the types in the code, we let the compiler infers the types based on the context. The any type in C does exactly that.

Different from statically typed languages, dynamically typed languages like Python and Javascript determine the types at runtime. The benefit is that the code is usually more concise and you can do crazy things like conditionally returning different types of data in a function. The downside is that it makes code maintenance significantly harder and easier to produce bugs. That's why we now have Typescript and type annotations in Python to mitigate these problems.

CodePudding user response:

Here, before assigning the variable, we specify the type of data it would store.

No, that's a mischaracterization. You present a declaration of variable age that happens to include an initialization. Initialization is not assignment. Although in many cases you could separate that into a declaration without initialization and a separate assignment, there are cases where that would not be valid. Overall, the declaration is the primary element in the code presented, not the initialization.

Does we define it just to tell the compiler/interpreter how many bytes the value will occupy in memory. No other reason?

We specify the data type because modern C requires variables to be declared before use, and the data type is a required part of the C syntax for variable declarations. At the language level, that's why, end of story.

As for what benefits that yields, yes, it does convey how much storage to reserve. More importantly, however, it relieves the language implementation from any need to store, track, or dynamically determine objects' types at runtime. The resulting bare-bytes object representations are, generally speaking, congruent with the underlying machine data model. This is memory efficient (which was an order of magnitude more important when C was designed than it is now), and CPU efficient.

C code is also amenable to type-based static analysis, which helps detect (some) bugs early, and it supports a wider variety of basic data types than dynamically typed languages such as Python do.

And speaking of Python, do note that even this poster child for dynamically-typed languages now supports data type annotations to provide a few of the advantages that static typing has to offer.

  •  Tags:  
  • c
  • Related