my-server
← Wiki

Comparison of programming languages (basic instructions)

This article compares a large number of programming languages by tabulating their data types, their expression, statement, and declaration syntax, and some common operating-system interfaces.

Conventions of this article

Generally, var, , or is how variable names or other non-literal values to be interpreted by the reader are represented. The rest is literal code. Guillemets ( and ) enclose optional sections. indicates a necessary (whitespace) indentation.

The tables are not sorted lexicographically ascending by programming language name by default, and that some languages have entries in some tables but not others.

Type identifiers

Integers

  • The standard constants <code>int shorts</code> and <code>int lengths</code> can be used to determine how many <code>short</code>s and <code>long</code>s can be usefully prefixed to <code>short int</code> and <code>long int</code>. The actual sizes of <code>short int</code>, <code>int</code>, and <code>long int</code> are available as the constants <code>short max int</code>, <code>max int</code>, and <code>long max int</code> etc.
  • Commonly used for characters.
  • The ALGOL 68, C and C++ languages do not specify the exact width of the integer types <code>short</code>, <code>int</code>, <code>long</code>, and (C99, C++11) <code>long long</code>, so they are implementation-dependent. In C and C++ <code>short</code>, <code>long</code>, and <code>long long</code> types are required to be at least 16, 32, and 64 bits wide, respectively, but can be more. The <code>int</code> type is required to be at least as wide as <code>short</code> and at most as wide as <code>long</code>, and is typically the width of the word size on the processor of the machine (i.e. on a 32-bit machine it is often 32 bits wide; on 64-bit machines it is sometimes 64 bits wide). C99 and C++11 also define the <code>[u]intN_t</code> exact-width types in the stdint.h header. See C syntax#Integral types for more information. In addition the types <code>size_t</code> and <code>ptrdiff_t</code> are defined in relation to the address size to hold unsigned and signed integers sufficiently large to handle array indices and the difference between pointers.
  • Perl 5 does not have distinct types. Integers, floating point numbers, strings, etc. are all considered "scalars".
  • PHP has two arbitrary-precision libraries. The BCMath library just uses strings as datatype. The GMP library uses an internal "resource" type.
  • The value of <code>n</code> is provided by the <code>SELECTED_INT_KIND</code> intrinsic function.
  • ALGOL 68G's runtime option <code>--precision "number"</code> can set precision for <code>long long int</code>s to the required "number" significant digits. The standard constants <code>long long int width</code> and <code>long long max int</code> can be used to determine actual precision.
  • COBOL allows the specification of a required precision and will automatically select an available type capable of representing the specified precision. "<code>PIC S9999</code>", for example, would require a signed variable of four decimal digits precision. If specified as a binary field, this would select a 16-bit signed type on most platforms.
  • Smalltalk automatically chooses an appropriate representation for integral numbers. Typically, two representations are present, one for integers fitting the native word size minus any tag bit () and one supporting arbitrary sized integers (). Arithmetic operations support polymorphic arguments and return the result in the most appropriate compact representation.
  • Ada range types are checked for boundary violations at run-time (as well as at compile-time for static expressions). Run-time boundary violations raise a "constraint error" exception. Ranges are not restricted to powers of two. Commonly predefined Integer subtypes are: Positive () and Natural (). <code>Short_Short_Integer</code> (8 bits), <code>Short_Integer</code> (16 bits) and <code>Long_Integer</code> (64 bits) are also commonly predefined, but not required by the Ada standard. Runtime checks can be disabled if performance is more important than integrity checks.
  • Ada modulo types implement modulo arithmetic in all operations, i.e. no range violations are possible. Modulos are not restricted to powers of two.
  • Commonly used for characters like Java's char.
  • <code>int</code> in PHP has the same width as <code>long</code> type in C has on that system.
  • Erlang is dynamically typed. The type identifiers are usually used to specify types of record fields and the argument and return types of functions.
  • When it exceeds one word.

