Pascal có tốt hơn C không?

Có thể bạn nghĩ, tại sao tôi nên chọn Pascal thay vì C chẳng hạn. Hoặc có thể bạn đang so sánh Free Pascal với một trình biên dịch Pascal khác. Chà, trong trường hợp đó, hãy nhớ đọc trang này trước khi đưa ra quyết định của bạn. Chà, vì cái gì mà Free Pascal lại tốt như vậy?

  • Ngôn ngữ rất rõ ràng Pascal là một ngôn ngữ rất hay, các chương trình của bạn sẽ dễ đọc và dễ bảo trì hơn so với ngôn ngữ C chẳng hạn, và thậm chí hãy quên C++ đi. Và bạn không cần phải từ bỏ sức mạnh, ngôn ngữ Pascal mạnh mẽ như bạn muốn
  • Không có Makefiles Không giống như hầu hết các ngôn ngữ lập trình, Pascal không cần Makefiles. Bạn có thể tiết kiệm một lượng lớn thời gian, trình biên dịch chỉ tự tìm ra tệp nào cần được biên dịch lại
  • Trình biên dịch Pascal nhanh với F lớn và Free Pascal cũng không ngoại lệ. Có, bạn không còn cần phải phát triển gốc trong khi biên dịch chương trình của mình, chỉ cần nhấn phím biên dịch và thế là xong, ngay cả đối với các chương trình lớn
  • Mỗi đơn vị có mã định danh riêng Trong Pascal, bạn không bao giờ phải lo lắng về việc làm ô nhiễm không gian tên, giống như trong C, nơi mã định danh cần phải là duy nhất trên toàn bộ chương trình. Không, trong Pascal, mỗi đơn vị có không gian tên riêng và điều đó rất thoải mái
  • Tốc độ cao, sử dụng bộ nhớ thấp Là ngôn ngữ được biên dịch thành mã máy nhanh bằng trình biên dịch hiện đại, Free Pascal đã có thể biến Pascal trở thành một trong những ngôn ngữ nhanh nhất hiện có. Hơn nữa, các chương trình Free Pascal có xu hướng sử dụng ít bộ nhớ. Để so sánh với các ngôn ngữ khác, chúng tôi đề xuất điểm chuẩn Shootout và khuyên bạn nên sửa đổi trọng số theo sở thích của riêng mình
  • Môi trường phát triển tích hợp Free Pascal đi kèm với một IDE hoạt động trên nhiều nền tảng, trong đó bạn có thể viết, biên dịch và gỡ lỗi chương trình của mình. Bạn sẽ tiết kiệm được rất nhiều thời gian khi sử dụng IDE, người bạn lập trình tốt nhất mà bạn có
  • Tích hợp tuyệt vời với trình biên dịch chương trình Bạn có nghĩ pascal dành cho những kẻ yếu đuối cần học lập trình không? . Thật tuyệt vời cho lập trình công nghệ cao và cho những người mê sách nhất trong số các bạn, chúng tôi có các trình biên dịch tích hợp. Bạn có thể dễ dàng kết hợp mã hợp ngữ và mã Pascal, bằng ngôn ngữ bạn muốn? . Bạn có muốn chuyển đổi chương trình của mình thành tệp nguồn cho Nasm không?
  • Lập trình hướng đối tượng Và nếu bạn lập trình nghiêm túc, tất nhiên bạn rất quan tâm đến lập trình hướng đối tượng. Sử dụng các cách OOP của Turbo Pascal và Object Pascal theo sở thích của bạn. FCL và Free Vision và cung cấp cho bạn các thư viện đối tượng mạnh mẽ mà bạn cần. Đối với nhu cầu cơ sở dữ liệu của bạn, chúng tôi hỗ trợ PostgreSQL, MySQL, Interbase và ODBC
  • Smartlinking Free Trình liên kết thông minh của Pascal loại bỏ bất kỳ biến hoặc mã nào mà bạn không sử dụng. Điều đó làm cho các chương trình nhỏ trở nên nhỏ với chữ S lớn, trong khi chúng vẫn được liên kết tĩnh, tránh địa ngục DLL
  • Độc lập phân phối [Linux] Do đó, phần mềm được biên soạn bởi phiên bản Linux của Free Pascal sẽ chạy trên bất kỳ bản phân phối Linux nào, giúp phần mềm của bạn hỗ trợ nhiều bản phân phối Linux trở nên dễ dàng hơn rất nhiều
  • Có sẵn cho nhiều nền tảng trên một số kiến ​​trúc Free Pascal có sẵn cho nhiều nền tảng hơn hầu hết các trình biên dịch Pascal khác và cho phép biên dịch chéo dễ dàng, chỉ cần thay đổi mục tiêu trong IDE và biên dịch. Và có nhiều công việc đang diễn ra cho nhiều nền tảng và bộ xử lý hơn nữa
  • Tương thích Có mã hiện có? . Chúng tôi gần như hoàn toàn tương thích với Turbo Pascal và tương thích khá tốt với mã nguồn Delphi. Nếu bạn có mã bằng ngôn ngữ khác, chẳng hạn như C hoặc trình biên dịch mã chương trình, chỉ cần sử dụng trình biên dịch yêu thích cho mã đó và gọi mã từ Free Pascal


                A Comparison of the Syntax of C/C++ and Pascal
                - ---------- -- --- ------ -- ----- --- ------




                             *** VERSION 1.6 ***


            copyright © 1998, 1997, 1996, 1994, 1993, 1992, 1989

                               Russell C. Bjork

                        Professor of Computer Science

                          Gordon College, Wenham, MA

    Reproduction for non-commercial educational use is permitted; any other use
    requires permission of the author.


                            PRELIMINARY NOTE: 
                            ----------------

        The C programming language was originally developed by Dennis Ritchie in
