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, var, or var 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.

Integers

 * The standard constants  and   can be used to determine how many  s and  s can be usefully prefixed to   and  . The actual sizes of ,  , and   are available as the constants  ,  , and   etc.
 * Commonly used for characters.
 * The ALGOL 68, C and C++ languages do not specify the exact width of the integer types,  ,  , and (C99, C++11)  , so they are implementation-dependent. In C and C++  ,  , and   types are required to be at least 16, 32, and 64 bits wide, respectively, but can be more. The   type is required to be at least as wide as   and at most as wide as  , 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   exact-width types in the stdint.h header. See C syntax for more information. In addition the types   and   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  is provided by the  intrinsic function.
 * ALGOL 68G's runtime option  can set precision for  s to the required "number" significant digits. The standard constants   and   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. " ", 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 (SmallInteger) and one supporting arbitrary sized integers (LargeInteger). 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 .   (8 bits),   (16 bits) and   (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.
 * in PHP has the same width as  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  and   can be used to determine how many  s and  s can be usefully prefixed to   and  . The actual sizes of ,  , and   are available as the constants  ,   and   etc. With the constants  ,   and   available for each type's machine epsilon.
 * declarations of single precision often are not honored
 * The value of  is provided by the  intrinsic function.
 * ALGOL 68G's runtime option  can set precision for  s to the required "number" significant digits. The standard constants   and   can be used to determine actual precision.
 * These IEEE floating-point types will be introduced in the next COBOL standard.
 * Same size as  on many implementations.
 * Swift supports 80-bit extended precision floating point type, equivalent to  in C languages.

Complex numbers

 * The value of  is provided by the  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   evaluates to true and everything in   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   is provided by the intrinsic function.

Array

 * In most expressions (except the  and   operators), values of array types in C are automatically converted to a pointer of its first argument. See C syntax for further details of syntax and pointer operations.
 * The C-like  works in Java, however   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  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.
 * 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  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  or   variable and it is never assigned to later, there is an "unused variable" warning. If no value is provided for a   or   or   variable, there is an error. There is a "non-upper-case globals" error for non-uppercase   variables. After it is defined, a   variable can only be assigned to in an   block or function.

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.
 * may be used instead of the period at the end.
 * In Rust, the comma 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  ).

Loop statements

 * " n" is used to change the loop interval. If " " is omitted, then the loop interval is 1.
 * This implements the universal quantifier ("for all" or "$$\forall$$") as well as the existential quantifier ("there exists" or "$$\exists$$").
 * may be used instead of.
 * may be used instead of.
 * Type of set expression must implement trait.

Exceptions

 * Common Lisp allows,   and   to define restarts for use with  . 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 " " for forward declarations.
 * Eiffel allows the specification of an application's root class and feature.
 * In Fortran, function/subroutine parameters are called arguments (since  is a language keyword); the   keyword is required for subroutines.
 * Instead of using, 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.
 * or  may be used instead of.
 * is available to convert any type that has an implementation of the trait. Both   and  return a   that contains the specified type if there is no error. The turbofish  on   can be omitted if the type can be inferred from context.

Standard stream I/O

 * ALGOL 68 additionally as the "unformatted" transput routines:,  ,  , and.
 * and  read unformatted text from stdin. Use of gets is not recommended.
 * and  write unformatted text to stdout.
 * writes unformatted text to stderr
 * INPUT_UNIT, OUTPUT_UNIT, ERROR_UNIT are defined in the ISO_FORTRAN_ENV module.

Reading command-line arguments

 * In Rust,  and   return iterators,   and   respectively.   converts each argument to a   and it panics if it reaches an argument that cannot be converted to UTF-8.   returns a non-lossy representation of the raw strings from the operating system, which can be invalid UTF-8.
 * In Visual Basic, command-line arguments are not separated. Separating them requires a split function.
 * 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  statements.

Execution of commands
Fortran 2008 or newer.