User:PGSONIC/Pascal and C


 * 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.

Modularity
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:


 * 1) include
 * 2) 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:


 * 1) include                            /* includes /usr/include/stdio.h */
 * 2) include                         /* includes /usr/include/sys/stat.h */
 * 3) include "helper.h"                          /* includes ./helper.h */
 * 4) 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
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, where the last element is code 0 no matter what the character set, which is represented as. 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:

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   (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:

The  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
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
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.

Ranges
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:

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