1972.  Two major dialects of C have been available during the history of the 
language: "traditional C" [the original dialect which was distributed for many
years with all Unix systems] and ANSI C [an improved dialect standardized by 
ANSI.]  This document will describe features of both dialects.  

        An ANSI C compiler will accept programs written in traditional C [but 
not vice versa].  There still are a few C compilers that accept only traditional
C, and older books often use this dialect for example programs, so it is 
important to be aware of it.  However, since the compiler can catch certain 
common programming errors in an ANSI C program that would not be caught in a
tradtional C program, a person newly learning C is well-advised to learn the 
ANSI dialect.

        C++ is a newer language built on the base of C by Bjarne Stroustrup in 
the early 1980's. It includes several improvements to C, plus many new features 
designed to support object-oriented programming, while still retaining the basic
features of ANSI C.  As a result, a properly-written ANSI C program will be 
accepted by a C++ compiler.  [However, many traditional C programs will not be 
accepted.  Given the growing popularity of C++, this is another good reason for 
learning ANSI C rather than traditional C.]  

        C++ and its supporting libraries are currently undergoing 
standardization by ANSI/ISO, with publication of a final standard expected in
late 1998.  Currently available C++ compilers and libraries do not yet support
all of the features of this standard.

        For the most part, this handout will only describe features that are
common to both ANSI C and C++.  A few C++ features that make programming easier
or make for better programming style are also included - these are labelled
C++ only and must not be used in programs that are to be compiled by a C
compiler.  [In each case, there is an alternate C construct that serves the same
purpose.]  Finally, a few C++ features are described as new ANSI standard C++.
These may or may not be available yet in a particular C++ implementation.


Pascal                                  C/C++
------                                  -----

                        Lexical Conventions
                        ------- -----------

Not case-sensitive; upper and lower     Case-sensitive; upper and lower case
case letters are equivalent - so        letters are different - so

somename, SOMENAME, Somename, SomeName  somename, SOMENAME, Somename, SomeName  
etc. are all the same.                  are all different

--------------------------------------------------------------------------------

                            Comments
                            --------

[* This is a comment *]                 /* This is a comment */

                                        C++ only

                                        // This is a also comment - in C++ only
                                        // A comment specified this way extends
                                        // only to the end of the current line

--------------------------------------------------------------------------------

                            Constants
                            ---------

The rules for numeric constants [integer, real] are essentially the same, 
though C is a bit more flexible.  [See a C reference manual for details.]

'c'                                     'c'

'This is a string'                      "This is a string"

                                        NOTES:

                                        1. Non-printing characters may be
                                           embedded in either character or 
                                           string constants by using various
                                           escapes - e.g.

                                           \0   - null character
                                           \n   - newline
                                           \t   - tab
                                           \b   - backspace
                                           \f   - formfeed
                                           \\   - \
                                           \'   - '
                                           \"   - "
                                           \ddd - Character whose OCTAL code is
                                                  ddd - e.g. '\101' is the same 
                                                  as 'A'.

