2010-02-16 21 views
8

Có sự khác biệt nào nếu tôi biên dịch chương trình sau bằng c89 và c99? Tôi nhận được cùng một đầu ra. Có thực sự là một sự khác biệt giữa hai?C89 vs c99 Trình biên dịch GCC

#include <stdio.h> 

    int main() 
    { 
     // Print string to screen. 
     printf ("Hello World\n"); 
    } 

gcc -o helloworld -std=c99 helloworld.c 
vs 
gcc -o helloworld -std=c89 helloworld.c 
+5

"gcc -std = c89" và "gcc -std = c99" không tuân thủ đầy đủ các tiêu chuẩn tương ứng. Thêm "-pedantic" hoặc "-pedantic-errors" để có được một cái gì đó gần đầy đủ sự phù hợp. –

Trả lời

6

Về lý thuyết, cần có một sự khác biệt. Sử dụng "//" để hủy nhận xét không phải là một phần của C89, vì vậy nếu nó thực thi đúng quy tắc C89, điều đó sẽ tạo ra lỗi trình biên dịch (với -ansi -pedantic, nó có thể làm điều đó, nhưng tôi không nhớ chắc chắn rồi).

Điều đó mang lại ý tưởng về ký tự chung: nếu chương trình biên dịch thành C89, nó sẽ thường biên dịch thành C99 và cung cấp kết quả giống hệt nhau. C99 chủ yếu mua cho bạn một số tính năng mới không có trong C89, vì vậy bạn có thể sử dụng (ví dụ) các mảng độ dài biến đổi không được phép trong C89.

Bạn có thể phải yêu cầu thực thi quy tắc pedantic để xem tất cả các khác biệt - C99 có ý định chuẩn hóa thực tiễn hiện tại và một số thực tiễn hiện tại là phần mở rộng gcc, một số được bật theo mặc định.

+2

+1, Bắt tốt; '//' bình luận có thể là phần duy nhất của C99 đáng giá. '-ansi -pedantic' không xuất hiện lỗi:' main.c: 5: error: biểu thức mong đợi trước '/' token' –

+1

Tôi không thấy các chú thích '//' hữu ích hơn thế nào, chỉ có hai ít ký tự hơn ... nó giống như nói rằng C nên sử dụng '.' để truy cập vào các thành viên của con trỏ đến các cấu trúc hơn là' -> 'bởi vì nó lưu gõ. –

+0

@JoeD '//' nhận xét chỉ ghi lại một dòng, có nghĩa là chúng ít có khả năng bị vô tình bị bắt bởi một nhóm nhận xét khác hoặc gây ra sự cố. Hơn nữa, lập luận của bạn về '.' là ngớ ngẩn; sử dụng cùng một toán tử cho những gì về cơ bản là hoạt động tương tự có lẽ là một ý tưởng hay. – Alice

1

trên diễn đàn này http://www.velocityreviews.com/forums/t287495-p2-iso-c89-and-iso-c99.html tôi thấy điều này:

Tóm lại: 99 là tiêu chuẩn hóa, có từ khoá mới, công cụ mảng mới, số phức, các chức năng thư viện và như vậy. Các trình biên dịch khác là c89 hoàn chỉnh vì chúng đã có tất cả thời gian này để tạo ra chúng.

A) ANSI X3.159-1989. This is the original 1989 C standard, dated December 1989, with Rationale. The main body of the language is described in section 3, and the "C library" -- stdio, functions, and so on -- in section 4.

B) ISO 9899:1990. This is the original ISO C standard. "ANSI" is the American National Standards Institute, so the international crowd have to have their own standards with their own, different, numbering system. They simply adopted ANSI's 1989 standard, removed the Rationale, and renumbered the sections (calling them "clauses" instead). With very few exceptions you can just add three, so that most of the language is described in section -- er, "clause" -- 6, and the "C library" part in section 7.

C) ISO 9899:1999. This is the newfangled "C99" standard, with its Variable Length Arrays, Flexible Array Members, new keywords like "restrict" and "_Bool", new semantics for the "static" keyword, new syntax to create anonymous aggregates, new complex-number types, hundreds of new library functions, and so on.

The new ISO standard was immediately "back-adopted" by ANSI. I have not seen any official "ANSI-sanctioned" claim about this, but given the usual numbering systems, I would expect this to be ANSI Standard number X3.159-1999. (The numbering system is pretty obvious: a standard, once it comes out, gets a number -- X. for ANSI, or just a number for ISO -- and a suffix indicating year of publication. An update to an existing standard reuses the number, with the new year.)

Although X3.159-1989 and 9899:1990 have different years and section numbering, they are effectively identical, so "C89" and "C90" really refer to the same language. Hence you can say either "C89" or "C90" and mean the same thing, even to those aware of all the subtleties.

There were also several small revisions to the original 1990 ISO standard: "Normative Addendum 1", and two "Technical Corrigenda" (numbered; giving Technical Corrigendum 1 and TC2). The two TCs are considered to be "bug fixes" for glitches in the wording of the standard, while NA1 is an actual "change". In practice, the TCs do not really affect users, while NA1 adds a whole slew of functions that people can use, so NA1 really is more significant. NA1 came out in 1994, so one might refer to "ISO 9899:1990 as modified by NA1" as "C94". I have seen it called "C95", too.

28
  • // ý kiến ​​không phải là một phần của C89 nhưng là OK trong C99,
  • rơi tắt của main() mà không trả lại bất kỳ giá trị tương đương với return 0; trong C99, nhưng không phải như vậy trong C89. Từ N1256 (pdf), 5.1.2.2.3p1:

    If the return type of the main function is a type compatible with int , a return from the initial call to the main function is equivalent to calling the exit function with the value returned by the main function as its argument; reaching the } that terminates the main function returns a value of 0.

hành vi Vì vậy, mã của bạn đã không xác định trong C89, và hành vi được xác định rõ trong C99.