Pascal for C users: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: Pascal for C users
| Old | New | Line | |
|---|---|---|---|
| 1 | + | ## Overview | |
| 2 | + | ||
| 3 | + | This page gives some translations between C(++) and Pascal concepts. | |
| 4 | + | ||
| 5 | + | ## Translation | |
| 6 | + | ||
| 7 | + | > **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. | |
| 8 | + | ||
| 9 | + | | C | [[Pascal]] | Notes | | |
| 10 | + | |----|----|----| | |
| 11 | + | | { | [[Begin]] | | | |
| 12 | + | | } | [[End]] | | | |
| 13 | + | | = | := | [[Becomes]] | | |
| 14 | + | | == | = | [[Equal]] | | |
| 15 | + | | / | / | [[Slash|Float Division]] (Integer: [[Div|div]]) | | |
| 16 | + | | % | [[Mod]] | **Mod**ulo operation | | |
| 17 | + | | ! | [[Not]] | Logical not | | |
| 18 | + | | != | \<\> | [[Not equal]] | | |
| 19 | + | | && | [[And]] | Logical and | | |
| 20 | + | | \<nowiki\> | | [[Or]] | | |
| 21 | + | | & | [[And]] | Bitwise and | | |
| 22 | + | | \| | [[Or]] | Bitwise or | | |
| 23 | + | | ^ | [[Xor]] | Exclusive or | | |
| 24 | + | | ~ | [[Not]] | One's complement | | |
| 25 | + | | \>\> | [[Shr]] | 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. | | |
| 26 | + | | \<\< | [[Shl]] | bit **sh**ift **l**eft | | |
| 27 | + | | ++ | [[Inc]] | | | |
| 28 | + | | -- | [[Dec]] | | | |
| 29 | + | | /* | { or (* | [[Comments|Comment]] start | | |
| 30 | + | | */ | } or *) | Comment end | | |
| 31 | + | | // | // | End of line comment (only one line comment) | | |
| 32 | + | | 0x | [[Dollar sign|$]] | prefix for hex-number e.g. \$FFFFFF | | |
| 33 | + | | 0 | [[&]] | prefix for oct-number e.g. &77777777 | | |
| 34 | + | | 0b | [[Percent sign|%]] | prefix for bin-number e.g. %11111111 | | |
| 35 | + | | & | [[@]] | [[@|address operator]] | | |
| 36 | + | | * | [[^]] | See [[Pointer]] and [[Pointers]] | | |
| 37 | + | | if() | [[If]] [[Then]] | | | |
| 38 | + | | if() else | [[If]] [[Then]] [[Else]] | | | |
| 39 | + | | while | [[While]] [[Do]] | | | |
| 40 | + | | do while | [[Repeat]] [[Until]] [[Not]] | | | |
| 41 | + | | | | | | |
| 42 | + | | do while ! | [[Repeat]] [[Until]] | | | |
| 43 | + | | for ++ | [[For]] [[To]] [[Do]] | | | |
| 44 | + | | for -- | [[For]] [[Downto]] [[Do]] | | | |
| 45 | + | | switch case break | [[Case]] [[Of]] [[End]] | | | |
| 46 | + | | switch case break default | [[Case]] [[Of]] [[Else]] [[End]] | | | |
| 47 | + | | const a_c_struct *arg | [[Constref]] arg : a_c_struct | | | |
| 48 | + | | a_c_struct *arg | [[Var]] arg : a_c_struct | | | |
| 49 | + | | | | | | |
| 50 | + | ||
| 51 | + | | C type | [[Pascal]] [[Data type|type]] | Size (bits) | Range | Notes | | |
| 52 | + | |----|----|----|----|----| | |
| 53 | + | | char | [[Char]] | 8-bit | | [[ASCII]] | | |
| 54 | + | | signed char | [[Shortint]] | 8-bit | -128 .. 127 | | | |
| 55 | + | | unsigned char | [[Byte]] | 8-bit | 0 .. 255 | | | |
| 56 | + | | char* | [[PChar]] | (32-bit) | | Pointer to a null-terminated string | | |
| 57 | + | | short int | [[Smallint]] | 16-bit | -32768 .. 32767 | | | |
| 58 | + | | unsigned short int | [[Word]] | 16-bit | 0 .. 65535 | | | |
| 59 | + | | int | [[Integer]] | (16-bit or) 32-bit | -2147483648..2147483647 | Generic integer types | | |
| 60 | + | | unsigned int | [[Cardinal]] | (16-bit or) 32-bit | 0 .. 4294967295 | Generic integer types | | |
| 61 | + | | long int | [[Longint]] | 32-bit | -2147483648..2147483647 | | | |
| 62 | + | | unsigned long int | [[Longword]] | 32-bit | 0 .. 4294967295 | | | |
| 63 | + | | float | [[Single]] | 32-bit | 1.5E-45 .. 3.4E+38 | | | |
| 64 | + | | double | [[Double]] | 64-bit | 5.0E-324 .. 1.7E+308 | | | |
| 65 | + | | unsigned long long int | [[uInt64]] | 64-bit | 0 .. 18446744073709551616 | | | |
| 66 | + | | | | | | | | |
| 67 | + | ||
| 68 | + | | C type | [[Pascal]] | Notes | | |
| 69 | + | |----|----|----| | |
| 70 | + | | struct { } | [[Record]] [[End]] | | | |
| 71 | + | | union { } | [[Record]] [[Case]] [[Of]] [[End]] | [[Case|Variant Record]] | | |
| 72 | + | | | | | | |
| 73 | + | ||
| 74 | + | | C | [[Pascal]] | [[Unit]] | | |
| 75 | + | |----|----|----| | |
| 76 | + | | abs | Abs | [[System unit|System]] | | |
| 77 | + | | acos | ArcCos | [[Math unit|Math]] | | |
| 78 | + | | asin | ArcSin | [[Math unit|Math]] | | |
| 79 | + | | atan | ArcTan | [[System unit|System]] | | |
| 80 | + | | atof | StrToFloat | [[sysutils|SysUtils]] | | |
| 81 | + | | atoi | StrToInt | [[sysutils|SysUtils]] | | |
| 82 | + | | atol | StrToInt | [[sysutils|SysUtils]] | | |
| 83 | + | | atoll | StrToInt64 | [[sysutils|SysUtils]] | | |
| 84 | + | | ceil | Ceil | [[Math unit|Math]] | | |
| 85 | + | | cos | Cos | [[System unit|System]] | | |
| 86 | + | | exp | Exp | [[System unit|System]] | | |
| 87 | + | | floor | Floor | [[Math unit|Math]] | | |
| 88 | + | | pow | Power | [[Math unit|Math]] | | |
| 89 | + | | round | Round | [[System unit|System]] | | |
| 90 | + | | sin | Sin | [[System unit|System]] | | |
| 91 | + | | sqrt | Sqrt | [[System unit|System]] | | |
| 92 | + | | strcpy | Copy | [[System unit|System]] | | |
| 93 | + | | strlen | Length | [[System unit|System]] | | |
| 94 | + | | tan | Tan | [[Math unit|Math]] | | |
| 95 | + | | toupper | UpCase | [[System unit|System]] | | |
| 96 | + | | | | | | |
| 97 | + | ||
| 98 | + | ### C++ | |
| 99 | + | ||
| 100 | + | | C++ type | [[Pascal]] | Notes | | |
| 101 | + | |----|----|----| | |
| 102 | + | | class { } | [[Class]] [[End]] | | | |
| 103 | + | | class: { } | [[Class]]( ) [[End]] | | | |
| 104 | + | | template \<class T\> class { } | [[Generic]] = [[Class]]\<T\> [[End]] | | | |
| 105 | + | | struct { } | [[Object]] [[End]] | If you want methods | | |
| 106 | + | | | | | | |
| 107 | + | ||
| 108 | + | ## See also | |
| 109 | + | ||
| 110 | + | - [[C to Pascal]] - conversion tools and libraries | |
| 111 | + | - [Short tutorial](http://gd.tuwien.ac.at/languages/pascal/fpc/docs-pdf/CinFreePascal.pdf) that describes the process of using C and C++ code in FreePascal, including writing of wrapper code | |
| 112 | + | - [[Creating bindings for C libraries]] | |
| 113 | + | - [[Common problems when converting C header files]] | |
| 114 | + | - [[SWIG]] | |
| 115 | + | - [“Comparison of Pascal and C” on the English Wikipedia](https://en.wikipedia.org/wiki/Comparison_of_Pascal_and_C) | |
| 116 | + | ||
| 117 | + | --- | |
| 118 | + | *From the Free Pascal wiki, page "Pascal for C users", as it stood on 2022-12-03 after a change by Blueicaro, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
