C语言:修订间差异

求闻百科,共笔求闻
添加的内容 删除的内容
(机器人:将英文日期转换为ISO格式)
(机器人:清理不当的来源、移除无用的模板参数)
 
第40行: 第40行:
|language = en
|language = en
|trans_title = TIOBE编程社区指数
|trans_title = TIOBE编程社区指数
}}</ref>。目前,C语言[[編譯器]]普遍存在於各種不同的[[操作系统]]中,例如[[Microsoft Windows]]、[[macOS]]、[[Linux]]、[[UNIX|Unix]]等。C語言的設計影響了众多後來的程式語言,例如[[C++]]、[[Objective-C]]、[[Java]]、[[C♯|C#]]等。
|
|
|
}}</ref>。目前,C语言[[編譯器]]普遍存在於各種不同的[[操作系统]]中,例如[[Microsoft Windows]]、[[macOS]]、[[Linux]]、[[UNIX|Unix]]等。C語言的設計影響了众多後來的程式語言,例如[[C++]]、[[Objective-C]]、[[Java]]、[[C♯|C#]]等。


二十世纪八十年代,為了避免各開發廠商用的C語言的語法產生差異,由[[美國國家標準協會|美國國家標準局]]為C語言訂定了一套完整的國際標準語法,稱為[[ANSI C]],作為C語言的標準。二十世纪八十年代至今的有关程式開發工具,一般都支持符合[[ANSI C]]的語法。
二十世纪八十年代,為了避免各開發廠商用的C語言的語法產生差異,由[[美國國家標準協會|美國國家標準局]]為C語言訂定了一套完整的國際標準語法,稱為[[ANSI C]],作為C語言的標準。二十世纪八十年代至今的有关程式開發工具,一般都支持符合[[ANSI C]]的語法。
第91行: 第88行:


=== 进一步了解 ===
=== 进一步了解 ===
C语言由函数和变量组成,C的函数就像是[[Fortran]]中的子程序和函数<ref name="自动生成1" /><ref name="自动生成2">{{Cite web |url =http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2310.pdf |accessdate =2020-06-10 |title =ISO/IEC 9899:2018 |||}}</ref>。
C语言由函数和变量组成,C的函数就像是[[Fortran]]中的子程序和函数<ref name="自动生成1" /><ref name="自动生成2">{{Cite web |url =http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2310.pdf |accessdate =2020-06-10 |title =ISO/IEC 9899:2018 }}</ref>。


在C语言中,程序从 [[主函式#C/C++|<code>main</code>]] 开始执行。<code>main</code> 函数通过调用和控制其他函数进行工作。例如上面的<code>printf</code>。程序员可以自己写函数,或从库中调用函数。在上面的<code>return 0;</code> 使得 <code>main</code> 返回一个值给调用程序的[[壳层]],表明程序是否成功运行。
在C语言中,程序从 [[主函式#C/C++|<code>main</code>]] 开始执行。<code>main</code> 函数通过调用和控制其他函数进行工作。例如上面的<code>printf</code>。程序员可以自己写函数,或从库中调用函数。在上面的<code>return 0;</code> 使得 <code>main</code> 返回一个值给调用程序的[[壳层]],表明程序是否成功运行。
第264行: 第261行:
1 2 3
1 2 3
4 5 ?
4 5 ?
根据C标准的规定,在存在初始化列表时,如果初始化列表中未提供对所有元素的初始化,则剩余元素会被默认初始化,并使用与静态变量相同的初始化规则<ref>{{Cite web|url=https://www.iso.org/standard/57853.html|title=ISO/IEC 9899:2011|publisher=International Organization for Standardization|accessdate=2020-03-08|language=en|||quote=§6.7.9 Initialization 21 If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.|}}</ref>。
根据C标准的规定,在存在初始化列表时,如果初始化列表中未提供对所有元素的初始化,则剩余元素会被默认初始化,并使用与静态变量相同的初始化规则<ref>{{Cite web|url=https://www.iso.org/standard/57853.html|title=ISO/IEC 9899:2011|publisher=International Organization for Standardization|accessdate=2020-03-08|language=en|||quote=§6.7.9 Initialization 21 If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.}}</ref>。


==== 指针 ====
==== 指针 ====
第422行: 第419行:


== 保留关键字 ==
== 保留关键字 ==
以下是C语言的保留关键字:<ref>{{Cite web|url=https://www.iso.org/standard/57853.html|title=ISO/IEC 9899:2011|publisher=International Organization for Standardization|accessdate=2020-03-08|language=en|||}}</ref>
以下是C语言的保留关键字:<ref>{{Cite web|url=https://www.iso.org/standard/57853.html|title=ISO/IEC 9899:2011|publisher=International Organization for Standardization|accessdate=2020-03-08|language=en}}</ref>


{| class="wikitable" border="1" width="500" cellpadding="0" cellspacing="0"
{| class="wikitable" border="1" width="500" cellpadding="0" cellspacing="0"
第467行: 第464行:


== 經典錯誤 ==
== 經典錯誤 ==
“void main()”的用法并不是任何标准制定的<ref>[http://www.stroustrup.com/bs_faq2.html#void-main Can I write "void main()"?] The definition <code>void main() { /* ... */ }</code> is not and never has been C++, nor has it even been C.</ref><ref>{{Cite web |url=http://www.ibm.com/developerworks/cn/linux/l-c99/index.html |title=用 C99 进行开放源代码的开发 |accessdate=2011-01-21 |||}}</ref>。 C語言标准語法是“int main()”,任何实现都必须支持<code>int main(void) { /* ... */ }</code>和<code>int main(int argc, char* argv[]) { /* ... */ }</code><ref>「The function called at program startup is named '''main'''. The implementation declares no prototype for this function. It shall be defined with a return type of '''int''' and with no parameters: <code>int main(void) { /* ... */ }</code> or with two parameters (referred to here as '''argc''' and '''argv''', though any names may be used, as they are local to the function in which they are declared): <code>int main(int argc, char *argv[]) { /* ... */ }</code> or equivalent; or in some other implementation-defined manner.」,引自ISO/IEC 9899:1999, Section 5.1.2.2.1 Program startup</ref>。 在 C++ 標準中,main的標準型態應是int,否则类型是由实现定义的。任何实现都必须支持<code>int main() { /* ... */ }</code>和<code>int main(int argc, char* argv[]) { /* ... */ }</code><ref>「An implementation shall not predefine the '''main''' function. This function shall not be overloaded. It shall have a return type of type '''int''', but otherwise its type is implementation-defined. All implementations shall allow both of the following definitions of '''main''': <code>int main() { /* ... */ }</code> and <code>int main(int argc, char* argv[]) { /* ... */ }</code>.」,引自 ISO/IEC 14882, 第一版(1998)、第二版(2003)與第三版(2011), section 3.6.1 Main function</ref>。
“void main()”的用法并不是任何标准制定的<ref>[http://www.stroustrup.com/bs_faq2.html#void-main Can I write "void main()"?] The definition <code>void main() { /* ... */ }</code> is not and never has been C++, nor has it even been C.</ref><ref>{{Cite web |url=http://www.ibm.com/developerworks/cn/linux/l-c99/index.html |title=用 C99 进行开放源代码的开发 |accessdate=2011-01-21 }}</ref>。 C語言标准語法是“int main()”,任何实现都必须支持<code>int main(void) { /* ... */ }</code>和<code>int main(int argc, char* argv[]) { /* ... */ }</code><ref>「The function called at program startup is named '''main'''. The implementation declares no prototype for this function. It shall be defined with a return type of '''int''' and with no parameters: <code>int main(void) { /* ... */ }</code> or with two parameters (referred to here as '''argc''' and '''argv''', though any names may be used, as they are local to the function in which they are declared): <code>int main(int argc, char *argv[]) { /* ... */ }</code> or equivalent; or in some other implementation-defined manner.」,引自ISO/IEC 9899:1999, Section 5.1.2.2.1 Program startup</ref>。 在 C++ 標準中,main的標準型態應是int,否则类型是由实现定义的。任何实现都必须支持<code>int main() { /* ... */ }</code>和<code>int main(int argc, char* argv[]) { /* ... */ }</code><ref>「An implementation shall not predefine the '''main''' function. This function shall not be overloaded. It shall have a return type of type '''int''', but otherwise its type is implementation-defined. All implementations shall allow both of the following definitions of '''main''': <code>int main() { /* ... */ }</code> and <code>int main(int argc, char* argv[]) { /* ... */ }</code>.」,引自 ISO/IEC 14882, 第一版(1998)、第二版(2003)與第三版(2011), section 3.6.1 Main function</ref>。


== 参见 ==
== 参见 ==
第478行: 第475行:
=== 參考資料 ===
=== 參考資料 ===
{{reflist|refs=
{{reflist|refs=
<ref name="dottcl">{{cite web |url=http://cm.bell-labs.com/cm/cs/who/dmr/chist.html |title=The Development of the C Language |quote=The scheme of type composition adopted by C owes considerable debt to Algol 68, although it did not, perhaps, emerge in a form that Algol's adherents would approve of. |last=Ritchie |first=Dennis M. ||date=1993-01 |accessdate=2008-01-01 |||}}</ref>
<ref name="dottcl">{{cite web |url=http://cm.bell-labs.com/cm/cs/who/dmr/chist.html |title=The Development of the C Language |quote=The scheme of type composition adopted by C owes considerable debt to Algol 68, although it did not, perhaps, emerge in a form that Algol's adherents would approve of. |last=Ritchie |first=Dennis M. ||date=1993-01 |accessdate=2008-01-01 }}</ref>
}}
}}