Comparison of programming languages (basic instructions)
This article needs additional citations for verification. (February 2009) |
Comparison of programming languages is a common topic of discussion among software engineers. Basic instructions of several programming languages are compared here.
Type identifiers
8 bit (byte) | 16 bit (short integer) | 32 bit | 64 bit (long integer) | Word size | Arbitrarily precise (bignum) | ||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Signed | Unsigned | Signed | Unsigned | Signed | Unsigned | Signed | Unsigned | Signed | Unsigned | ||
C (C99 fixed-width) | int8_t | uint8_t | int16_t | uint16_t | int32_t | uint32_t | int64_t | uint64_t | int | unsigned int | — |
C (C99 variable-width) | signed char | unsigned char | short[a] | unsigned short[a] | long[a] | unsigned long[a] | long long[a] | unsigned long long[a] | |||
C++ | signed char | unsigned char | short[a] | unsigned short[a] | long[a] | unsigned long[a] | long long[a] | unsigned long long[a] | |||
Objective-C | signed char | unsigned char | short[a] | unsigned short[a] | long[a] | unsigned long[a] | long long[a] | unsigned long long[a] | int or NSInteger |
unsigned int or NSUInteger | |
C# | sbyte | byte | short | ushort | int | uint | long | ulong | — | ||
Java | byte; | — | char;[b] | — | — | java.math.BigInteger | |||||
Common Lisp | |||||||||||
Scheme | |||||||||||
Pascal (FPC) | shortint | byte | smallint | word | longint | longword | int64 | qword | integer | cardinal | — |
Visual Basic | — | Byte | Integer | — | Long | — | — | ||||
Visual Basic .NET | SByte | Short | UShort | Integer | UInteger | Long | ULong | ||||
Python 2.x | — | int | — | long | |||||||
Python 3.x | — | int | |||||||||
JavaScript | — | ||||||||||
S-Lang | |||||||||||
FORTRAN 77 | — | INTEGER | — | ||||||||
PHP | — | int | — | [d] | |||||||
Perl 5 | —[c] | Math::BigInt | |||||||||
Ruby | — | Fixnum | — | Bignum | |||||||
Windows PowerShell | — | ||||||||||
OCaml | — | int32 | — | int64 | — | int or nativeint |
open Big_int;; big_int | ||||
Haskell (GHC) | «import Int» Int8 |
«import Word» Word8 |
«import Int» Int16 |
«import Word» Word16 |
«import Int» Int32 |
«import Word» Word32 |
«import Int» Int64 |
«import Word» Word64 |
Int | «import Word» Word |
Integer |
- ^a – The C and C++ languages do not specify the exact width of the integer types "short", "int", "long", and (C99) "long long", so they are implementation-dependent. The "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 often 64 bits wide). C99 also defines the "[u]intN_t" exact-width types in the stdint.h header. See C syntax#Integral types for more information.
- ^b – Commonly used for characters.
- ^c – Perl 5 does not have distinct types. Integers, floating point numbers, strings, etc. are all considered "scalars".
- ^d – PHP has two arbitrary-precision libraries. The BCMath library just uses strings as datatype. The GMP library uses an internal "resource" type.
Single precision | Double precision | Processor dependent | |
---|---|---|---|
C | float[a] | double | —[a] |
Objective-C | |||
C++ (STL) | |||
C# | float | — | |
Java | |||
Common Lisp | |||
Scheme | |||
Pascal (Free Pascal) | single | double | real |
Visual Basic | Single | Double | — |
Visual Basic .NET | |||
Python | — | float | |
JavaScript | Number[1] | — | |
S-Lang | |||
FORTRAN 77 | REAL | DOUBLE PRECISION | |
PHP | float | ||
Perl | |||
Ruby | — | Float | — |
Windows PowerShell | |||
OCaml | — | float | |
Haskell (GHC) | Float | Double |
^a declarations of single precision often are not honored
Integer | Floating point | |
---|---|---|
C (C99) | — | type complex x; |
C++ (STL) | «std::»complex<type> x; | |
C# | — | |
Java | ||
Objective-C | ||
Common Lisp | (setf x #C(re im)) or (setf x (complex re im)) | |
Scheme | (define x re+imi) | |
Pascal | — | |
Visual Basic | ||
Visual Basic .NET | ||
Perl | use Math::Complex; ... $x = re + im*i; or $x = cplx(re, im); | |
Python | x = re + imj or x = complex(re, im) | |
JavaScript | — | |
S-Lang | — | x = re + imi; or x = re + imj; |
FORTRAN 77 | COMPLEX X | |
Ruby | require "complex" ... x = re + im.im or x = Complex(re, im) | |
Windows PowerShell | — | |
OCaml | — | open Complex;; let x = { re = re; im = im } |
Haskell (GHC) | import Complex x = re :+ im |
Other variable types
Text | Boolean | Object/Universal | ||
---|---|---|---|---|
Character | String[a] | |||
C (C99) | char | — | bool[b] | void * |
C++ (STL) | «std::»string | |||
C# | string | bool | object | |
Java | String | boolean | Object | |
Objective-C | unichar | NSString * | BOOL | id |
Common Lisp | ||||
Scheme | ||||
Pascal(ISO) | char | — | boolean | — |
Object Pascal (Delphi) | string | |||
Visual Basic | — | String | Boolean | Variant |
Visual Basic .NET | Char | Object | ||
Python | — | str | bool | object |
JavaScript | string | boolean | ||
S-Lang | ||||
FORTRAN 77 | CHARACTER*1 | — | LOGICAL | — |
PHP | — | string | bool | object |
Perl | ||||
Ruby | Fixnum or String[c] | String | TrueClass, FalseClass | Object |
Windows PowerShell | ||||
OCaml | char | string | bool | — |
Haskell (GHC) | Char | String | Bool |
^a
^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 In Ruby, characters do not have a specific type. Indexing a String results in a Fixnum, representing the character code of the character.
Derived types
fixed array | dynamic array | |||
---|---|---|---|---|
one-dimensional array | multi-dimensional array | one-dimensional array | multi-dimensional array | |
C (C99) | —[2] | —[2] | ||
C++ (STL) | «std::»vector<type> x(size); | |||
C# | type[size] | type[size1, size2,...] | System.Collections.ArrayList or System.Collections.Generic.List<type> | |
Java | type[size][3] | type[size1][size2]...[3][4] | ArrayList or ArrayList<type> | |
Objective-C | NSMutableArray | |||
JavaScript | ||||
Common Lisp | ||||
Scheme | ||||
Pascal | array[first..last] of type[5] | array[first1..last1] of array[first2..last2] ... of type [5] or |
||
Visual Basic | ||||
Visual Basic .NET | System.Collections.ArrayList or System.Collections.Generic.List(Of type) | |||
Python | list | |||
S-Lang | ||||
FORTRAN 77 | ||||
PHP | ||||
Perl | ||||
Ruby | Array | |||
Windows PowerShell | ||||
OCaml | ||||
Haskell (GHC) |
Composite types
Simple composite types | Algebraic data types | Unions | ||
---|---|---|---|---|
Records | Tuple expression | |||
C (C99) | struct «name» {type name;...}; | — | union {type name;...}; | |
Objective-C | ||||
C++ | [6][7] | |||
C# | struct name {type name;...} | — | ||
Java | [6] | |||
JavaScript | ||||
Common Lisp | (cons val1 val2)[8] | |||
Scheme | — | |||
Pascal[9] | «name=»record
... |
— | «name=»record
name: type; ... | |
Visual Basic | ||||
Visual Basic .NET | Structure name
... |
|||
Python | [6] | «(»val1, val2, val3, ... «)» | — | |
S-Lang | struct {name [=value], ...} | |||
FORTRAN 77 | ||||
PHP | [6] | |||
Perl | [10] | — | ||
Ruby | [6] | |||
Windows PowerShell | ||||
OCaml | type name = {«mutable» name : type;...} | «(»val1, val2, val3, ... «)» | type name = Foo «of type» | Bar «of type» | ... | |
Haskell | data Name = Constr {name :: type,...} | (val1, val2, val3, ... ) | data Name = Foo «types» | Bar «types» | ... |
Declarations
variable | constant | type synonym | |
---|---|---|---|
C (C99) | type name «= initial_value»; | enum{ name = value }; | typedef type synonym; |
Objective-C | |||
C++ | const type name = value ; | ||
C# | using synonym = type; | ||
Java | final type name = value; | — | |
JavaScript [11] | var name = initial_value | ||
Common Lisp [11] | (defparameter name initial_value) | (defconstant name value) | (deftype synonym () 'type) |
Scheme[11] | (define name initial_value) | ||
Pascal[9] | name: type «= initial_value»; | name = value; | synonym = type; |
Visual Basic | Dim name As type | Const name As type = value | |
Visual Basic .NET | Dim name As type«= initial_value» | Imports synonym = type | |
Python[11] | x = initial_value | synonym = type[12] | |
S-Lang[11] | x = initial_value; | typedef struct {...} typename | |
FORTRAN 77 | type name | ||
PHP [11] | $x = initial_value; | — | |
Perl [11] | «my »$x = initial_value; | ||
Ruby [11] | x = initial_value | ||
Windows PowerShell [11] | «[type]» $name = initial_value | ||
OCaml[13] | let name «: type» = initial_value | type synonym = type | |
Haskell[13] | «name::type;» name = initial_value | type Synonym = type |
Statements in guillemets (« … ») are optional.
Conditional statements
if | else if | select case | conditional expression | |
---|---|---|---|---|
C (C99) | if (condition) {instructions} «else {instructions}» | if (condition) {instructions} else if (condition) {instructions} ... «else {instructions}» | switch (variable) {case case1: instructions «break;» ... «default: instructions»} | condition ? valueIfTrue : valueIfFalse |
Objective-C | ||||
C++ (STL) | ||||
Java | ||||
JavaScript | ||||
PHP | ||||
C# | switch (variable) {case case1: instructions; jump statement;... «default: instructions; jump statement;»} | |||
Windows PowerShell | if (condition) { instructions } elseif (condition) { instructions } ... «else { instructions }» | switch (variable) { case1 { instructions «break;» ... «default { instructions }»} | ||
Perl | if (condition) {instructions} «else {instructions}» or unless (notcondition) {instructions} «else {instructions}» |
if (condition) {instructions} elsif (condition) {instructions} ... «else {instructions}» or unless (notcondition) {instructions} elsif (condition) {instructions} ... «else {instructions}» |
use feature "switch"; ... given (variable) {when (case1) { instructions } ... «default { instructions }»} | condition ? valueIfTrue : valueIfFalse |
Ruby | if condition
|
if condition
«else
|
case variable when case1
«else
| |
Common Lisp | (when condition instructions) or (if condition (progn instructions) «(progn instructions)») |
(cond (condition1 instructions) (condition2 instructions) ... «(t instructions)») | (case (variable) (case1 instructions) (case2 instructions) ... «(t instructions)») | (if condition valueIfTrue valueIfFalse) |
Scheme | (when condition instructions) or (if condition (begin instructions) «(begin instructions)») |
(cond (condition1 instructions) (condition2 instructions) ... «(else instructions)») | (case (variable) ((case1) instructions) ((case2) instructions) ... «(else instructions)») | |
Pascal | if condition then begin
«else begin
|
if condition then begin
else if condition then begin
... «else begin
|
case variable of case1: instructions; ... «else: instructions » end; |
|
Visual Basic | If condition Then
|
If condition Then
«Else
|
Select Case variable Case case1
«Case Else
|
IIf(condition, valueIfTrue, valueIfFalse) |
Visual Basic .NET | If(condition, valueIfTrue, valueIfFalse) | |||
Python | if condition : Tab instructions «else: Tab ↹ instructions» |
if condition : Tab ↹ instructions elif condition : Tab ↹ instructions ... «else: Tab ↹ instructions» |
— | valueIfTrue if condition else valueIfFalse |
S-Lang | if (condition) { instructions } «else { instructions }» | if (condition) { instructions } else if (condition) { instructions } ... «else { instructions }» | switch (variable) { case case1: instructions } { case case2: instructions } ... | |
FORTRAN 77 | IF condition THEN instructions «ELSE instructions» ENDIF |
IF condition THEN instructions ELSEIF condition THEN instructions ... «ELSE instructions» ENDIF |
index = f(variable) GOTO ( c1, c2, ... cn) index ... c1 instructions |
|
OCaml | if condition then expression «else expression» or if condition then begin instructions end «else begin instructions end» |
if condition then expression else if condition then expression ... «else expression» or if condition then begin instructions end else if condition then begin instructions end ... «else begin instructions end» |
match value with pattern1 -> expression | pattern2 -> expression ... «| _ -> expression»[14] | if condition then valueIfTrue else valueIfFalse |
Haskell (GHC) | if condition then expression else expression or when condition (do instructions) or unless notcondition (do instructions) |
if condition then expression else if condition then expression ... else expression or result | condition = expression | condition = expression ... «| otherwise = expression» |
case value of {pattern1 -> expression; pattern2 -> expression ... «; _ -> expression»}[14] | |
if | else if | select case | conditional expression |
The bold is the literal code. The non-bold is interpreted by the reader. Statements in guillemets (« … ») are optional.
while | do while | for i = 1 to N | foreach | |
---|---|---|---|---|
C (C99) | while (condition) { instructions } | do { instructions } while (condition) | for («type» i = 0; i < N; i++) { instructions } | — |
Objective-C | for (type item in set) { instructions } | |||
C++ (STL) | «std::»for_each(start, end, function) | |||
C# | foreach (type item in set) { instructions } | |||
Java | for (type item : set) { instructions } | |||
JavaScript | for (var i = 0; i < N; i++) { instructions } | for (var property in object) { instructions } | ||
PHP | foreach (range(0, N-1) as $i) { instructions } or for ($i = 0; $i < N; $i++) { instructions } |
foreach (set as item) { instructions } or foreach (set as key => item) { instructions } | ||
Windows PowerShell | for ($i = 0; $i -lt N; $i++) { instructions } | foreach (item in set) { instructions using item } | ||
Perl | while (condition) { instructions } or until (notcondition) { instructions }[15] |
do { instructions } while (condition) or do { instructions } until (notcondition)[15] |
for«each» «$i» (0 .. N-1) { instructions } or for ($i = 0; $i < N; $i++) { instructions } |
for«each» «$item» (set) { instructions } |
Ruby | while condition
or until notcondition
|
begin
or begin
|
for i in 0...N
or 0.upto(N-1) { |i| instructions } |
for item in set
or set.each { |item| instructions } |
Common Lisp | (loop while condition do instructions) or (do () (notcondition) instructions)[15] |
(loop do instructions while condition) | (loop for i from 0 to (-1 N) do instructions) or (dotimes (i N) instructions) or (do ((i 0 (1+ i))) ((>= i N)) instructions) |
(loop for item in set do instructions) or (dolist (item set) instructions) |
Scheme | (do () (notcondition) instructions)[15] or (let loop () (if condition (begin instructions (loop)))) |
(let loop () (instructions (if condition (loop)))) | (do ((i 0 (+ i 1))) ((>= i N)) instructions) or (let loop ((i 0)) (if (< i N) (begin instructions (loop (+ i 1))))) |
(for-each (lambda (item) instructions) list) |
Pascal | while condition do begin
|
repeat
|
for i := 1 «step 1» to N do begin
|
— |
Visual Basic | Do While condition
or Do Until notcondition
|
Do
or Do
|
For i = 1 To N «Step 1»
|
For Each item In set
|
Visual Basic .NET | For i «As type» = 1 To N «Step 1»
|
For Each item As type In set
| ||
Python | while condition : Tab ↹ instructions «else: Tab ↹ instructions» |
— | for i in range(0, N): Tab ↹ instructions «else: Tab ↹ instructions» |
for item in set: Tab ↹ instructions «else: Tab ↹ instructions» |
S-Lang | while (condition) { instructions } «then optional-block» | do { instructions } while (condition) «then optional-block» | for (i = 0; i < N; i++) { instructions } «then optional-block» | foreach item(set) «using (what)» { instructions } «then optional-block» |
FORTRAN 77 | data-sort-value="" style="background: var(--background-color-interactive, #ececec); color: var(--color-base, inherit); vertical-align: middle; text-align: center; " class="table-na" | — | DO nnnn I = 1,N instructions nnnn CONTINUE |
— | |
OCaml | while condition do instructions done | — | for i = 0 to N-1 do instructions done | Array.iter (fun item -> instructions) array List.iter (fun item -> instructions) list |
Haskell (GHC) | — | Control.Monad.forM_ [0..N-1] (\i -> do instructions) | Control.Monad.forM_ list (\item -> do instructions) |
The bold is the literal code. The non-bold is interpreted by the reader. Statements in guillemets (« … ») are optional.
throw | handler | assertion | |
---|---|---|---|
C (C99) | longjmp(state, exception); | switch (setjmp(state)) { case 0: instructions break; case exception: instructions ... } | assert(condition); |
C++ (STL) | throw exception; | try { instructions } catch «(exception)» { instructions } ... | |
C# | try { instructions } catch «(exception)» { instructions } ... «finally { instructions }» | Debug.Assert(condition); | |
Java | try { instructions } catch (exception) { instructions } ... «finally { instructions }» | assert condition; | |
JavaScript | try { instructions } catch (exception) { instructions } «finally { instructions }» | ? | |
PHP | try { instructions } catch (exception) { instructions } ... | assert(condition); | |
S-Lang | try { instructions } catch «exception» { instructions } ... «finally { instructions }» | ? | |
Windows PowerShell | trap «[exception]» { instructions } ... instructions | [Debug]::Assert(condition) | |
Objective-C | @throw exception; | @try { instructions } @catch (exception) { instructions } ... «@finally { instructions }» | NSAssert(condition, description); |
Perl | die exception; | eval { instructions }; if ($@) { instructions } | ? |
Ruby | raise exception | begin
«else
|
|
Common Lisp | (error exception) | (handler-case (progn instructions) (exception instructions) ...) | ? |
Scheme (R6RS) | (raise exception) | (guard (con (condition instructions) ...) instructions) | ? |
Pascal | raise Exception.Create() | try Except on E: exception do begin instructions end; end; | ? |
Visual Basic | — | ? | |
Visual Basic .NET | Throw exception | Try
«Finally
|
Debug.Assert(condition) |
Python | raise exception | try: Tab ↹ instructions except «exception»: Tab ↹ instructions ... «else: Tab ↹ instructions» «finally: Tab ↹ instructions» |
assert condition |
FORTRAN 77 | — | ? | |
OCaml | raise exception | try expression with pattern -> expression ... | assert condition |
Haskell (GHC) | throw exception or throwError expression |
catch tryExpression catchExpression or catchError tryExpression catchExpression |
assert condition expression |
Other control flow statements
exit block(break) | continue | label | branch (goto) | return value from generator | |
---|---|---|---|---|---|
C (C99) | break; | continue; | label: | goto label; | — |
Objective-C | |||||
C++ (STL) | |||||
C# | yield return value; | ||||
Java | break «label»; | continue «label»; | — | ||
JavaScript | |||||
PHP | break «levels»; | continue «levels»; | — | goto ; | return() ; |
Perl | last «label»; | next «label»; | label: | goto label; | |
Common Lisp | (return) or (return-from block) |
(go tag) | |||
Scheme | |||||
Pascal(ISO) | — | label: [9] | goto label; | — | |
Pascal(FPC) | break; | continue; | |||
Visual Basic | Exit block | — | label: | GoTo label | |
Visual Basic .NET | Continue block | ||||
Python | break | continue | — | yield value | |
S-Lang | break; | continue; | |||
FORTRAN 77 | — | a number in columns 1 to 5 | GOTO label | — | |
Ruby | break | next | |||
Windows PowerShell | break« label» | continue | |||
OCaml | — | ||||
Haskell (GHC) |
Statements in guillemets (« … ») are optional. See reflection for calling and declaring functions by strings.
calling a function | basic/void function | value-returning function | required main function | |
---|---|---|---|---|
C (C99) | foo(«parameters»); | void foo(«parameters») { instructions } | type foo(«parameters») { instructions ... return value; } | int main(«int argc, char *argv[]») { instructions } |
Objective-C | ||||
C++ (STL) | ||||
C# | static void Main(«string[] args») { instructions } or static int Main(«string[] args») { instructions } | |||
Java | public static void main(String[] args) { instructions } or public static void main(String... args) { instructions } | |||
JavaScript | function foo(«parameters») { instructions } or var foo = function («parameters») {instructions } or var foo = new Function («"parameter", ... ,"last parameter"» "instructions"); |
function foo(«parameters») { instructions ... return value; } | — | |
Common Lisp | (foo «parameters») | — | (defun foo (parameters) instructions) | |
Scheme | (define (foo parameters) instructions) or (define foo (lambda (parameters) instructions)) | |||
Pascal | foo«(parameters)» | procedure foo«(parameters)»; «forward;» [17] «var
|
function foo«(parameters)»: type; «forward;»[17] «var
foo := value |
program name; «var
|
Visual Basic | Foo(«parameters») | Sub Foo(«parameters»)
|
Function Foo(«parameters») As type
Foo = value |
Sub Main()
|
Visual Basic .NET | Function Foo(«parameters») As type
Return value |
Sub Main(«ByVal CmdArgs() As String»)
or Function Main(«ByVal CmdArgs() As String») As Integer
| ||
Python[18] | foo(«parameters») | def foo(«parameters»): Tab ↹ instructions |
def foo(«parameters»): Tab ↹ instructions Tab ↹ return value |
— |
S-Lang | foo(«parameters» «;qualifiers») | define foo («parameters») { instructions } | define foo («parameters») { instructions ... return value; } | public define slsh_main () { instructions } |
FORTRAN 77 | SUBROUTINE FOO«(parameters)»
|
type FUNCTION FOO«(parameters)»
FOO = value |
PROGRAM main | |
PHP | foo(«parameters») | function foo(«parameters») { instructions } | function foo(«parameters») { instructions ... return value; } | — |
Perl | foo(«parameters») or &foo«(parameters)» |
sub foo { «my (parameters) = @_;» instructions } | sub foo { «my (parameters) = @_;» instructions... «return» value; } | |
Ruby | foo«(parameters)» | def foo«(parameters)»
|
def foo«(parameters)»
expression resulting in return value or def foo«(parameters)»
return value | |
Windows PowerShell | function foo «(parameters)» { instructions }; or function foo { «param(parameters)» instructions } |
function foo «(parameters)» { instructions … return value }; or function foo { «param(parameters)» instructions … return value } | ||
OCaml | foo parameters | — | let «rec» foo parameters = expression or let foo = fun parameters -> expression | |
Haskell | foo parameters = expression or foo parameters = do ... return value |
«main :: IO ()» main = do instructions |
Where string is a signed decimal number:
string to integer | string to long integer | string to floating point | integer to string | floating point to string | |
---|---|---|---|---|---|
C (C99) | integer = atoi(string); | long = atol(string); | float = atof(string); | sprintf(string, "%i", integer); | sprintf(string, "%f", float); |
Objective-C | integer = [string intValue]; | long = [string longLongValue]; | float = [string doubleValue]; | string = [NSString stringWithFormat:@"%i", integer]; | string = [NSString stringWithFormat:@"%f", float]; |
C++ (STL) | «std::»istringstream(string) >> number; | «std::»ostringstream o; o << number; string = o.str(); | |||
C# | integer = int.Parse(string); | long = long.Parse(string); | float = float.Parse(string); or double = double.Parse(string); |
string = number.ToString(); | |
Java | integer = Integer.parseInt(string); | long = Long.parseLong(string); | float = Float.parseFloat(string); or double = Double.parseDouble(string); |
string = Integer.toString(integer); | string = Float.toString(float); or string = Double.toString(double); |
JavaScript [19] | integer = parseInt(string); | float = parseFloat(string); or float = new Number (string) or float = string*1; |
— | string = number.toString (); or string = new String (number); or string = number+""; | |
Common Lisp | (setf integer (parse-integer string)) | (setf float (read-from-string string)) | (setf string (princ-to-string number)) | ||
Scheme | (define number (string->number string)) | (define string (number->string number)) | |||
Pascal | integer := StrToInt(string); | float := StrToFloat(string); | string := IntToStr(integer); | string := FloatToStr(float); | |
Visual Basic | integer = CInt(string) | long = CLng(string) | float = CSng(string) or double = CDbl(string) |
string = CStr(number) | |
Visual Basic .NET | |||||
Python | integer = int(string) | long = long(string) | float = float(string) | string = str(number) | |
S-Lang | integer = atoi(string); | long = atol(string); | float = atof(string); | string = string(number); | |
FORTRAN 77 | READ(string,format) integer | — | READ(string,format) float | WRITE(string,format) number | |
PHP | integer = intval(string); or integer = (int)string; |
float = floatval(string); or float = (float)string; |
string = "number"; or string = strval(number); or string = (string)number; | ||
Perl | [20] | string = "integer"; or string = sprintf("%d", integer); |
string = "float"; or string = sprintf("%f", float); | ||
Ruby | integer = string.to_i | float = string.to_f | string = number.to_s | ||
Windows PowerShell | integer = [int]string | long = [long]string | float = [float]string | string = [string]number; or string = "number"; or string = (number).ToString() | |
OCaml | let integer = int_of_string string | let float = float_of_string string | let string = string_of_int integer | let string = string_of_float float | |
Haskell (GHC) | number = read string | string = show number |
read from | write to | ||
---|---|---|---|
stdin | stdout | stderr | |
C (C99) | scanf(format, &x); or fscanf(stdin, format, &x); [21] |
printf( format, x); or fprintf(stdout, format, x); [22] |
fprintf(stderr, format, x );[23] |
Objective-C | |||
C++ | «std::»cin >> x; or «std::»getline(«std::»cin, str); |
«std::»cout << x; | «std::»cerr << x; or «std::»clog << x; |
C# | x = Console.Read(); or x = Console.ReadLine(); |
Console.Write(«format, »x); or Console.WriteLine(«format, »x); |
Console.Error.Write(«format, »x); or Console.Error.WriteLine(«format, »x); |
Java | x = System.in.read(); or x = new Scanner(System.in).nextInt(); or x = new Scanner(System.in).nextLine(); |
System.out.print(x); or System.out.printf(format, x); or System.out.println(x); |
System.err.print(x); or System.err.printf(format, x); or System.err.println(x); |
JavaScript Web Browser implementation |
document.write(x) | ||
JavaScript Active Server Pages |
Response.Write(x) | ||
JavaScript Windows Script Host |
x = WScript.StdIn.Read(chars) or x = WScript.StdIn.ReadLine() |
WScript.Echo(x) or WScript.StdOut.Write(x) or WScript.StdOut.WriteLine(x) |
WScript.StdErr.Write(x) or WScript.StdErr.WriteLine(x) |
Common Lisp | (setf x (read-line)) | (princ x) or (format t format x) |
(princ x *error-output*) or (format *error-output* format x) |
Scheme (R6RS) | (define x (read-line)) | (display x) or (format #t format x) |
(display x (current-error-port)) or (format (current-error-port) format x) |
Pascal | read(x); or readln(x); |
write(x); or writeln(x); |
— |
Visual Basic | Input« prompt,» x | Print x or ? x | |
Visual Basic .NET | x = Console.Read() or x = Console.ReadLine() |
Console.Write(«format, »x) or Console.WriteLine(«format, »x) |
Console.Error.Write(«format, »x) or Console.Error.WriteLine(«format, »x) |
Python 2.x | x = raw_input(«prompt») | print x or sys.stdout.write(x) |
print >> sys.stderr, x or sys.stderr.write(x) |
Python 3.x | x = input(«prompt») | print(x«, end=""») | print(x«, end=""», file=sys.stderr) |
S-Lang | fgets (&x, stdin) | fputs (x, stdout) | fputs (x, stderr) |
FORTRAN 77 | READ(5,format) variable names | WRITE(6,format) expressions | — |
PHP | $x = fgets(STDIN); or $x = fscanf(STDIN, format); |
print x; or echo x; or printf(format, x); |
fprintf(STDERR, format, x); |
Perl | $x = <>; or $x = <STDIN>; |
print x; or printf format, x; |
print STDERR x; or printf STDERR format, x; |
Ruby | x = gets | puts x or printf(format, x) |
$stderr.puts(x) or $stderr.printf(format, x) |
Windows PowerShell | $x = Read-Host«« -Prompt» text»; or $x = [Console]::Read(); or $x = [Console]::ReadLine() |
x; or Write-Output x; or echo x |
Write-Error x |
OCaml | let x = read_int () or let str = read_line () or Scanf.scanf format (fun x ... -> ...) |
print_int x or print_endline str or Printf.printf format x ... |
prerr_int x or prerr_endline str or Printf.eprintf format x ... |
Haskell (GHC) | x <- readLn or str <- getLine |
print x or putStrLn str |
hPrint stderr x or hPutStrLn stderr str |
Reading command-line arguments
Argument values | Argument counts | Program name / Script name | |
---|---|---|---|
C (C99) | argv[n] | argc | first argument |
Objective-C | |||
C++ | |||
C# | args[n] | args.Length | Assembly.GetEntryAssembly().Location; |
Java | args.length | ||
JavaScript Windows Script Host implementation |
WScript.Arguments(n) | WScript.Arguments.length | ? |
Common Lisp | ? | ? | ? |
Scheme (R6RS) | (list-ref (command-line) n) | (length (command-line)) | first argument |
Pascal | ParamStr(n) | ParamCount | first argument |
Visual Basic | Command | — | ? |
Visual Basic .NET | CmdArgs(n) | CmdArgs.Length | [Assembly].GetEntryAssembly().Location |
Python | sys.argv[n] | len(sys.argv) | first argument |
S-Lang | __argv[n] | __argc | first argument |
FORTRAN 77 | — | ? | |
PHP | $argv[n] | $argc | first argument |
Perl | $ARGV[n] | scalar(@ARGV) | $0 |
Ruby | ARGV[n] | ARGV.size | $0 |
Windows PowerShell | $args[n] | $args.Length | $MyInvocation.MyCommand.Name |
OCaml | Sys.argv.(n) | Array.length Sys.argv | first argument |
Haskell (GHC) | do { args <- System.getArgs; return args !! n } | do { args <- System.getArgs; return length args } | System.getProgName |
Execution of commands
Shell command | Execute program | |
---|---|---|
C | system("command"); | execl(path, args); or execv(path, arglist); |
C++ | ||
C# | System.Diagnostics.Process.Start(path, argstring); | |
Visual Basic | Interaction.Shell(command «, WindowStyle» «, isWaitOnReturn») | |
Visual Basic .NET | Microsoft.VisualBasic.Interaction.Shell(command «, WindowStyle» «, isWaitOnReturn») or System.Diagnostics.Process.Start(path, argstring) | |
Java | Runtime.exec(command); or new ProcessBuilder(command).start(); | |
JavaScript Windows Script Host implementation |
WScript.CreateObject ("WScript.Shell").Run(command «, WindowStyle» «, isWaitOnReturn»); | |
Common Lisp | (shell command) | |
Scheme | (system command) | |
Pascal | system(command); | |
OCaml | Sys.command command | |
Haskell (GHC) | System.system command | |
Perl | system(command) or $output = `command` |
exec(path, args) |
Ruby | system(command) or output = `command` |
exec(path, args) |
PHP | system(command) or output = `command` |
exec(command) |
Python | os.system(command) | subprocess.Popen(command) or os.execv(path, args) |
S-Lang | system(command) | |
Windows PowerShell | [Diagnostics.Process]::Start(command) | «Invoke-Item »program arg1 arg2 … |
Notes
- ^ 8.5 The Number Type [1]
- ^ a b Arrays in C are pointers
- ^ a b The C-like "type x[]" works in Java, however "type[] x" is the preferred form of array declaration.
- ^ Multidimensional arrays in this language are actually arrays of arrays (Iliffe vector). i.e. arrays of references to subarrays.
- ^ a b c Subranges are use to define the bounds of the array
- ^ a b c d e Only classes are supported.
- ^
struct
s in C++ are actually classes - ^ pair only
- ^ a b c Cite error: The named reference
Pascal declare
was invoked but never defined (see the help page). - ^ 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.
- ^ a b c d e f g h i In this language, types are dynamic. Type checking is done on the actual object at runtime.
- ^ Types are just regular objects in Python, so you can just assign them.
- ^ a b Types in this language are automatically inferred and checked at compile time. Type annotations as given here are completely optional.
- ^ a b This syntax performs pattern matching and is very powerful. It is usually used to deconstruct algebraic data types.
- ^ a b c d e f g h i The instructions are repeated while the condition is false
- ^ a b "step n" is used to change the loop interval. If "step" is omitted, then the loop interval is 1.
- ^ a b Pascal requires "forward;" for forward declaration.
- ^ Python also supports decorators on functions which transparently wrap the function as given with another function
- ^ JavaScript only uses floating point numbers so there are some technicalities
- ^ Perl doesn't have separate types. Strings and numbers are interchangeable.
- ^
gets(x)
andfgets(x, length, stdin)
read unformatted text from stdin. Use ofgets
is not recommended - ^
puts(x)
andfputs(x, stdout)
write unformatted text to stdout - ^
fputs(x, stderr)
writes unformatted text to stderr