In C++, you can define variables with auto instead of giving it a default type such as int. Like this (where foo is an int):

int x = foo
auto y = foo

Both contain foo.

In Python, you can define a variable without specifying the data type. Like this:

x = foo

The compiler determines the type of the variable at runtime based on the type of data assigned.

What are the differences between using an auto datatype and dynamic typing?

  • $\begingroup$ For one, and I think it's the most mentioned disadvantage for C++, figuring out what the type of the variable actually is can get pretty hard if your IDE isn't top notch. Especially if foo is a templated function that can depend on its' parameter values. $\endgroup$
    – kouta-kun
    Commented Sep 19, 2023 at 1:15
  • 1
    $\begingroup$ This question may be naively posed, but answers need not be. This may already have answers elsewhere in Internet. I am most interested in differences from the programmer's (ie user of the language) point of view, especially the implications for how code is structured/organised. $\endgroup$
    – Pablo H
    Commented Sep 19, 2023 at 13:31

1 Answer 1


auto is an inferred type but it's still a static type. The difference is that the compiler knows what every value's type is in a statically-typed language (including the actual type of the auto values), but in a dynamically-typed language the type is only known at runtime. Also, variables in a statically-typed language have one fixed type even if they're declared with auto, but variables in a dynamic language can change types during program execution.

Consider the following C++ code:

int main() {
  auto foo = "hello";
  puts(foo / 5);

This code will fail to compile, because you can't divide a const char* by an integer. Notably, even though foo is declared with type auto, the compiler knows that it's actual type is const char*. In fact, using const char* foo = "hello" gives the exact same semantics, auto is just syntax sugar for the programmer.

Now, consider the corresponding Python code:

def main():
  foo = "hello"
  print(foo / 5)

This code will fail at runtime if/when main is called. Python doesn't statically check types, so while you also can't divide a string by an integer, it's only detected when foo / 5 actually runs.

Failing at compile-time is generally regarded as better, but there are other drawbacks to static types (see: "What are the pros and cons of static typing?"). For example, you can't assign an auto variable a value of a different type than it was declared with, because it still has a single consistent ("static") type, the type is just inferred.

int main() {
  // This is OK, `foo` has type `const char*`
  auto foo = "hello";
  foo = "world";
  // So is this, `bar` has type `int`
  auto bar = 42;
  bar = 45;
  // However, this doesn't compile, because `foo` has already been assigned type `const char*`
  foo = 48;

But you can assign differently-typed values the same variable in a language with dynamic types, because foo has no consistent type (it's type is "dynamic").

# This code runs as expected, with no errors
foo = "hello"
foo = 42
print(foo / 2)
  • $\begingroup$ (IMHO, reusing variable names like that, especially for different types, is a code smell and risks confusion.  So that's not a very strong argument.)  Other advantages of static typing include lookup/completion in an IDE, performance/optimisation, and safer refactoring.  One of the few traditional disadvantages was verbosity — but that's dwindling with type inference in modern languages such as Kotlin, which have all the safety and expressivity of static typing but much of the conciseness and fast development of dynamic typing. $\endgroup$
    – gidds
    Commented Apr 13 at 14:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .