Difference between statically typed and dynamically typed language

Statically typed, will need to always declare of what type of data will be used for the declared variable, say either int, float or string upfront before using it.

Dynamically typed, it will internally declare its type based on the type of the data it is holding or assigned to.

Dynamically TypedStatically Typed
PythonC
JavascriptC++
RubyJava

For example, let’s consider python for dynamically typed

# Now here 'test_var' is holding a value of type string
test_var = 'This is a string'

print(test_var)
This is a string

# Now we are trying to assign the value of type int to the same variable 
# which was holding the string type value
test_var = 10 

print(test_var)
10

Now for statically typed, let’s consider our darling C language

#include<stdio.h>

int main()
{
// Now here 'test_var' is holding a value of type character array
const test_var a[] = "this is a string";
printf("%s\n", a);

// Here we are trying to assign a value of type int to the same variable 
// where a string type value was saved
test_var = 10;
return 0;
}

// This is the error we get while trying to compile the above code
static_typed.c: In function main:
static_typed.c:7:3: error: assignment to expression with array type
 test_var = 10;

Leave a reply:

Your email address will not be published.