Comparison of Pascal and C
The computer programming languages C and Pascal have similar times of origin, influences, and purposes. Both were used to design their own compilers early in their lifetimes. The original Pascal definition appeared in 1969 and a first compiler in 1970. The first version of C appeared in 1972.
Both are descendants of the ALGOL language series. ALGOL introduced programming language support for structured programming, where programs are constructed of single entry and single exit constructs such as if, while, for and case. Pascal stems directly from ALGOL W, while it shared some new ideas with ALGOL 68. The C language is more indirectly related to ALGOL, originally through B, BCPL, and CPL, and later through ALGOL 68 and also Pascal. Some Pascal dialects also incorporated traits from C.
The languages documented here are the Pascal designed by Niklaus Wirth, as standardized as ISO 7185 in 1982, and the C designed by Dennis Ritchie, as standardized as C89 in 1989. The reason is that these versions both represent the mature version of the language, and also because they are comparatively close in time. ANSI C and C99 features, and features of later implementations of Pascal are not included in the comparison, despite the improvements in robustness and functionality that they conferred e.g. Comparison of Pascal and Delphi
Syntax
, Pascal is much more ALGOL-like than C. English keywords are retained where C uses punctuation symbols – Pascal hasand, or, and mod where C uses &&, ||, and % for example. However, C is more ALGOL-like than Pascal regarding declarations, retaining the type-name ''variable-name'' syntax. For example, C can accept declarations at the start of any block, not just the outer block of a function.Semicolon use
Another, more subtle, difference is the role of the semicolon. In Pascal, semicolons separate individual statements within a compound statement; instead in C, they terminate the statement. In C, they are also syntactically part of the statement. This difference manifests mainly in two situations:- in Pascal, a semicolon can never be directly before
else, whereas in C, it is mandatory, unless a block statement is used - the last statement before an
endoruntilis not required to be followed by a semicolon
Comments
In the first edition of C, there are only/* block comments */. C later added // line comments.In Pascal, there are
and .Identifiers and keywords
C and Pascal differ in their interpretation of upper and lower case. C is case sensitive while Pascal is not, thusMyLabel and mylabel are distinct names in C but identical in Pascal. In both languages, identifiers consist of letters and digits, with the rule that the first character may not be a digit. In C, the underscore counts as a letter, so even _abc is a valid name. Names with a leading underscore are often used to differentiate special system identifiers in C.Both C and Pascal use keywords. Examples are if, while, const, for and goto, which are keywords that happen to be common to both languages. In C, the basic built-in type names are also keywords or combinations of keywords, while in Pascal the built-in type names are predefined normal identifiers.
Definitions, declarations, and blocks
In Pascal, subroutine definitions start with keywords procedure or function and type definitions with type. In C, all subroutines havefunction definitions and type definitions use the keyword typedef. Both languages use a mix of keywords and punctuation for definitions of complex types; for instance, arrays are defined by the keyword array in Pascal and by punctuation in C, while enumerations are defined by the keyword enum in C but by punctuation in Pascal.In Pascal subroutines, begin and end delimit a block of statements preceded by local declarations, while C functions use "" to delimit a block of statements optionally preceded by declarations : C strictly defines that any declarations must occur before the statements within a particular block but allows blocks to appear within blocks, which is a way to go around this. By its syntax of a subroutine's body, Pascal enforces that declarations occur before statements. Pascal also allows definitions of types and functionsnot only variable declarations to be encapsulated by function definitions to any level of depth.
Implementation
The grammars of both languages are of a similar size. From an implementation perspective the main difference between the two languages is that to parse C it is necessary to have access to a symbol table for types, while in Pascal there is only one such construct, assignment. For instance, the C fragmentX * Y; could be a declaration of Y to be an object whose type is pointer to X, or a statement-expression that multiplies X and Y. In contrast, the corresponding Pascal fragments var Y : ^X; and Z := X * Y; are inherently unambiguous; correct parsing does not require a symbol table.Simple types
Integers
Pascal requires all variable and function declarations to specify their type explicitly. In traditional C, a type name may be omitted in most contexts and the default typeint is then implicitly assumed.C accommodates different sizes and signed and unsigned modes for integers by using modifiers such as
long, short, signed, unsigned, etc. The exact meaning of the resulting integer type is machine-dependent, what can be guaranteed is that long int is no shorter than int and int is no shorter than short int. However, in C standard, there are at least minimal sizes of types are specified which guarantees char to be a single byte and int to be at least two bytes.Subranges
In Pascal, a similar end is performed by declaring a subrange of integer :type a = 1..100;
b = -20..20;
c = 0..100000;
This subrange feature is not supported by C.
A major, if subtle, difference between C and Pascal is how they promote integer operations. In Pascal, the result of an operation is defined for all integer/subrange types, even if intermediate results do not fit into an integer. The result is undefined only if it does not fit into the integer/subrange on the left hand side of the assignment. This may imply an artificial restriction on the range of integer types, or may require slow execution to handle the intermediate results: However, the compiler may take advantage of restricted subranges to produce more efficient code.
In C, operands must first be promoted to the size of the required result: intermediate results are undefined if they do not fit into the range of the promoted operands. If range of the required result is greater than the range of operands, this normally produces slow inefficient code, even from a good optimising compiler. However, a C compiler is never required or expected to handle out of range intermediate results: it is the programmers responsibility to ensure that all intermediate results fit into the operand range.
Pre-Standard implementations of C as well as Small-C et al. allowed integer and pointer types to be relatively freely intermixed.
Character types
In C the character type ischar which is a kind of integer that is no longer than short int,. Expressions such as 'x' + 1 are therefore perfectly legal, as are declarations such as int i = 'i'; and char c = 74;.This integer nature of
char is clearly illustrated by declarations such asunsigned char uc = 255; // common limit
signed char sc = -128; // common negative limit
Whether the
char type should be regarded as signed or unsigned by default is up to the implementation.In Pascal, characters and integers are distinct types. The inbuilt compiler functions
ord and chr can be used to typecast single characters to the corresponding integer value of the character set in use, and vice versa. e.g. on systems using the ASCII character set ord = 49 and chr is a TAB character.Boolean types
In Pascal, boolean is an enumerated type. The possible values of boolean are false and true, with ordinal value of false = 0 and true = 1. For conversion to integer, ord is used:i := ord;
There is no standard function for integer to boolean, however, the conversion is simple in practice:
b := i <> 0;
C has a Boolean type,
bool, since C23, but did not have one prior. C uses binary valued relational operators which may be regarded as Boolean in the sense that they always give results that are either zero or one. As all tests are performed by zero-checks, false is represented by zero, while true is represented by any other value. This is visible in the bool numeric datatype defined in .