Floating point

  • The standard constants <code>real shorts</code> and <code>real lengths</code> can be used to determine how many <code>short</code>s and <code>long</code>s can be usefully prefixed to <code>short real</code> and <code>long real</code>. The actual sizes of <code>short real</code>, <code>real</code>, and <code>long real</code> are available as the constants <code>short max real</code>, <code>max real</code> and <code>long max real</code> etc. With the constants <code>short small real</code>, <code>small real</code> and <code>long small real</code> available for each type's machine epsilon.
  • declarations of single precision often are not honored
  • The value of <code>n</code> is provided by the <code>SELECTED_REAL_KIND</code> intrinsic function.
  • ALGOL 68G's runtime option <code>--precision "number"</code> can set precision for <code>long long real</code>s to the required "number" significant digits. The standard constants <code>long long real width</code> and <code>long long max real</code> can be used to determine actual precision.
  • These IEEE floating-point types will be introduced in the next COBOL standard.
  • Same size as <code>double</code> on many implementations.
  • Swift supports 80-bit extended precision floating point type, equivalent to <code>long double</code> in C languages.

Complex numbers

  • The value of <code>n</code> is provided by the <code>SELECTED_REAL_KIND</code> intrinsic function.
  • Generic type which can be instantiated with any base floating point type.

Other variable types

  • specifically, strings of arbitrary length and automatically managed.
  • This language represents a boolean as an integer where false is represented as a value of zero and true by a non-zero value.
  • All values evaluate to either true or false. Everything in <code>TrueClass</code> evaluates to true and everything in <code>FalseClass</code> evaluates to false.
  • This language does not have a separate character type. Characters are represented as strings of length 1.
  • Enumerations in this language are algebraic types with only nullary constructors
  • The value of <code>n</code> is provided by the <code>SELECTED_INT_KIND</code> intrinsic function.

Derived types

Array

  • In most expressions (except the <code>sizeof</code> and <code>&</code> operators), values of array types in C are automatically converted to a pointer of its first argument. See C syntax#Arrays for further details of syntax and pointer operations.
  • The C-like <code>type x[]</code> works in Java, however <code>type[] x</code> is the preferred form of array declaration.
  • Subranges are used to define the bounds of the array.
  • JavaScript's array are a special kind of object.
  • The <code>DEPENDING ON</code> clause in COBOL does not create a true variable length array and will always allocate the maximum size of the array.

Other types

  • Only classes are supported.
  • <code>struct</code>s in C++ are actually classes, but have default public visibility and are also POD objects. C++11 extended this further, to make classes act identically to POD objects in many more cases.
  • pair only
  • Although Perl doesn't have records, because Perl's type system allows different data types to be in an array, "hashes" (associative arrays) that don't have a variable index would effectively be the same as records.
  • Enumerations in this language are algebraic types with only nullary constructors

Variable and constant declarations

  • Pascal has declaration blocks. See functions.
  • Types are just regular objects, so you can just assign them.
  • In Perl, the "my" keyword scopes the variable into the block.
  • Technically, this does not declare name to be a mutable variable—in ML, all names can only be bound once; rather, it declares name to point to a "reference" data structure, which is a simple mutable cell. The data structure can then be read and written to using the <code>!</code> and operators, respectively.
  • If no initial value is given, an invalid value is automatically assigned (which will trigger a run-time exception if it used before a valid value has been assigned). While this behaviour can be suppressed it is recommended in the interest of predictability. If no invalid value can be found for a type (for example in case of an unconstraint integer type), a valid, yet predictable value is chosen instead.
  • In Rust, if no initial value is given to a <code>let</code> or <code>let mut</code> variable and it is never assigned to later, there is an "unused variable" warning. If no value is provided for a <code>const</code> or <code>static</code> or <code>static mut</code> variable, there is an error. There is a "non-upper-case globals" error for non-uppercase <code>const</code> variables. After it is defined, a <code>static mut</code> variable can only be assigned to in an <code>unsafe</code> block or function.

Control flow

