Is C strongly typed?
Solution 1:
"Strongly typed" and "weakly typed" are terms that have no widely agreed-upon technical meaning. Terms that do have a well-defined meaning are
Dynamically typed means that types are attached to values at run time, and an attempt to mix values of different types may cause a "run-time type error". For example, if in Scheme you attempt to add one to true by writing
(+ 1 #t)
this will cause an error. You encounter the error only if you attempt to execute the offending code.Statically typed means that types are checked at compile time, and a program that does not have a static type is rejected by the compiler. For example, if in ML you attempt to add one to true by writing
1 + true
, the program will be rejected with a (probably cryptic) error message. You always get the error even if the code might never be executed.
Different people prefer different systems according in part to how much they value flexibility and how much they worry about run-time errors.
Sometimes "strongly typed" is used loosely to mean "statically typed", and "weakly typed" is used incorrectly to mean "dynamically typed". A better use for the term "strongly typed" is that "you cannot work around or subvert the type system", whereas "weakly typed" means "there are loopholes in the type system". Perversely, most languages with static type systems have loopholes, while many languages with dynamic type systems have no loopholes.
None of these terms are connected in any way with the number of implicit conversions available in a language.
If you want to talk precisely about programming languages, it is best to avoid the terms "strongly typed" and "weakly typed". I would say that C is a language that is statically typed but that has a lot of loopholes. One loophole is that you can freely cast any pointer type to any other pointer type. You can also create a loophole between any two types of your choice by declaring a C union that has two members, one for each of the types in question.
I have written more about static and dynamic typing at why-interpreted-langs-are-mostly-ducktyped-while-compiled-have-strong-typing.
Solution 2:
It's hard to classify every language into 'weakly' or 'strongly' typed -- it's more of a continuum. But, in comparison to other languages, C is fairly strongly typed. Every object has a compile-time type, and the compiler will let you know (loudly) if you're doing something with an object that its type doesn't let you do. For example, you can't call functions with the wrong types of parameters, access struct/union members which don't exist, etc.
But there are a few weaknesses. One major weakness is typecasts - they essentially say that you're going to be mucking around with the types of objects, and the compiler should be quiet (when it can). void*
is also another weakness -- it's a generic pointer to an unknown type, and when you use them, you have to be extra careful that you're doing the right thing. The compiler can't statically check most uses of void*
. void*
can also be converted to a pointer to any type without a cast (only in C, not in C++), which is another weakness.
Solution 3:
C is considered to be weakly typed, because you can convert any type to any other type through a cast, without a compiler error. You can read more about the issue here.
Solution 4:
The literature isn't clear about this. I think that strongly typed isn't yes/no, there are varying degrees of strong typing.
A programming language has a specification of how it executes programs. Sometimes it's not clear how to execute with certain programs. For example, programs that try to subtract a string from a number. Or programs that divide by zero. There are several ways to deal with these conditions. Some languages have rules for dealing with these errors (for example they throw an exception). Other languages just don't have rules to deal with these situations. Those languages generally have type systems to prevent compiling programs that lead to unspecified behavior. And there also exist languages that have unspecified behavior and don't have a type system to prevent these errors at compile time (if you write a program that hits unspecified behavior it might launch the missiles).
So:
Languages that specify what happens at runtime in every case (like adding a number to a string) are called dynamically typed. Languages that prevent executing programs with errors at compile time are statically typed. Languages that don't specify what happens and also don't have a type system to prevent errors are called weakly typed.
So is Java statically typed? Yes, because its type system disallows subtracting a string from a number. No, because it allows you to divide by zero. You could prevent division by zero at compile time with a type system. For example by creating a number type that can't be zero (e.g. NonZeroInt), and only allow to divide by numbers that have this type.
So is C strongly typed or weakly typed? C is strongly typed because the type system disallows some type errors. But it's weakly typed in other cases when it's undefined what happens (and the type system doesn't protect you).
Solution 5:
C is more strongly typed than Javascript and less strongly typed than Ada.
I'd say it falls more into the strongly typed side of the continuum. but someone else might disagree (even if they're wrong).
How's that for definitive?