C/C++ after Pascal
This is the start of a new section to help you to apply your knowledge of Pascal when learning C and C++. C++ extends the capabilities of C, so that C code should compile with a C++ compiler. We advise you to try C first, so in this tutorial the sections on C++ follow those on C.
C is more difficult than Pascal, but the transition from Pascal to C is manageable. You should be comfortable with handling pointers in Pascal before attempting C. We advise you to learn C# before C if you have the time so that learning C will be even less challenging. It is easy to find guidance on C, but be aware that much of this is intended for professionals rather than school students.
- case sensitive (C) or case insensitive (Pascal);
- variables declared in-line (C) or in a named section (var section in Pascal);
- range checking of array indices (Pascal) or usually not implemented (C);
- identifiers typically camel case (e.g. myNum in C++ and MyNum in Pascal) or lower case with underscores (my_num in C);
- inbuilt primitive types such as integer and char;
- arrays, lists, and records/structs;
- operators for purposes such as assignment, addition, subtraction, multiplication, division (including integer division), comparison and Boolean expressions;
- selection by if and case/switch;
- procedures and functions (and their parameters passed by value or by reference);
- inbuilt routines e.g. for string manipulation;
- other means of splitting up and reusing the code such as by classes (C++) and files;
- enumerated types (enums in C++) and sets (C++);
- file handling and database access;
- error trapping;
- event handling;
- pointers/C++ references;
- networking capabilities.
The next page shows a comparison table, giving the C equivalents of fragments of Pascal code.
Compiling the Demonstration Code
See Getting Started with Code::Blocks for advice on (1) downloading the free IDE, (2) installing it on Windows and on the Debian Linux operating system of a Raspberry Pi and (3) using it for the first time. If you only need to compile a few short programs you can try the inbuilt gcc compiler without an IDE as follows.
Windows on a PC
Open the Command Prompt and change directory to that containing your C source file. The instruction g++ main.c -o my_prog will compile the C source file named main.c to an executable named my_prog.exe. Execute it with the command my_prog.
Debian Linux on a Raspberry Pi
Open the LXTerminal on the Raspberry Pi and change directory to that containing your C source file. The instruction g++ main.c -o my_prog will compile the C source file named main.c to an executable named my_prog. (Copy the code into an editor such as Leafpad and save it as, for example, main.c). Execute my_prog with the command ./my_prog.