Comparison_of_programming_languages_(basic_instructions)

Comparison of programming languages (basic instructions)

Comparison of programming languages (basic instructions)

Add article description


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. Tab ↹ 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

More information 8 bit (byte), 16 bit (short integer) ...
  • ^a The standard constants int shorts and int lengths can be used to determine how many shorts and longs can be usefully prefixed to short int and long int. The actual sizes of short int, int, and long int are available as the constants short max int, max int, and long max int etc.
  • ^b Commonly used for characters.
  • ^c The ALGOL 68, C and C++ languages do not specify the exact width of the integer types short, int, long, and (C99, C++11) long long, so they are implementation-dependent. In C and C++ short, long, and long long types are required to be at least 16, 32, and 64 bits wide, respectively, but can be more. The int type is required to be at least as wide as short and at most as wide as long, 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[citation needed] also define the [u]intN_t exact-width types in the stdint.h header. See C syntax#Integral types for more information. In addition the types size_t and ptrdiff_t 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.
  • ^d Perl 5 does not have distinct types. Integers, floating point numbers, strings, etc. are all considered "scalars".
  • ^e PHP has two arbitrary-precision libraries. The BCMath library just uses strings as datatype. The GMP library uses an internal "resource" type.
  • ^f The value of n is provided by the SELECTED_INT_KIND[4] intrinsic function.
  • ^g ALGOL 68G's runtime option --precision "number" can set precision for long long ints to the required "number" significant digits. The standard constants long long int width and long long max int can be used to determine actual precision.
  • ^h COBOL allows the specification of a required precision and will automatically select an available type capable of representing the specified precision. "PIC S9999", 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.
  • ^i 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.
  • ^j 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 (range 1 .. Integer'Last) and Natural (range 0 .. Integer'Last). Short_Short_Integer (8 bits), Short_Integer (16 bits) and Long_Integer (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.
  • ^k Ada modulo types implement modulo arithmetic in all operations, i.e. no range violations are possible. Modulos are not restricted to powers of two.
  • ^l Commonly used for characters like Java's char.
  • ^m int in PHP has the same width as long type in C has on that system.[c]
  • ^n Erlang is dynamically typed. The type identifiers are usually used to specify types of record fields and the argument and return types of functions.[5]
  • ^o When it exceeds one word.[6]

Floating point

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

Complex numbers

More information Integer, Single precision ...
  • ^a The value of n is provided by the SELECTED_REAL_KIND[8] intrinsic function.
  • ^b Generic type which can be instantiated with any base floating point type.

Other variable types

More information Text, Boolean ...
  • ^a specifically, strings of arbitrary length and automatically managed.
  • ^b This language represents a boolean as an integer where false is represented as a value of zero and true by a non-zero value.
  • ^c All values evaluate to either true or false. Everything in TrueClass evaluates to true and everything in FalseClass evaluates to false.
  • ^d This language does not have a separate character type. Characters are represented as strings of length 1.
  • ^e Enumerations in this language are algebraic types with only nullary constructors
  • ^f The value of n is provided by the SELECTED_INT_KIND[4] intrinsic function.

Derived types

Array

More information fixed size array, dynamic size array ...
  • ^a In most expressions (except the sizeof and & 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.
  • ^b The C-like type x[] works in Java, however type[] x is the preferred form of array declaration.
  • ^c Subranges are used to define the bounds of the array.
  • ^d JavaScript's array are a special kind of object.
  • ^e The DEPENDING ON clause in COBOL does not create a true variable length array and will always allocate the maximum size of the array.

Other types

More information Simple composite types, Algebraic data types ...
  • ^a Only classes are supported.
  • ^b structs 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.
  • ^c pair only
  • ^d 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.
  • ^e Enumerations in this language are algebraic types with only nullary constructors

Variable and constant declarations

More information variable, constant ...
  • ^a Pascal has declaration blocks. See functions.
  • ^b Types are just regular objects, so you can just assign them.
  • ^c In Perl, the "my" keyword scopes the variable into the block.
  • ^d 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.
  • ^e 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.
  • ^f In Rust, if no initial value is given to a let or let mut variable and it is never assigned to later, there is an "unused variable" warning. If no value is provided for a const or static or static mut variable, there is an error. There is a "non-upper-case globals" error for non-uppercase const variables. After it is defined, a static mut variable can only be assigned to in an unsafe block or function.

Conditional statements

More information if, else if ...
  • ^a 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.
  • ^b This is pattern matching and is similar to select case but not the same. It is usually used to deconstruct algebraic data types.
  • ^c In languages of the Pascal family, the semicolon is not part of the statement. It is a separator between statements, not a terminator.
  • ^d END-IF may be used instead of the period at the end.
  • ^e 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

More information while loop, do while loop ...
  • ^a "step n" is used to change the loop interval. If "step" is omitted, then the loop interval is 1.
  • ^b This implements the universal quantifier ("for all" or "") as well as the existential quantifier ("there exists" or "").
  • ^c THRU may be used instead of THROUGH.
  • ^d «IS» GREATER «THAN» may be used instead of >.
  • ^e Type of set expression must implement trait std::iter::IntoIterator.

Exceptions

More information throw, handler ...
  • ^a Common Lisp allows with-simple-restart, restart-case and restart-bind to define restarts for use with invoke-restart. Unhandled conditions may cause the implementation to show a restarts menu to the user before unwinding the stack.
  • ^b 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

More information exit block (break), continue ...
  • ^a Pascal has declaration blocks. See functions.
  • ^b label must be a number between 1 and 99999.

See reflective programming for calling and declaring functions by strings.

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

Where string is a signed decimal number:

More information string to integer, string to long integer ...
  • ^a JavaScript only uses floating point numbers so there are some technicalities.[7]
  • ^b Perl doesn't have separate types. Strings and numbers are interchangeable.
  • ^c NUMVAL-C or NUMVAL-F may be used instead of NUMVAL.
  • ^ str::parse is available to convert any type that has an implementation of the std::str::FromStr trait. Both str::parse and FromStr::from_str return a Result that contains the specified type if there is no error. The turbofish (::<_>) on str::parse can be omitted if the type can be inferred from context.
More information read from, write to ...
  • ^a ALGOL 68 additionally as the "unformatted" transput routines: read, write, get, and put.
  • ^b gets(x) and fgets(x, length, stdin) read unformatted text from stdin. Use of gets is not recommended.
  • ^c puts(x) and fputs(x, stdout) write unformatted text to stdout.
  • ^d fputs(x, stderr) writes unformatted text to stderr
  • ^e INPUT_UNIT, OUTPUT_UNIT, ERROR_UNIT are defined in the ISO_FORTRAN_ENV module.[15]
More information Argument values, Argument counts ...
  • ^a In Rust, std::env::args and std::env::args_os return iterators, std::env::Args and std::env::ArgsOs respectively. Args converts each argument to a String and it panics if it reaches an argument that cannot be converted to UTF-8. ArgsOs returns a non-lossy representation of the raw strings from the operating system (std::ffi::OsString), which can be invalid UTF-8.
  • ^b In Visual Basic, command-line arguments are not separated. Separating them requires a split function Split(string).
  • ^c 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 ACCEPT statements.

Execution of commands

More information Shell command, Execute program ...

^a Fortran 2008 or newer.[17]


References

  1. Ada Reference Manual – Language and Standard Libraries; ISO/IEC 8652:201x (E), "Reference Manual" (PDF). Archived from the original (PDF) on 2011-04-27. Retrieved 2013-07-19.
  2. "Common Lisp HyperSpec (TM)". lispworks.com. Retrieved 30 January 2017.
  3. "www.islisp.info: Specification". islisp.info. Archived from the original on 22 January 2016. Retrieved 30 January 2017.
  4. "selected_int_kind in Fortran Wiki". fortranwiki.org. Retrieved 30 January 2017.
  5. "Erlang — Types and Function Specifications". erlang.org. Retrieved 30 January 2017.
  6. "Erlang — Advanced". erlang.org. Retrieved 30 January 2017.
  7. "selected_real_kind in Fortran Wiki". fortranwiki.org. Retrieved 30 January 2017.
  8. "The GNU C Library: Complex Numbers". gnu.org. Retrieved 30 January 2017.
  9. "Grammar vb". Visual Basic Language Specification. 2016-06-17. Archived from the original on 2019-08-29. Retrieved 2019-08-29.
  10. "for...of". mozilla.org. Retrieved 30 January 2017.
  11. "Try-Catch for VB". google.com. Archived from the original on 16 April 2016. Retrieved 30 January 2017.
  12. Klabnik, Steve; Nichols, Carol. "Error Handling". The Rust Programming Language.
  13. "Prime decomposition – Rosetta Code". rosettacode.org. Retrieved 30 January 2017.
  14. "iso_fortran_env in Fortran Wiki". fortranwiki.org. Retrieved 30 January 2017.
  15. "Execute a system command – Rosetta Code". rosettacode.org. Retrieved 30 January 2017.
  16. "EXECUTE_COMMAND_LINE – The GNU Fortran Compiler". gnu.org. Retrieved 30 January 2017.

Share this article:

This article uses material from the Wikipedia article Comparison_of_programming_languages_(basic_instructions), and is written by contributors. Text is available under a CC BY-SA 4.0 International License; additional terms may apply. Images, videos and audio are available under their respective licenses.