Difference between revisions of "Pascal for C users"

From Lazarus wiki
m (See also: Fix typo)
 
(36 intermediate revisions by 15 users not shown)
Line 1: Line 1:
{|   border="1" style="margin: 1em 1em 1em 0; background: #f9f9f9; border: 1px  solid; border-collapse: collapse;"
+
{{Pascal for C users}}
!   C  
+
== Overview ==
!   [[Pascal]]  
+
This page gives some translations between C(++) and Pascal concepts.
!
+
 
 +
== Translation ==
 +
{{Note|Logical operators (AND/OR/XOR etc) are logical or boolean depending on the type of the arguments (boolean or integer). Since Pascal had a separate boolean type from the start, boolean operators on integers were not necessary, and are always logical.}}
 +
 
 +
{| class="wikitable"
 +
! C !! [[Pascal]] !! Notes
 
|-
 
|-
 
|   {  
 
|   {  
Line 22: Line 27:
 
|   /  
 
|   /  
 
|   /  
 
|   /  
|   [[Slash|Division]] (or sometimes [[Div|div]])  
+
|   [[Slash|Float Division]] (Integer: [[Div|div]])  
 
|-
 
|-
 
|   %  
 
|   %  
 
|   [[Mod]]
 
|   [[Mod]]
|  
+
|   '''Mod'''ulo operation
 
|-
 
|-
 
|   !  
 
|   !  
 
|   [[Not]]
 
|   [[Not]]
|  
+
|   Logical not
 
|-
 
|-
 
|   !=  
 
|   !=  
Line 38: Line 43:
 
|   &&  
 
|   &&  
 
|   [[And]]
 
|   [[And]]
|  
+
|   Logical and
 
|-
 
|-
 
| &nbsp; <nowiki> || </nowiki> &nbsp;
 
| &nbsp; <nowiki> || </nowiki> &nbsp;
 
| &nbsp; [[Or]]
 
| &nbsp; [[Or]]
| &nbsp;
+
| &nbsp; Logical or
 +
|-
 +
| &nbsp; & &nbsp;
 +
| &nbsp; [[And]]
 +
| &nbsp; Bitwise and
 +
|-
 +
| &nbsp; <nowiki> | </nowiki> &nbsp;
 +
| &nbsp; [[Or]]
 +
| &nbsp; Bitwise or
 
|-
 
|-
 
| &nbsp; ^ &nbsp;
 
| &nbsp; ^ &nbsp;
 
| &nbsp; [[Xor]]
 
| &nbsp; [[Xor]]
| &nbsp;
+
| &nbsp; Exclusive or
 +
|-
 +
| &nbsp; ~ &nbsp;
 +
| &nbsp; [[Not]]
 +
| &nbsp; One's complement
 
|-
 
|-
 
| &nbsp; >> &nbsp;
 
| &nbsp; >> &nbsp;
 
| &nbsp; [[Shr]]
 
| &nbsp; [[Shr]]
| &nbsp; bit '''sh'''ift '''r'''ight &nbsp;
+
| &nbsp; bit '''sh'''ift '''r'''ight &nbsp; Note: shr is a '''logical bitshift''', not arithmetic. If the left operand is a negative value of a signed type, then the result of an shr operation may not be what you expect.
 
|-
 
|-
 
| &nbsp; << &nbsp;
 
| &nbsp; << &nbsp;
Line 65: Line 82:
 
|-
 
|-
 
| &nbsp; /* &nbsp;
 
| &nbsp; /* &nbsp;
| &nbsp; {
+
| &nbsp; { or (*
| &nbsp;
+
| &nbsp; [[Comments|Comment]] start
 
|-
 
|-
 
| &nbsp; */ &nbsp;
 
| &nbsp; */ &nbsp;
| &nbsp; }
+
| &nbsp; } or *)
| &nbsp;
+
| &nbsp; Comment end
 
|-
 
|-
 
| &nbsp; // &nbsp;
 
| &nbsp; // &nbsp;
 
| &nbsp; //
 
| &nbsp; //
| &nbsp;
+
| &nbsp; End of line comment (only one line comment) &nbsp;
 +
|-
 +
| &nbsp; 0x &nbsp;
 +
| &nbsp; [[Dollar_sign|$]]
 +
| &nbsp; prefix for hex-number e.g. $FFFFFF &nbsp;
 +
|-
 +
| &nbsp; 0 &nbsp;
 +
| &nbsp; [[&|&]]
 +
| &nbsp; prefix for oct-number e.g. &77777777 &nbsp;
 +
|-
 +
| &nbsp; 0b &nbsp;
 +
| &nbsp; [[Percent_sign|%]]
 +
| &nbsp; prefix for bin-number e.g. %11111111 &nbsp;
 +
|-
 +
| &nbsp; & &nbsp;
 +
| &nbsp; [[@]]
 +
| &nbsp; [[@|address operator]]
 +
|-
 +
| &nbsp; * &nbsp;
 +
| &nbsp; [[^]]
 +
| &nbsp; See [[Pointer]] and [[Pointers]]
 
|-
 
|-
 
| &nbsp; if() &nbsp;
 
| &nbsp; if() &nbsp;
Line 111: Line 148:
 
| &nbsp; switch case break default &nbsp;
 
| &nbsp; switch case break default &nbsp;
 
| &nbsp; [[Case]] [[Of]] [[Else]] [[End]] &nbsp;
 
| &nbsp; [[Case]] [[Of]] [[Else]] [[End]] &nbsp;
 +
| &nbsp;
 +
|-
 +
| &nbsp; const a_c_struct *arg &nbsp;
 +
| &nbsp; [[Constref]] arg : a_c_struct &nbsp;
 +
| &nbsp;
 +
|-
 +
| &nbsp; a_c_struct *arg &nbsp;
 +
| &nbsp; [[Var]] arg : a_c_struct &nbsp;
 
| &nbsp;
 
| &nbsp;
 
|-
 
|-
Line 116: Line 161:
  
  
{| &nbsp; border="1" style="margin: 1em 1em 1em 0; background: #f9f9f9; border: 1px  solid; border-collapse: collapse;"
+
{| class="wikitable"
! &nbsp; C type &nbsp;
+
! C type !! [[Pascal]] [[Data_type|type]] !! Size (bits) !! Range !! Notes
! &nbsp; [[Pascal]] [[Data_type|type]] &nbsp;
 
! &nbsp; Size (bits) &nbsp;
 
! &nbsp; Range &nbsp;
 
! &nbsp;
 
 
|-
 
|-
 
| &nbsp; char &nbsp;
 
| &nbsp; char &nbsp;
Line 146: Line 187:
 
|
 
|
 
| &nbsp; Pointer to a null-terminated string &nbsp;  
 
| &nbsp; Pointer to a null-terminated string &nbsp;  
 +
|-
 +
| &nbsp; short int &nbsp;
 +
| &nbsp; [[Smallint]]
 +
| &nbsp; 16-bit
 +
| &nbsp; -32768 .. 32767 &nbsp;
 +
|
 +
|-
 +
| &nbsp; unsigned short int &nbsp;
 +
| &nbsp; [[Word]]
 +
| &nbsp; 16-bit
 +
| &nbsp; 0 .. 65535 &nbsp;
 +
|
 +
|-
 +
| &nbsp; int &nbsp;
 +
| &nbsp; [[Integer]]
 +
| &nbsp; (16-bit or) 32-bit  &nbsp;
 +
| &nbsp; -2147483648..2147483647 &nbsp;
 +
| &nbsp; Generic integer types &nbsp;
 +
|-
 +
| &nbsp; unsigned int &nbsp;
 +
| &nbsp; [[Cardinal]]
 +
| &nbsp; (16-bit or) 32-bit  &nbsp;
 +
| &nbsp; 0 .. 4294967295 &nbsp;
 +
| &nbsp; Generic integer types &nbsp;
 +
|-
 +
| &nbsp; long int &nbsp;
 +
| &nbsp; [[Longint]]
 +
| &nbsp; 32-bit
 +
| &nbsp; -2147483648..2147483647 &nbsp;
 +
| &nbsp; 
 +
|-
 +
| &nbsp; unsigned long int &nbsp;
 +
| &nbsp; [[Longword]]
 +
| &nbsp; 32-bit
 +
| &nbsp; 0 .. 4294967295
 +
| &nbsp; 
 
|-
 
|-
 
| &nbsp; float &nbsp;
 
| &nbsp; float &nbsp;
Line 161: Line 238:
 
|-
 
|-
 
|}
 
|}
 +
 +
 +
{| class="wikitable"
 +
! C type !! [[Pascal]] !! Notes
 +
|-
 +
| &nbsp; struct { } &nbsp;
 +
| &nbsp; [[Record]] [[End]] &nbsp;
 +
| &nbsp;
 +
|-
 +
| &nbsp; union { } &nbsp;
 +
| &nbsp; [[Record]] [[Case]] [[Of]] [[End]] &nbsp;
 +
| &nbsp; [[Case#Variant Record|Variant Record]] &nbsp;
 +
|-
 +
|}
 +
 +
 +
{| class="wikitable"
 +
! C !! [[Pascal]] !! [[Unit]]
 +
|-
 +
| &nbsp; abs &nbsp;
 +
| &nbsp; Abs &nbsp;
 +
| &nbsp; [[System unit|System]] &nbsp;
 +
|-
 +
| &nbsp; acos &nbsp;
 +
| &nbsp; ArcCos &nbsp;
 +
| &nbsp; [[Math unit|Math]] &nbsp;
 +
|-
 +
| &nbsp; asin &nbsp;
 +
| &nbsp; ArcSin &nbsp;
 +
| &nbsp; [[Math unit|Math]] &nbsp;
 +
|-
 +
| &nbsp; atan &nbsp;
 +
| &nbsp; ArcTan &nbsp;
 +
| &nbsp; [[System unit|System]] &nbsp;
 +
|-
 +
| &nbsp; atof &nbsp;
 +
| &nbsp; StrToFloat &nbsp;
 +
| &nbsp; [[sysutils|SysUtils]] &nbsp;
 +
|-
 +
| &nbsp; atoi &nbsp;
 +
| &nbsp; StrToInt &nbsp;
 +
| &nbsp; [[sysutils|SysUtils]] &nbsp;
 +
|-
 +
| &nbsp; atol &nbsp;
 +
| &nbsp; StrToInt &nbsp;
 +
| &nbsp; [[sysutils|SysUtils]] &nbsp;
 +
|-
 +
| &nbsp; atoll &nbsp;
 +
| &nbsp; StrToInt64 &nbsp;
 +
| &nbsp; [[sysutils|SysUtils]] &nbsp;
 +
|-
 +
| &nbsp; ceil &nbsp;
 +
| &nbsp; Ceil &nbsp;
 +
| &nbsp; [[Math unit|Math]] &nbsp;
 +
|-
 +
| &nbsp; cos &nbsp;
 +
| &nbsp; Cos &nbsp;
 +
| &nbsp; [[System unit|System]] &nbsp;
 +
|-
 +
| &nbsp; exp &nbsp;
 +
| &nbsp; Exp &nbsp;
 +
| &nbsp; [[System unit|System]] &nbsp;
 +
|-
 +
| &nbsp; floor &nbsp;
 +
| &nbsp; Floor &nbsp;
 +
| &nbsp; [[Math unit|Math]] &nbsp;
 +
|-
 +
| &nbsp; pow &nbsp;
 +
| &nbsp; Power &nbsp;
 +
| &nbsp; [[Math unit|Math]] &nbsp;
 +
|-
 +
| &nbsp; round &nbsp;
 +
| &nbsp; Round &nbsp;
 +
| &nbsp; [[System unit|System]] &nbsp;
 +
|-
 +
| &nbsp; sin &nbsp;
 +
| &nbsp; Sin &nbsp;
 +
| &nbsp; [[System unit|System]] &nbsp;
 +
|-
 +
| &nbsp; sqrt &nbsp;
 +
| &nbsp; Sqrt &nbsp;
 +
| &nbsp; [[System unit|System]] &nbsp;
 +
|-
 +
| &nbsp; strcpy &nbsp;
 +
| &nbsp; Copy &nbsp;
 +
| &nbsp; [[System unit|System]] &nbsp;
 +
|-
 +
| &nbsp; strlen &nbsp;
 +
| &nbsp; Length &nbsp;
 +
| &nbsp; [[System unit|System]] &nbsp;
 +
|-
 +
| &nbsp; tan &nbsp;
 +
| &nbsp; Tan &nbsp;
 +
| &nbsp; [[Math unit|Math]] &nbsp;
 +
|-
 +
| &nbsp; toupper &nbsp;
 +
| &nbsp; UpCase &nbsp;
 +
| &nbsp; [[System unit|System]] &nbsp;
 +
|-
 +
|}
 +
 +
=== C++ ===
 +
{| class="wikitable"
 +
! C++ type !! [[Pascal]] !! Notes
 +
|-
 +
| &nbsp; class { } &nbsp;
 +
| &nbsp; [[Class]] [[End]] &nbsp;
 +
| &nbsp;
 +
|-
 +
| &nbsp; class: { } &nbsp;
 +
| &nbsp; [[Class]]( ) [[End]] &nbsp;
 +
| &nbsp;
 +
|-
 +
| &nbsp; template <class T> class { } &nbsp;
 +
| &nbsp; [[Generic]] = [[Class]]<T> [[End]] &nbsp;
 +
| &nbsp;
 +
|-
 +
| &nbsp; struct { } &nbsp;
 +
| &nbsp; [[Object]] [[End]] &nbsp;
 +
| &nbsp; If you want methods
 +
|-
 +
|}
 +
 +
== See also ==
 +
 +
* [[C to Pascal]] - conversion tools and libraries
 +
* [http://gd.tuwien.ac.at/languages/pascal/fpc/docs-pdf/CinFreePascal.pdf Short tutorial] that describes the process of using C and C++ code in FreePascal, including writing of wrapper code
 +
* [[Creating bindings for C libraries]]
 +
* [[Common problems when converting C header files]]
 +
* [[SWIG]]
 +
* [https://en.wikipedia.org/wiki/Comparison_of_Pascal_and_C “Comparison of Pascal and C” on the English Wikipedia]

Latest revision as of 23:02, 22 February 2021

Deutsch (de) English (en) français (fr)

Overview

This page gives some translations between C(++) and Pascal concepts.

Translation

Note-icon.png

Note: Logical operators (AND/OR/XOR etc) are logical or boolean depending on the type of the arguments (boolean or integer). Since Pascal had a separate boolean type from the start, boolean operators on integers were not necessary, and are always logical.

C Pascal Notes
  {     Begin    
  }     End  
  =     :=   Becomes  
  ==     =   Equal
  /     /   Float Division (Integer: div)  
  %     Mod   Modulo operation
  !     Not   Logical not
  !=     <>   Not equal  
  &&     And   Logical and
  ||     Or   Logical or
  &     And   Bitwise and
  |     Or   Bitwise or
  ^     Xor   Exclusive or
  ~     Not   One's complement
  >>     Shr   bit shift right   Note: shr is a logical bitshift, not arithmetic. If the left operand is a negative value of a signed type, then the result of an shr operation may not be what you expect.
  <<     Shl   bit shift left  
  ++     Inc  
  --     Dec  
  /*     { or (*   Comment start
  */     } or *)   Comment end
  //     //   End of line comment (only one line comment)  
  0x     $   prefix for hex-number e.g. $FFFFFF  
  0     &   prefix for oct-number e.g. &77777777  
  0b     %   prefix for bin-number e.g. %11111111  
  &     @   address operator
  *     ^   See Pointer and Pointers
  if()     If Then  
  if() else     If Then Else  
  while     While Do  
  do while     Repeat Until Not   
  do while !     Repeat Until    
  for ++     For To Do  
  for --     For Downto Do    
  switch case break     Case Of End  
  switch case break default     Case Of Else End    
  const a_c_struct *arg     Constref arg : a_c_struct    
  a_c_struct *arg     Var arg : a_c_struct    


C type Pascal type Size (bits) Range Notes
  char     Char     8-bit     ASCII
  signed char     Shortint   8-bit   -128 .. 127  
  unsigned char     Byte   8-bit   0 .. 255  
  char*     PChar   (32-bit)   Pointer to a null-terminated string  
  short int     Smallint   16-bit   -32768 .. 32767  
  unsigned short int     Word   16-bit   0 .. 65535  
  int     Integer   (16-bit or) 32-bit     -2147483648..2147483647     Generic integer types  
  unsigned int     Cardinal   (16-bit or) 32-bit     0 .. 4294967295     Generic integer types  
  long int     Longint   32-bit   -2147483648..2147483647    
  unsigned long int     Longword   32-bit   0 .. 4294967295  
  float     Single   32-bit   1.5E-45 .. 3.4E+38  
  double     Double   64-bit   5.0E-324 .. 1.7E+308  


C type Pascal Notes
  struct { }     Record End    
  union { }     Record Case Of End     Variant Record  


C Pascal Unit
  abs     Abs     System  
  acos     ArcCos     Math  
  asin     ArcSin     Math  
  atan     ArcTan     System  
  atof     StrToFloat     SysUtils  
  atoi     StrToInt     SysUtils  
  atol     StrToInt     SysUtils  
  atoll     StrToInt64     SysUtils  
  ceil     Ceil     Math  
  cos     Cos     System  
  exp     Exp     System  
  floor     Floor     Math  
  pow     Power     Math  
  round     Round     System  
  sin     Sin     System  
  sqrt     Sqrt     System  
  strcpy     Copy     System  
  strlen     Length     System  
  tan     Tan     Math  
  toupper     UpCase     System  

C++

C++ type Pascal Notes
  class { }     Class End    
  class: { }     Class( ) End    
  template <class T> class { }     Generic = Class<T> End    
  struct { }     Object End     If you want methods

See also