User:PGSONIC/Pascal and C

From Wikipedia, the free encyclopedia
WARNING - THIS IS A WORK IN PROGRESS PAGE TO REPLACE Comparison of Pascal and C. THIS PAGE IS BY NO MEANS COMPLETE.

This is a comparison of the programming languages Pascal and C. It is divided into three sections describing the advantages of each.

Advantages of C[edit]

Modularity[edit]

C offers modular programming, albeit not initially. The C preprocessor, developed a few years before the first edition of The C Programming Language was written, contained a preprocessing directive called #include that allowed one to include a file. The #include directive is in two formats:

#include <file>
#include "file"

The version with angle brackets allows you to include a file from the standard include directory (usually /usr/include on Unix systems), while the other form allows you to include a file relative to the current directory, or in another directory. For example:

#include <stdio.h>                            /* includes /usr/include/stdio.h */
#include <sys/stat.h>                         /* includes /usr/include/sys/stat.h */
#include "helper.h"                           /* includes ./helper.h */
#include "/sharedfiles/includefiles/helper.h" /* includes /sharedfiles/includefiles/helper.h */

Pascal does not offer such an ability, relying on the standard built-ins like read and ord. Many versions of Pascal, including Borland Pascal and ISO's standardized Extended Pascal, offer this ability (albeit differently: Borland uses the keyword USES and Extended Pascal uses IMPORT).

Strings in C[edit]

Both C and Pascal share the disadvantage of only having a fixed string size, but the representation of a string in C and Pascal are different. In C, a string is an array of char, where the last element is code 0 no matter what the character set, which is represented as '\0'. In Pascal, a string is a packed array of characters, where the array indices start at 1.

Pascal enforces the differentiation of strings of different lengths; the following program is invalid:

PROGRAM X;
VAR
    A : PACKED ARRAY[1..5] OF CHAR;
    B : PACKED ARRAY[1..6] OF CHAR;
BEGIN
    A := 'Hello';
    B := 'Planet';
    IF A <> B THEN
         B := A;
END.

because the two strings cannot be compared. C solves this problem by providing no built-in string manipulation. A C string is just an array of characters. In C arrays cannot be assigned to, and in most contexts they evaluate to the address of their first element, so using the comparison == operator would always yield 0 (false) when comparing distinct arrays in memory. (Identical string literals needn't be distinct in memory.) But library functions declared in string.h, as well as some in stdio.h and stdlib.h, can be used to manipulate strings. For example, the above program in C is:

#include <string.h>

int main(void)
{
    char a[6] = "Hello"; /* Make room for the '\0' */
    char b[] = "Planet"; /* The size (7) is automatically computed if omitted */
    if (strcmp(a, b) != 0) /* strcmp returns negative, zero, or positive
                            * depending on which parameter is lexicographically
                            * greater (zero if they're equal) */
        strcpy(b, a); /* strcpy(destination, source) */
    return 0;
}

The '\0' at the end of every string provides the advantage that the functions can work regardless of the string length. In Pascal, this is not possible.

Because in C arrays and pointers are in many ways equivalent, one can use dynamic memory allocation with the malloc function set to create arbitrary-length strings. In Pascal, even if you pack a pointer to a character array, you can't give it a string literal.

Many Pascal implementations, as well as Extended Pascal, provide arbitrary length strings and built-in string functions to fix this.

System programming[edit]

C gives no protection of any kind, is low-level, and offers bitwise operators, thus allowing it to be used for purposes such as operating system development. Pascal, on the other hand, is a highly protected high-level language for programs and has no bit manipulation. The only way to provide bit support in Pascal is to link C functions into Pascal - a compiler-dependent and non-standard feature.

File system access[edit]

Neither Pascal nor C provide built-in access to the file system, but Pascal does not offer it in any way, while C provides stdio.h, which does this. The reason is that the original implementation of Pascal was run on a computer without explicit file systems as we know them. Many Pascal implementations offer extensions to Pascal's RESET/REWRITE functions that allow you to open functions. Extended Pascal uses "binding" to access the file system.

Advantages of Pascal[edit]

Ranges[edit]

Pascal can use ranges of numbers for building types and sets (see below). In C, one has to explicitly define the range using comparison operators. Also, Pascal offers the ability to make range types:

TYPE
    Grade = 0..100;
    USCreditScore = 300..850;

In C, one has to provide error checking to make this work with the built-in types. Bit fields is a helpful way to provide for 0..n ranges, but it is still not as accurate as Pascal's system.

Sets[edit]

Pascal's ability to handle sets is both a blessing and a curse. The blessing is that sets simplify complicated code. The curse is that the size of sets is highly implementation defined.

Strings in Pascal[edit]

Although C has string advantages, Pascal does not need a standard library and functions to compare strings, giving it a bit of an advantage.

Because C strings have the '\0' at the end, character sets like RADIX-50 will not work with C, where that character code is a valid character (in RADIX-50's case, the space - which is really bad for C). Pascal does not have that restriction.

Further reading[edit]