Notes 3 --- Introduction to C for Pascal
Programmers
Dialects of C:
There are two main divisions here: the language definition and the interface with the compiler and the operating
system.
- There are at least four main versions of language definition, though only the first three will be significant:
- K & R C: For “Kernighan and Ritchie”. Dennis Ritchie invented the language, and he and Brian
Kernighan wrote the definitive manual in 1977.
- ANSI C: ANSI is the American National Standards Organization. ANSI C was defined in the late 1980’s.
The primary differences are: function prototypes were added, more support for strong typing was provided, and
some features were added for portability to non-UNIX systems.
- C++: Developed by Bjarne Stroustrup in the 1980’s. Added object-oriented features and more support for
strong typing to C.
- Parallel C languages: Add features to C or C++ to support explicit data parallelism.
- We will be concerned with two principal types of interface:
- The UNIX interface is text-based, and uses standard UNIX operating systems features.
- The Turbo/Borland interface is a menu-based integrated environment, much like the Turbo Pascal
environment. Turbo uses an ANSI C or C++ language definition.
- Significant differences (other than the way in which programs are entered, compiled, and run) include:
- Utility functions may be located in different library modules in UNIX and Turbo, or in C and C++. In
addition, each may provide some library functions not required by the ANSI definition and not provided by the
other.
- The file access features (discussed later) are slightly different both from UNIX to Turbo, and from C to C++.
- Turbo provides quite a bit more in the way of easily accessible debugging and profiling.
- The Turbo compiler will usually complain when a K & R feature is used; a UNIX compiler will
generally let it go in the interest of backward compatibility.
- Information on the lexical structure.
- Declarations, scopes, and modules in C.
- Comment characters.
- The C type system.
- Variables and constants.
- Enumerated types.
- Structures and unions.
- Initialization.
- The set of C operators.
- Accessors for structured types.
- Fundamentals.
- Parameters. Emulating var parameters.
- Return values and the return statement.
- Static variables and storage classes.
- Typedef for structures and function pointers.
- printf and scanf.
- Using files for read/write.
- File manipulation commands in C and Pascal.
- Discussion of C++ to be added.
Compilation:
- Standard compilation of a C program invokes a compiler (typically cc or gcc) at the
operating system command line on the source file, and produces an object file named a.out by default. We
can redirect the output into another file by using the redirect.
cc myprog.c > myprog.exe...............(or)
cc -o myprog.exe myprog.c (preferable)
- The program is executed by typing the file name at the command line; redirects can be used to have the
program get its input from one file and put its output on another. The defaults are: input from the keyboard, output
to the screen, errors to the screen; A given program may redirect input, output, both, or neither.
myprog.exe < datafile.1 > outputfile.1
- Turbo C/C++ compilation instead invokes the compiler via the Compile command inside the Turbo
environment, and produces a file with the same base and extension exe (so myprog.c will
automatically compile into myprog.exe).
- In Turbo, the Run command executes the exe file associated with the current program. Input
arguments (including files) can be specified via the Arguments command of the Run menu.
- There are other useful flags for the cc/gcc/tcc commands. In particular, in a UNIX environment, the
-g flag compiles the program to permit debugging and profiling. This flag is implicitly on in compiling in
Turbo C/C++.
- Large projects are typically managed by makefiles (and in Turbo by
declaring a Project ).