Comparison Table for Pascal and C

In a console program to test the C code fragments tabulated below we included headers as follows:
#include "stdio.h"
#include "stdlib.h"
#include "stdbool.h"
#include "string.h"
#include "time.h"
Language Feature Pascal C
Statement separator ; ; (required to terminate each statement, even before else)
Comment

//Comment after code
{Comment spanning
lines}

//Comment after code
/* Comment spanning
lines */

Quoted string

'Hello World'

"Hello World"

Declaring constants const VAT_RATE = 0.2; const float VAT_RATE = 0.2
Declaring variables

var
  forename, surname : string;
  paysTax : Boolean;
  totalHits : integer;
  averageScore : double;
               //same as real
  initial: char;
               //same as ansiChar    
  months : array [1..12] of
           string;
  rainfall : array [2004 .. 2009,
              1..12] of integer;
  ageRecords : array[1..1000] of
               TRec;


Also  wideChar,  byte,  single, currency,  TDateTime ...


char forename[20], surname[20];
bool pays_tax;
int total_hits;
double average_score;

char initial;

char months[12][9];
  
int rainfall[11][6];
struct age_record age_records[1000];
//where age_record has been declared as a struct


Also short,  time_t ...

Assigning variables

surname := 'Morrison';
averageScore := 24.56;
paysTax := True;
initial := 'N';
dateSubmitted := StrToDateTime(
                  '27/3/2015');




months[1] := 'January';
rainfall[2000, 1] := 15; // or  
rainfall[2000][1] := 15;

strcpy(surname, "Morrison");
average_score = 24.56;
pays_tax = true; //lower case t
initial = 'N';  //Single quotes  
struct tm date_submitted;
date_submitted.tm_mday = 27;
date_submitted.tm_mon = 2;
date_submitted.tm_year = 2015 - 1900;
date_submitted.tm_hour = 0;
date_submitted.tm_min = 0;
date_submitted.tm_sec = 0;
strcpy(months[0], "January");
rainfall[0][0] = 15;

If statement
  One statement
  executed if true



  More than one
  statement
  executed if true


if (currentNum > max) then
  max := currentNum
else
  writeln('max still ', max);


if (currentNum > max) then
  begin
    max := currentNum;
    writeln('max increased');
  end
else
  write('max still ', max);

if (current_num > max)
  max = current_num;
else
  printf("max still %d", max);

if (current_num > max)
{
  max = current_num;
  printf("max increased");
}
else
  printf("max still %d", max);

Case/Switch statement

case iChoice of
  1 : DoThis;
  2 : DoThat;
end;

switch(choice)
{
  case 1: DoThis();
          break;
  case 2: DoThat();
          break;
}

For loop

for i := 1 to 10 do
  begin
    lines of code
  end;
  
for i := 10 downto 1 do
  begin
    lines of code
  end;

for (i = 1; i <= 10; i++)
{
  lines of code
}

for (i = 10; i >= 1; i--)
{
  lines of code
}

While loop

while amount > 0 do
  begin
    lines of code
  end;

while (amount > 0)
{
  lines of code
}

Repeat loop

repeat
  lines of code
until finished = true;

do
{
  lines of code
}
while (!finished);

Defining records/structs

type
  TAgeRec = record
    surname : string;
    age : integer;
  end;

struct age_record
{
  char surname[20];
  int age;
};

Operators for:
  addition
  subtraction
  multiplication
  real division
  integer division
  remainder


+
-
*
/
div
mod


+
-
*
/ (with a real operand)
/ (with integer operands)
%

Procedure without parameters

procedure ListRecords;
begin
  lines of code
end;


To call the procedure:
ListRecords;

void list_records()
{
  lines of code
}


To call the procedure:
list_records();

Procedure with parameter passed by value

procedure ListRec(recNo: Integer);
begin
  lines of code
end;


To call the procedure for the fifth record:
ListRec(5);
//or pass a variable that has the value 5

void list_rec(int rec_no)
{
  lines of code
}
                  
                  
To call the procedure for the fifth record:
list_rec(5);
//or pass a variable that has the value 5

Procedure with parameter passed by reference

procedure Calc(var total :
              integer);
begin
  lines of code
end;

public calc(int *total)
{
  lines of code to manipulate *total
}
//call with e.g.
calc(&score);

Function

function Cube(Num: double) : double;
begin
   Cube := Num * Num * Num;
end;

double cube(double num)
{
  return num * num * num;
}

Inbuilt string functions
  Type conversions
                  
                  
  


  String manipulation


IntToStr converts integer to string
FloatToStr converts real to string
FloatToStrf converts real to formatted string
DateTimeToStr converts from TDateTime to string
StrToInt converts string to integer  
                  

Length returns length of  string



Pos looks for one string within another
and returns the position of the first matching character.

(StrUtils Unit)
MidStr returns the required number of characters
from any position in the string
LeftStr returns the required number of characters
from the front of the string
RightStr returns the required number of characters
from the back of the string
UpperCase  returns a string in upper case
LowerCase returns a string in lower case


char str_my_int[15], str_my_float[15];
sprintf(str_my_int, "%d", my_int);
sprintf(str_my_float, "%4.2f", my_float);
ctime()
int i_my_str = strtol(my_int_str, NULL, 10); //base 10

char alph[27] =
  "abcdefghijklmnopqrstuvwxyz";
int str_len = strlen(alph);



// strstr returns pointer to
// substring within string                  
int z_pos = strstr(alph, "jkl") - alph;
char substr[4];
strncpy(substr, alph + 9, 3);

strncpy(substr, alph, 3);

strncpy(substr, alph + strlen(alph) - 3, 3);
                  
strupr(alphabet)
strlwr(forename)

Writing a text file

var MyFile : TextFile;
begin
  assignFile (MyFile, 'F:\line.txt');
  rewrite (MyFile);
  writeln('One line only');
  closeFile (MyFile);
end;

FILE *f_ptr;
f_ptr = fopen("F:test.txt", "w");
// write lines line 1, l2 and l3
fprintf(f_ptr, "line 1\nl 2\nl 3\n");
fclose(f_ptr);

Reading a text file

var
  line : String;
  myFile : TextFile;
begin
  assignFile(myFile, 'F:\line.txt');
  reset(myFile);
  readln(myFile, line);
  closeFile(MyFile);
end;

(Shows how to read the whole contents)

FILE *f_ptr;
char line[50];
f_ptr = fopen("F:test.txt", "r");
while(fgets(line, 50, f_ptr))
  printf(line);
fclose(f_ptr);

Programming - a skill for life!

How to apply your knowledge of Pascal when learning C/C++