Comparison of Pascal and C

This difference manifests mainly in two situations: A superfluous semicolon can be put on the last line before end, thereby formally inserting an empty statement.

Names with a leading underscore are often used to differentiate special system identifiers in C. Both C and Pascal use keywords (words reserved for use by the language).

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.

In contrast, the corresponding Pascal fragments var Y : ^X; and Z := X * Y; are inherently unambiguous; correct parsing does not require a symbol table.

In Pascal, a similar end is performed by declaring a subrange of integer (a compiler may then choose to allocate a smaller amount of storage for the declared variable): This subrange feature is not supported by C. A major, if subtle, difference between C and Pascal is how they promote integer operations.

Pre-Standard implementations of C as well as Small-C et al. allowed integer and pointer types to be relatively freely intermixed.

During expression evaluation, and in both languages, a Boolean value may be internally stored as a single bit, a single byte, a full machine word, a position in the generated code, or as a condition code in a status register, depending on machine, compiler, and situation; these factors are usually more important than the language compiled.

In C, integers may be implicitly converted to floating point numbers, and vice versa (though possible precision loss may be flagged by warnings).

In Pascal enumerations are ordinal and parsed using ord(), succ() and pred() functions and are distinct from the array structure.

In C, enumerations are in fact implemented as arrays and red becomes just a synonym for 0, green for 1, blue for 2, and nothing prevents a value outside this range to be assigned to the variable a.

C arrays are simply defined by a base type and the number of elements: and are always indexed from 0 up to SIZE−1 (i.e. modulo SIZE).

In Pascal the number of elements in each array type is determined at compile-time and cannot be changed during the execution of the program.

One way of implementing the above example in original Pascal, but without the automatic size adjustment, is: In both languages, a string is a primitive array of characters.

Here there is an example: There is no equivalent feature to with in C. In C, the exact bit length of a field can be specified: How much storage is used depends on traits (e.g., word-alignment) of the target system.

This feature is available in Pascal by using the subrange construct (3 bits gives a range from 0 to 7) in association with the keyword packed: Both C and Pascal support records which can include different fields overlapping each other: Both language processors are free to allocate only as much space for these records as needed to contain the largest type in the union/record.

The biggest difference between C and Pascal is that Pascal supports the explicit use of a "tagfield" for the language processor to determine if the valid component of the variant record is being accessed: In this case, the tag field q must be set to the right state to access the proper parts of the record.

Pointer arithmetic (a common source of programming errors in C, especially when combined with endianness issues and platform-independent type sizes) is not permitted in Pascal.

It is a common mistake in C, due either to inexperience or to a simple typing error, to accidentally put assignment expressions in conditional statements such as if (a = 10) { ... }.

It is notable that ALGOL's conditional expression in the form z := if a > b then a else b; has an equivalent in C (the ternary operator from CPL) but not in Pascal, which will use if a > b then z:=a; else z:=b;.

When Niklaus Wirth designed Pascal, the desire was to limit the number of levels of precedence (fewer parse routines, after all).

In Pascal a boolean expression that relies on a particular evaluation ordering (possibly via side-effects in function calls) is, more or less, regarded as an error.

The following example introduces a Boolean variable which indicates whether or not the target character has been found: Alternatively, the test for end of array can be separated from the array access and a goto statement can break out of the search if the target is found: Statements for building control structures are roughly analogous and relatively similar (at least the first three).

This is equivalent, but arguably less safe, since it stores program specific information like jump addresses and stack frames in a programmer accessible structure.

Variadic functions are almost impossible to get right with PASCAL and STDCALL methods, because only the caller really knows how many arguments were passed in order to clean them up.

In the following example, the statement (*cmpar)(s1, s2) is equivalent to strcmp(s1, s2): In Pascal functions and procedures can be passed as parameters to functions or procedures: Early C had neither constant declarations nor type declarations, and the C language was originally defined as needing a "preprocessor"; a separate program, and pass, that handled constant, include and macro definitions, to keep memory usage down.

Later, with ANSI C, it obtained constant and type definitions features and the preprocessor also became part of the language, leading to the syntax we see today.

It has a simple low level separate compilation facility, however (traditionally using the same generic linker used for assembly language), Pascal does not.

As a consequence, the variant record became a favourite feature to breach the type system by all programmers in love with tricks, which usually turn into pitfalls and calamities".

In C files do not exist as a built-in type (they are defined in a system header) and all I/O takes place via library calls.

In its original form (as described by Niklaus Wirth), Pascal qualifies as a managed pointer language, some 30 years before either Java or C#.