Conditional statements

  • A single instruction can be written on the same line following the colon. Multiple instructions are grouped together in a block which starts on a newline (The indentation is required). The conditional expression syntax does not follow this rule.
  • This is pattern matching and is similar to select case but not the same. It is usually used to deconstruct algebraic data types.
  • In languages of the Pascal family, the semicolon is not part of the statement. It is a separator between statements, not a terminator.
  • <code>END-IF</code> may be used instead of the period at the end.
  • In Rust, the comma (<code>,</code>) at the end of a match arm can be omitted after the last match arm, or after any match arm in which the expression is a block (ends in possibly empty matching brackets <code>{}</code>).

Loop statements

  • "<code>step</code> n" is used to change the loop interval. If "<code>step</code>" is omitted, then the loop interval is 1.
  • This implements the universal quantifier ("for all" or ) as well as the existential quantifier ("there exists" or ).
  • <code>THRU</code> may be used instead of <code>THROUGH</code>.
  • may be used instead of <code>></code>.
  • Type of set expression must implement trait <code>std::iter::IntoIterator</code>.

Exceptions

  • Common Lisp allows <code>with-simple-restart</code>, <code>restart-case</code> and <code>restart-bind</code> to define restarts for use with <code>invoke-restart</code>. Unhandled conditions may cause the implementation to show a restarts menu to the user before unwinding the stack.
  • Uncaught exceptions are propagated to the innermost dynamically enclosing execution. Exceptions are not propagated across tasks (unless these tasks are currently synchronised in a rendezvous).

Other control flow statements

  • Pascal has declaration blocks. See functions.
  • label must be a number between 1 and 99999.

Functions

See reflective programming for calling and declaring functions by strings.

  • Pascal requires "<code>forward;</code>" for forward declarations.
  • Eiffel allows the specification of an application's root class and feature.
  • In Fortran, function/subroutine parameters are called arguments (since <code>PARAMETER</code> is a language keyword); the <code>CALL</code> keyword is required for subroutines.
  • Instead of using <code>"foo"</code>, a string variable may be used instead containing the same value.

Type conversions

Where string is a signed decimal number:

  • JavaScript only uses floating point numbers so there are some technicalities.
  • Perl doesn't have separate types. Strings and numbers are interchangeable.
  • <code>NUMVAL-C</code> or <code>NUMVAL-F</code> may be used instead of <code>NUMVAL</code>.
  • <code>str::parse</code> is available to convert any type that has an implementation of the <code>std::str::FromStr</code> trait. Both <code>str::parse</code> and <code>FromStr::from_str</code> return a <code>Result</code> that contains the specified type if there is no error. The turbofish (<code><nowiki>::<_></nowiki></code>) on <code>str::parse</code> can be omitted if the type can be inferred from context.

Standard stream I/O

  • ALGOL 68 additionally as the "unformatted" transput routines: <code>read</code>, <code>write</code>, <code>get</code>, and <code>put</code>.
  • <code>gets(x)</code> and <code>fgets(x, length, stdin)</code> read unformatted text from stdin. Use of gets is not recommended.
  • <code>puts(x)</code> and <code>fputs(x, stdout)</code> write unformatted text to stdout.
  • <code>fputs(x, stderr)</code> writes unformatted text to stderr
  • are defined in the module.

Reading command-line arguments

  • In Rust, <code>std::env::args</code> and <code>std::env::args_os</code> return iterators, <code>std::env::Args</code> and <code>std::env::ArgsOs</code> respectively. <code>Args</code> converts each argument to a <code>String</code> and it panics if it reaches an argument that cannot be converted to UTF-8. <code>ArgsOs</code> returns a non-lossy representation of the raw strings from the operating system (<code>std::ffi::OsString</code>), which can be invalid UTF-8.
  • In Visual Basic, command-line arguments are not separated. Separating them requires a split function <code>Split(string)</code>.
  • The COBOL standard includes no means to access command-line arguments, but common compiler extensions to access them include defining parameters for the main program or using <code>ACCEPT</code> statements.

Execution of commands

Fortran 2008 or newer.

See also

References