2. String constants are terminated by a null character \0. Thus, the total space allocated for a string is one more than the number of characters in it. 3. String constants may be continued over more than one line by having a \ be the very last character on the line to be continued - e.g. "This is a string constant that ex\ tends over more than one line." C++ and some ANSI C compilers only 4. A string constant may be continued over more than one line by simply closing the quotes on one line and re-opening them on the next - e.g. "This is a string constant that ex" "tends over more than one line." -------------------------------------------------------------------------------- const #define size 100 size = 100; #define pi 3.14159 pi = 3.14159; #define first 'A' first = 'A'; #define filename "XYZ.DAT" filename = 'XYZ.DAT'; ANSI C and C++ only const int size = 100; const float pi = 3.14159; const char first = 'A'; const char filename[] = "XYZ.DAT"; NOTE: In C [but not C++], an integer constant declared this way may not be used to specify the size of an array in most cases. -------------------------------------------------------------------------------- Declarations of variables ------------ -- --------- var int i; i: integer; float r; r: real; int b; b: boolean; char c; c: char; int j, k, l; j, k, l: integer; New ANSI Standard C++ bool b; NOTES: 1. C does not use a word like var to introduce variable declarations - it just declares them. 2. C does not have a separate type for boolean values. Int is used, with 0 = false and 1 = true. The new ANSI C++ standard calls for a built in type bool with values false and true, but not all C++ compilers implement it yet. 3. In addition to the scalar types shown above, C has: short int [may be abbreviated short] long int [may be abbreviated long] double [double-precision real] 4. Any integer type [including char] may be prefixed by unsigned - e.g. unsigned int i; unsigned char c; -------------------------------------------------------------------------------- var int a[10]; a: array[0..9] of integer; float b[5][10]; b: array[0..4, 0..9] of real; NOTE: Arrays in C always have subscripts ranging from 0 to declared size - 1. c: array[1..10] of integer; NO DIRECT EQUIVALENT IN C. WORK AROUND THIS BY DECLARING: int c[10]; AND LETTING SUBSCRIPTS RANGE FROM 0..9. NOTE: C does not distinguish between packed and unpacked arrays -------------------------------------------------------------------------------- var struct student: record { int id; id: integer; char name[11]; name: packed array[1..10] float gpa; of char; } student; gpa: real end; New ANSI Standard C++ In this and subsequent similar examples, the declaration of char name[11] can be replaced by: string name; The standard header must be #included to allow this. -------------------------------------------------------------------------------- For this example, suppose the types employee and student have been previously declared: var union borrower: record { employee EBorr; case boolean of student SBorr; false: [EBorr: employee]; } borrower; true: [SBorr: student] end; NOTE: There is no provision for an explicit tag field in C unions, analogous to the tag field of a Pascal variant record. var NO DIRECT EQUIVALENT IN C. THIS CAN borrower: record BE HANDLED BY CREATING A STRUCT WHICH case IsStudent: boolean of CONTAINS THE TAG AND A UNION AS ITS false: [EBorr: employee]; FIELDS. [SEE BELOW] true: [SBorr: student] end; var struct borrower: record { id: integer; int id; name: packed array[1..10] char name[11]; of char; union case boolean of { employee EBorr; false: [EBorr: employee]; student SBorr; true: [SBorr: student] } Borr; end; } borrower; We can access the variants as We can access the variants as borrower.EBorr, borrower.SBorr borrower.Borr.EBorr, borrower.Borr.SBorr C++ only: The field name Borr for the union can be omitted; the variants can then be referenced as in Pascal. -------------------------------------------------------------------------------- var enum {chocolate, vanilla} flavor; flavor: [chocolate, vanilla]; NOTE: As in Pascal, it is usually better style with record and enumeration types [and often with array types] to first declare a named type and then a variable of that type. See below. -------------------------------------------------------------------------------- var squares: set of 1..9; NO DIRECT EQUIVALENT IN C. SOMEWHAT OF THE SAME EFFECT CAN SOMETIMES BE BE OBTAINED BY 1. Treating a longword as a bit vector representing of basetype size up to 32. Bitwise operations can be used to get the effect of set operations, e.g. int squares; ... squares := []; squares = 0; .. ... squares := squares + [2] squares |= 1 link; .. ... dispose[first]; free[first]; C++ only: first = new node; ... delete first; -------------------------------------------------------------------------------- NO PASCAL EQUIVALENT In C, the type array is equivalent to a pointer type to the element type of the array. Thus, the following pairs of operations are equivalent; either syntax can be used regardless of whether a is declared as an array or a pointer: a[0] = 1; IS EQUIVALENT TO *a = 1; a[3] = 1; IS EQUIVALENT TO *[a+3]= 1; Note that arithmetic on pointers is done in units of the size of the basetype. Thus, if ints occupy 4 bytes, then *[a+3] actually adds 12 to to calculate the desired address. Also, for formal parameters only, int a[]; IS EQUIVALENT TO int *a; As a consequence of this, the following code segments are NOT equivalent var a, b: array[0..9] of real; float a[10], b[10]; .. ... b := a; b = a; [In fact, this would not even be syntactically legal unless b were a formal parameter of a procedure.] C allows assignment of entire structures, but NOT arrays. The equivalent C code for the Pascal array assignment is for [i=0; i < 10; i++] b[i] = a[i]; -------------------------------------------------------------------------------- Functions and Procedures --------- --- ---------- function f[x, y: integer; TRADITIONAL C: z: real]: real; float f[x, y, z] int x, y; float z; var { q: integer; int q; begin q = x*x + y; q := sqr[x] + y; return q - z; f := q - z } end; ANSI C/C++: float f[int x, int y, float z] { int q; q = x*x + y; return q - z; } NOTE WELL THE DIFFERENCES IN USAGE OF SEMICOLONS BETWEEN PASCAL AND C -------------------------------------------------------------------------------- procedure p[x: integer]; TRADITIONAL C: void p[x] int x; var { temp: integer; int temp; begin ... .. return; end; } ANSI C/C++: void p[int x] { int temp; ... return; } NOTE: In either dialect, the return statement is optional at the end of the code. A return statement or statements may also appear in the middle of the code. -------------------------------------------------------------------------------- Given: Given: function f: integer; int f[] .. ... procedure p; void p[] .. ... These routines are called by: These routines are called by: x := f; x = f[]; p; p[]; -------------------------------------------------------------------------------- procedure p[var x: integer]; TRADITIONAL C: void p[x] int *x; begin { x := 17 *x = 17; end; } ANSI C/C++: void p[int *x] { *x = 17; } This routine is called by: In either dialect, this routine is called by: p[i]; p[&i]; [where i is an integer variable] [where i is an int variable] C++ only: void p[int &x] { x = 17; } Declared this way, this routine is called by: p[i]; -------------------------------------------------------------------------------- type TRADITIONAL C realarray = array[0..9] of real; procedure p[var a: realarray]; void p[a] float a[]; begin { a[1] := a[2] + a[3] a[1] = a[2] + a[3]; end; } OR, BECAUSE OF THE EQUIVALENCE OF ARRAYS AND POINTERS: void p[a] float *a; { a[1] = a[2] + a[3]; } ANSI C: void p[float a[]] { a[1] = a[2] + a[3]; } OR void p[float *a] { a[1] = a[2] + a[3]; } -------------------------------------------------------------------------------- function f[c: char]: integer; forward; TRADITIONAL C: int f[]; ANSI C/C++: int f[char c]; OR int f[char]; NOTE: Strictly speaking, no C dialect absolutely requires you to declare a function before it is used. If a call to a previously undeclared function is seen, the compiler assumes it is a function returning int and, in the case of ANSI C/C++, makes assumptions about the types of its formal parameters based on the types of the actual parameters appearing in the call. This serves as an implicit function declaration. However, it is always good practice to declare a function before it is used - and this is mandatory if the return type is other than int. Further, the C++ compiler [and some ANSI C compilers] issue a warning about 'implicit declaration of function' if you call a previously undeclared function. -------------------------------------------------------------------------------- function f[c: char]: integer; external; TRADITIONAL C: int f[]; ANSI C: int f[char c]; OR int f[char]; -------------------------------------------------------------------------------- program ... main[] { .. ... } begin [* main program *] OR [TRADITIONAL C] ... main[argc, argv] end. int argc; char *argv[]; { ... } OR [ANSI C] main[int argc, char * argv[]] { ... } -------------------------------------------------------------------------------- Operators and Expressions --------- --- ----------- C operators are grouped in decreasing order of precedence. All operators in the same group have the same precedence. L and R indicate left and right associativity, respectively. The names used in examples stand for variables or expressions of a certain type, as follows: x, y - no particular type i, j - integers b, c - boolean p - pointer a - array or pointer s - struct f - function t - type name s.name s.name p^.name p -> name a[i] a[i] f or f[arguments ...] f[] or f[arguments ...] NO PASCAL EQUIVALENT [post-increment] x++ NO PASCAL EQUIVALENT [post-decrement] x-- -------------------------------------------------------------------------------- NO PASCAL EQUIVALENT [pre-increment] ++x NO PASCAL EQUIVALENT [pre-decrement] --x p^ *p NO PASCAL EQUIVALENT [address of] &x +x [ANSI C/C++ only] +x -x -x not x !x NO PASCAL EQUIVALENT [bitwise not] ~i NO PASCAL EQUIVALENT [type cast] [t] x NO PASCAL EQUIVALENT [size in bytes] sizeof x NO PASCAL EQUIVALENT [size in bytes] sizeof [t] -------------------------------------------------------------------------------- x * y x * y L x / y x / y L i div j i / j L i mod j i % j L -------------------------------------------------------------------------------- x + y x + y L x - y x - y L -------------------------------------------------------------------------------- NO PASCAL EQUIVALENT [left shift] i > j L -------------------------------------------------------------------------------- x < y x < y L x > y x > y L x = y L -------------------------------------------------------------------------------- x = y x == y L x y x != y L -------------------------------------------------------------------------------- NO PASCAL EQUIVALENT [bitwise and] i & j L -------------------------------------------------------------------------------- NO PASCAL EQUIVALENT [bitwise xor] i ^ j L -------------------------------------------------------------------------------- NO PASCAL EQUIVALENT [bitwise or] i | j L -------------------------------------------------------------------------------- b and c b && c L -------------------------------------------------------------------------------- b or c b || c L -------------------------------------------------------------------------------- NO PASCAL EQUIVALENT [conditional expr] b ? x : y R -------------------------------------------------------------------------------- x := y x = y R x := x + y x += y R x := x - y x -= y R x := x * y x *= y R i := i div j i /= j R x := x / y x /= y R i := i mod j i %= j R NO PASCAL EQUIVALENT [see above] i = j R NO PASCAL EQUIVALENT " " i &= j R NO PASCAL EQUIVALENT " " i ^= j R NO PASCAL EQUIVALENT " " i |= j R -------------------------------------------------------------------------------- NO PASCAL EQUIVALENT [sequential eval] x, y L -------------------------------------------------------------------------------- The following operators are found only in C++ NO GENERAL PASCAL EQUIVALENT t[x] [type conversion] new[p] p = new t dispose[p] delete p -------------------------------------------------------------------------------- Executable Statements ---------- ---------- x := y + z x = y + z; NOTE: This is an instance of the C expression statement, which is actually much more flexible than its Pascal equivalent. The following is also legal [but contorted!] if [w += x = y++ * z] u = ++w; This is equivalent to: x = y * z; y = y + 1; w = w + x; if [w != 0] { w = w + 1; u = w; } -------------------------------------------------------------------------------- begin { x := y + z; x = y + z; w := x w = x; end } NOTES: 1. There is never a semicolon after the terminating } , but the last statement inside the compound statement does end with a semicolon. 2. The C compound statement may begin with declarations for local variables - e.g. NO PASCAL EQUIVALENT { int temp; temp = y; y = z; z = temp; } -------------------------------------------------------------------------------- if x < 0 then if [x < 0] x := -x x = -x; if x > y then if [x > y] max := x max = x; else else max := y max = y; NOTE: In C, a semicolon is a statement terminator, not a statement separator - so it MUST be used before else in a case like this. -------------------------------------------------------------------------------- while x < y do while [x < y] x := 2 * x x = 2 * x; -------------------------------------------------------------------------------- repeat do x := 2 * x; { y := y - 1 x = 2 * x; until x >= y y--; } while [x < y]; NOTE: The sense of the condition is the opposite from Pascal. C is consistent about always requiring compound statements to be surrounded by braces. -------------------------------------------------------------------------------- for i := 1 to n do for [i = 1 ; i > i; cin >> r; cin >> s; while [cin.get[] != '\n'] ; cin >> c, i, r; while [cin.get[] != '\n'] ; cout

Chủ Đề