Difference between revisions of "Pascal for C users"
From Lazarus wiki
Jump to navigationJump to search(31 intermediate revisions by 14 users not shown) | |||
Line 1: | Line 1: | ||
− | + | {{Pascal for C users}} | |
+ | == Overview == | ||
+ | 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 32: | Line 35: | ||
| ! | | ! | ||
| [[Not]] | | [[Not]] | ||
− | | Logical | + | | Logical not |
|- | |- | ||
| != | | != | ||
Line 40: | Line 43: | ||
| && | | && | ||
| [[And]] | | [[And]] | ||
− | | | + | | Logical and |
|- | |- | ||
| <nowiki> || </nowiki> | | <nowiki> || </nowiki> | ||
| [[Or]] | | [[Or]] | ||
− | | | + | | Logical or |
+ | |- | ||
+ | | & | ||
+ | | [[And]] | ||
+ | | Bitwise and | ||
+ | |- | ||
+ | | <nowiki> | </nowiki> | ||
+ | | [[Or]] | ||
+ | | Bitwise or | ||
|- | |- | ||
| ^ | | ^ | ||
| [[Xor]] | | [[Xor]] | ||
− | | | + | | Exclusive or |
+ | |- | ||
+ | | ~ | ||
+ | | [[Not]] | ||
+ | | One's complement | ||
|- | |- | ||
| >> | | >> | ||
| [[Shr]] | | [[Shr]] | ||
− | | bit '''sh'''ift '''r'''ight | + | | bit '''sh'''ift '''r'''ight 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. |
|- | |- | ||
| << | | << | ||
Line 77: | Line 92: | ||
| // | | // | ||
| End of line comment (only one line comment) | | End of line comment (only one line comment) | ||
+ | |- | ||
+ | | 0x | ||
+ | | [[Dollar_sign|$]] | ||
+ | | prefix for hex-number e.g. $FFFFFF | ||
+ | |- | ||
+ | | 0 | ||
+ | | [[&|&]] | ||
+ | | prefix for oct-number e.g. &77777777 | ||
+ | |- | ||
+ | | 0b | ||
+ | | [[Percent_sign|%]] | ||
+ | | prefix for bin-number e.g. %11111111 | ||
+ | |- | ||
+ | | & | ||
+ | | [[@]] | ||
+ | | [[@|address operator]] | ||
+ | |- | ||
+ | | * | ||
+ | | [[^]] | ||
+ | | See [[Pointer]] and [[Pointers]] | ||
|- | |- | ||
| if() | | if() | ||
Line 113: | Line 148: | ||
| switch case break default | | switch case break default | ||
| [[Case]] [[Of]] [[Else]] [[End]] | | [[Case]] [[Of]] [[Else]] [[End]] | ||
+ | | | ||
+ | |- | ||
+ | | const a_c_struct *arg | ||
+ | | [[Constref]] arg : a_c_struct | ||
+ | | | ||
+ | |- | ||
+ | | a_c_struct *arg | ||
+ | | [[Var]] arg : a_c_struct | ||
| | | | ||
|- | |- | ||
Line 118: | Line 161: | ||
− | {| | + | {| class="wikitable" |
− | ! | + | ! C type !! [[Pascal]] [[Data_type|type]] !! Size (bits) !! Range !! Notes |
− | ! | ||
− | ! | ||
− | ! | ||
− | ! | ||
|- | |- | ||
| char | | char | ||
Line 150: | Line 189: | ||
|- | |- | ||
| short int | | short int | ||
− | | [[ | + | | [[Smallint]] |
| 16-bit | | 16-bit | ||
| -32768 .. 32767 | | -32768 .. 32767 | ||
Line 174: | Line 213: | ||
|- | |- | ||
| long int | | long int | ||
− | | [[ | + | | [[Longint]] |
| 32-bit | | 32-bit | ||
| -2147483648..2147483647 | | -2147483648..2147483647 | ||
Line 180: | Line 219: | ||
|- | |- | ||
| unsigned long int | | unsigned long int | ||
− | | [[ | + | | [[Longword]] |
| 32-bit | | 32-bit | ||
| 0 .. 4294967295 | | 0 .. 4294967295 | ||
Line 195: | Line 234: | ||
| 64-bit | | 64-bit | ||
| 5.0E-324 .. 1.7E+308 | | 5.0E-324 .. 1.7E+308 | ||
− | | | + | |- |
+ | | unsigned long long int | ||
+ | | [[uInt64]] | ||
+ | | 64-bit | ||
+ | | 0 .. 18446744073709551616 | ||
+ | | | ||
+ | |- | ||
+ | |} | ||
+ | |||
+ | |||
+ | {| class="wikitable" | ||
+ | ! C type !! [[Pascal]] !! Notes | ||
+ | |- | ||
+ | | struct { } | ||
+ | | [[Record]] [[End]] | ||
+ | | | ||
+ | |- | ||
+ | | union { } | ||
+ | | [[Record]] [[Case]] [[Of]] [[End]] | ||
+ | | [[Case#Variant Record|Variant Record]] | ||
+ | |- | ||
+ | |} | ||
+ | |||
+ | |||
+ | {| class="wikitable" | ||
+ | ! C !! [[Pascal]] !! [[Unit]] | ||
+ | |- | ||
+ | | abs | ||
+ | | Abs | ||
+ | | [[System unit|System]] | ||
+ | |- | ||
+ | | acos | ||
+ | | ArcCos | ||
+ | | [[Math unit|Math]] | ||
+ | |- | ||
+ | | asin | ||
+ | | ArcSin | ||
+ | | [[Math unit|Math]] | ||
+ | |- | ||
+ | | atan | ||
+ | | ArcTan | ||
+ | | [[System unit|System]] | ||
+ | |- | ||
+ | | atof | ||
+ | | StrToFloat | ||
+ | | [[sysutils|SysUtils]] | ||
+ | |- | ||
+ | | atoi | ||
+ | | StrToInt | ||
+ | | [[sysutils|SysUtils]] | ||
+ | |- | ||
+ | | atol | ||
+ | | StrToInt | ||
+ | | [[sysutils|SysUtils]] | ||
+ | |- | ||
+ | | atoll | ||
+ | | StrToInt64 | ||
+ | | [[sysutils|SysUtils]] | ||
+ | |- | ||
+ | | ceil | ||
+ | | Ceil | ||
+ | | [[Math unit|Math]] | ||
+ | |- | ||
+ | | cos | ||
+ | | Cos | ||
+ | | [[System unit|System]] | ||
+ | |- | ||
+ | | exp | ||
+ | | Exp | ||
+ | | [[System unit|System]] | ||
+ | |- | ||
+ | | floor | ||
+ | | Floor | ||
+ | | [[Math unit|Math]] | ||
+ | |- | ||
+ | | pow | ||
+ | | Power | ||
+ | | [[Math unit|Math]] | ||
+ | |- | ||
+ | | round | ||
+ | | Round | ||
+ | | [[System unit|System]] | ||
+ | |- | ||
+ | | sin | ||
+ | | Sin | ||
+ | | [[System unit|System]] | ||
+ | |- | ||
+ | | sqrt | ||
+ | | Sqrt | ||
+ | | [[System unit|System]] | ||
+ | |- | ||
+ | | strcpy | ||
+ | | Copy | ||
+ | | [[System unit|System]] | ||
+ | |- | ||
+ | | strlen | ||
+ | | Length | ||
+ | | [[System unit|System]] | ||
+ | |- | ||
+ | | tan | ||
+ | | Tan | ||
+ | | [[Math unit|Math]] | ||
+ | |- | ||
+ | | toupper | ||
+ | | UpCase | ||
+ | | [[System unit|System]] | ||
+ | |- | ||
+ | |} | ||
+ | === C++ === | ||
+ | {| class="wikitable" | ||
+ | ! 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 == | ||
+ | |||
+ | * [[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 17:57, 3 December 2022
│
Deutsch (de) │
English (en) │
français (fr) │
Overview
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.
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 | |
unsigned long long int | uInt64 | 64-bit | 0 .. 18446744073709551616 |
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
- C to Pascal - conversion tools and libraries
- 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
- “Comparison of Pascal and C” on the English Wikipedia