C语言(英語:C Language)是一种通用的、过程式编程程式語言,支持结构化编程、词法作用域和递归,使用静态类型系统,并且广泛用于系统软件与应用软件的开发。
C语言于1969年至1973年間,為了移植與開發UNIX作業系統,由丹尼斯·里奇與肯·汤普逊,以B语言为基础,在贝尔实验室設計、开发出來。二十世纪八十年代,C语言应用日渐广泛。為了避免各開發廠商用的C語言的語法產生差異,美國國家標準局為C語言訂定了一套完整的國際標準語法,稱為ANSI C,作為C語言的標準。与此同时,国际标准化组织也接受该标准为国际标准。因此,ANSI C也同时被称为ISO C。二十世纪八十年代至今的有关程式開發工具,一般都支持符合ANSI C的語法。
C语言具有高效、灵活、功能丰富、表达力强和較高的可移植性等特点,在程式設計中备受青睐,成为最近25年使用最为广泛的编程语言[4]。目前,C语言編譯器普遍存在於各種不同的操作系统中,例如Microsoft Windows、macOS、Linux、Unix等。C語言的設計影響了众多後來的程式語言,例如C++、Objective-C、Java、C#等。现行的许多软件都是由C语言或者其影响和衍生的编程语言开发出来的。
与ALGOL一族的大多数过程式编程语言类似,C語言是一個有結構化程式設計、具有变量作用域(variable scope)以及遞迴功能的程序式語言。其采用的静态类型系统可以防止无意的程序设计操作。C语言中所有的可执行代码都被包含在子程序(函数)裡。其傳遞參數均是以值傳遞(pass by value)[5],另外也可以傳遞指针(a pointer passed by value)。C语言是自由形式语言,即其源代码的缩进并不影响程序的功能,而是使用分号作为语句的结尾,花括号来表示代码块。
由于C语言的语言规模较小,若干高层的机制需要使用定义的函数来提供。比如,C语言并没有直接处理复合对象(例如字符串、集合、列表、数组等)的操作,也没有对于存储器分配工具和内存回收工具的直接定义,同时也本身不具有输入和输出以及文件访问的方法。然而,用户定义的函数和C语言标准库中的函数为这些高层的机制提供了可能性。[6]
C语言也具有以下的特性:[6]
20世纪70年代,肯·汤姆森为了使其设计的Unix系统更加高效,使用B语言的变种(即C语言)在DEC PDP-7计算机上重写了Unix。C语言中许多重要概念来源于BCPL语言,其对C语言的影响也间接地来源于B语言。在1978年,丹尼斯·里奇和布萊恩·柯林漢合作出版了《C程序设计语言》第一版,事实上即为K&R C标准[7]。1983年,为了制定一个独立于具体机器且无歧义的C语言标准,美国国家标准协会成立了一个委员会,并在1988年完成了该标准的制定,即ANSI C。此标准同时被国际标准化组织所采纳,也被称作ISO C。
其后,C语言至今经历了几次标准更新,诞生了C99、C11和目前最新的标准C18。C语言标准的下一次更新C2x目前正在起草中。
C語言的语法相对简洁而直接。C语言的形式文法由国际标准化组织所制定。[8]簡單來說,C語言包括如下文法:
//
/*
*/
int
float
double
char
enum
int numbers[5] = {1, 2, 3, 4, 5};
struct
union
if
else
for
while
do-while
goto
break
continue
jmp
int *array = (int *)malloc(5 * sizeof(int)); // 分配一個包含五個整數的數組 free(array); // 釋放使用malloc分配的內存
现在广泛被编程初学者使用的"hello, world"程序实例最初就是出现在《C程序设计语言》第一版中。下面是一個在標準輸出設備(stdout)上打印出 "Hello, world!" 字串的簡單程式。類似的程式,通常作為初學程式語言時的第一個程式:
#include <stdio.h> int main(void) { printf("Hello, world!\n"); return 0; }
其中只有int,void,return为C语言的关键字,预处理器会将#include <stdio.h>替换为stdio.h文件的内容。
#include <stdio.h>
main函数是C语言程序的入口点。
"Hello, world!\n"中的\n是一个转义字符,形式为\加上一个字符。所起的作用在ASCII码中规定。
"Hello, world!\n"
\n
\
printf是声明于stdio.h的函数,关于printf的更多细节,参见printf;
关于格式化字符串的更多信息,参见格式化字符串。
C语言的特色之一是:程序员必须亲自处理内存的分配细节。语言不负责内存边界检查,这是因为在运行时进行内存边界检查会造成性能问题,与UNIX哲学不符。此特性容易导致缓冲区溢出问题。然而,部分编译器(如英特尔编译器)会出于安全性的考量,提供方法以进行运行时内存边界检查[9]。
大多数C语言实现使用栈(Stack)来保存函数返回地址/栈帧基址、完成函数的参数传递和函数局部变量的存储。然而,在部分极特殊的平台上,使用栈并不能获得最大效率。此时的实现由编译器决定[10]。 如果程序需要在运行的过程中动态分配内存,可以利用堆(Heap)来实现。
基本上C程序的元素存储在内存的时候有3种分配策略:
如果一个变量声明为全局变量或者是函数的静态变量,这个变量的存储将使用静态分配方式。静态分配的内存一般会被编译器放在数据段或代码段来存储,具体取决于实现。这样做的前提是,在编译时就必须确定变量的大小。 以IA32的x86平台及gcc编译器为例,全局及静态变量放在数据段的低端;全局及静态常量放在代码段的高端。
函数的自动局部变量应该随着函数的返回会自动释放(失效),这个要求在一般的体系中都是利用栈(Stack)来满足的。相比于静态分配,这时候,就不必绝对要求这个变量在编译时就必须确定变量的大小,运行时才决定也不迟,但是C89仍然要求在编译时就要确定,而C99放松了这个限制。但无论是C89还是C99,都不允许一个已经分配的自动变量运行时改变大小。
所以说C函数永远不应该返回一个局部变量的地址。
要指出的是,自动分配也属于动态分配,甚至可以用alloca函数来像分配堆(Heap)一样进行分配,而且释放是自动的。
还有一种更加特殊的情况,变量的大小在运行时有可能改变,或者虽然单个变量大小不变,变量的数目却有很大弹性,不能静态分配或者自动分配,这时候可以使用堆(Heap)来满足要求。ANSI C定义的堆操作函数是malloc、calloc、realloc和free。
使用堆(Heap)内存将带来额外的开销和风险。
C語言的标准文档要求了一个平台移植C语言的时候至少要实现的一些功能和封装的集合,称为“标准库”,标准庫的声明头部通過预处理器命令#include進行引用。
在C89標準中:
在94年的修正版中
在C99中增加了六個函式庫
以上是C语言的标准。各个系统各自又对C库函数进行的各种扩充,就浩如烟海了。如POSIX C、GNU C等。
工具軟體可以幫助程式設計者避免一些程式中潛藏或容易出現的問題,例如常會造成程式未預期動作或是執行期錯誤的程式碼。
許多語言都有自動源代碼檢查及審計工具,C語言也有類似工具,像是Lint。可以在程式剛寫好時用Lint找出可能有問題的程式,通過Lint後再用C編譯器進行編譯,許多編譯器也可以設定是否要針對一些可能有問題的程式碼提出警告。MISRA C是一套針對嵌入式系統的法則,可主要也是避免一些可能有問題的程式碼。
也有一些編譯器、程式庫或作業系統可以處理一些非標準C語言的功能,例如邊界值檢查、缓存溢出偵測、序列化及自動垃圾回收功能。
使用像Valgrind或IBM Rational Purify(英语:Purify)等軟體工具,或者連結有特別malloc函式的程式庫,有助於找出一些運行期記憶體使用的問題。
“void main()”的用法并不是任何标准制定的[11][12]。 C語言标准語法是“int main()”,任何实现都必须支持int main(void) { /* ... */ }和int main(int argc, char* argv[]) { /* ... */ }[13]。 在 C++ 標準中,main的標準型態應是int,否则类型是由实现定义的。任何实现都必须支持int main() { /* ... */ }和int main(int argc, char* argv[]) { /* ... */ }[14]。
int main(void) { /* ... */ }
int main(int argc, char* argv[]) { /* ... */ }
int main() { /* ... */ }
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.
In C, all function arguments are passed ``by value.
void main() { /* ... */ }
int main(int argc, char *argv[]) { /* ... */ }
Artikel ini tidak memiliki referensi atau sumber tepercaya sehingga isinya tidak bisa dipastikan. Tolong bantu perbaiki artikel ini dengan menambahkan referensi yang layak. Tulisan tanpa sumber dapat dipertanyakan dan dihapus sewaktu-waktu.Cari sumber: Kerajaan Sontang – berita · surat kabar · buku · cendekiawan · JSTOR Kerajaan Sontang adalah salah satu kerajaan di Provinsi Sumatera Barat. Kerajaan Sontang bergabung dalam Republik Indonesia pada tahun 19…
1934 film by George White George White's ScandalsTheatrical release posterDirected byGeorge WhiteScreenplay byJack YellenStory byGeorge White Robert KaneProduced byGeorge WhiteStarringRudy ValléeJimmy DuranteAlice FayeAdrienne AmesGregory RatoffCliff EdwardsDixie DunbarCinematographyLee GarmesGeorge SchneidermanEdited byPaul WeatherwaxMusic byDavid ButtolphHugo FriedhoferProductioncompanyFox Film CorporationDistributed byFox Film CorporationRelease date March 16, 1934 (1934-03-16…
Matador Israel Lancho di capote de paseo (dress cape) sebelum bertarung dengan banteng pada festival Aste Nagusia festival di Bilbao, Spanyol, tahun 2005 Matador, secara harafiah berarti pembunuh, adalah seorang torero yang sangat ahli di dalam pertarungan melawan banteng. Torero adalah pelaku utama dalam pertunjukan pertarungan manusia melawan banteng di Spanyol dan negara-negara berbahasa Spanyol lainnya. Dalam pertarungan ini, seorang torero berperan mempermainkan dan pada akhirnya membunuh b…
RI Dorang (311), an Indonesian Navy Kraljevica-classClass overview BuildersTito's Shipyard, Kraljevica, PR Croatia, FPR Yugoslavia Operators Yugoslav Navy Bangladesh Navy Ethiopian Navy Indonesian Navy Sudanese Navy Succeeded byMornar class Built1951–1957 In commission1951–late 1980s Completed24 Retired24 General characteristics (First series) TypeAnti-susbmarine patrol boat DisplacementStandard: 195 tonnes (192 long tons; 215 short tons) Full: 220 tonnes (…
القطلونية الاسم الذاتي Català الناطقون أكثر من 11.5 مليون الدول إسبانيا فرنسا إيطاليا أندورا المنطقة قطلونية، بلنسية، جزر البليار، قطلونية الشمالية، أرغونة، مرسية، سردانية، أندرة الرتبة 75 الكتابة أبجدية لاتينية النسب هندية أوروبية إيطاليقيةرومنسيةقسطانية روما
Town in Kerala, India For the 2009 film, see Munnar (film). Hill station in Kerala, IndiaMunnarHill stationA view of MunnarNickname: The Kashmir of South IndiaMunnarShow map of KeralaMunnarShow map of IndiaCoordinates: 10°05′21″N 77°03′35″E / 10.08917°N 77.05972°E / 10.08917; 77.05972CountryIndiaStateKeralaDistrictIdukkiTalukDevikulamNamed forTea plantations, cool climateGovernment • TypeGrama Panchayat • BodyMunnar grama panchayat…
Calabria - 01 Plurinominalecollegio elettoraleStato Italia Elezioni perSenato della Repubblica ElettiSenatori Periodo 2017-2022Tipologiaplurinominale Numero eletti6 Periodo 2022-Numero eletti4 Territorio Manuale Il collegio elettorale plurinominale Calabria - 01 è un collegio elettorale plurinominale della Repubblica Italiana per l'elezione del Senato. Indice 1 Territorio 2 XVIII legislatura 2.1 Risultati elettorali 2.2 Eletti 2.2.1 Eletti nei collegi uninominali 2.2.2 Eletti nella quota p…
هذه المقالة يتيمة إذ تصل إليها مقالات أخرى قليلة جدًا. فضلًا، ساعد بإضافة وصلة إليها في مقالات متعلقة بها. (نوفمبر 2019) دانيال جاي سوليفان معلومات شخصية الميلاد 10 يونيو 1940 (83 سنة) مواطنة الولايات المتحدة الحياة العملية المدرسة الأم جامعة سان فرانسيسكو الحكومية المهن
مريم المصرية معلومات شخصية الميلاد سنة 344 الإسكندرية الوفاة سنة 421 (76–77 سنة)[1] فلسطين مواطنة روما القديمة الحياة العملية المهنة ناسكة تعديل مصدري - تعديل مريم المصرية أو ماريا المصرية وسميت أيضاً مريم القبطية (بالقبطية: Ϯⲁⲅⲓⲁ Ⲙⲁⲣⲓⲁ Ⲛⲣⲉ…
This article relies excessively on references to primary sources. Please improve this article by adding secondary or tertiary sources. Find sources: Prime Minister parodies Private Eye – news · newspapers · books · scholar · JSTOR (April 2008) (Learn how and when to remove this template message) Prime Minister parodies are a long-running feature of the British satirical magazine Private Eye, which have been included in the majority of issues since th…
Methods of teaching Pedagogy Approaches Critical pedagogyAnti-oppressive education Student-centered learningDialogic learning Key concepts EducationSocietyCulture Hidden curriculumLearning Learning spaceLearning theories vte Critical pedagogy Major works Pedagogy of the Oppressed Critical Pedagogy Primer Learning to Labour Schooling and the Struggle for Public Life Theorists Paulo Freire Henry Giroux Peter McLaren bell hooks Antonia Darder Joe Kincheloe Shirley Steinberg Paul Willis Ira Shor Ped…
Artikel ini sebatang kara, artinya tidak ada artikel lain yang memiliki pranala balik ke halaman ini.Bantulah menambah pranala ke artikel ini dari artikel yang berhubungan atau coba peralatan pencari pranala.Tag ini diberikan pada November 2022. Hein SimonsInformasi latar belakangNama lainHeintjeLahir12 Agustus 1955 (umur 68)AsalHeerlen[1], BelandaGenrepopPekerjaanpenyanyiInstrumenvokalTahun aktif1967-sekarangSitus webhttp://www.heinsimons.com/ Hendrik (Hein) Nicolaas Theodoor Simon…
Point on a stream's profile where a sudden change in stream gradient occurs This article needs additional citations for verification. Please help improve this article by adding citations to reliable sources. Unsourced material may be challenged and removed.Find sources: Knickpoint – news · newspapers · books · scholar · JSTOR (December 2015) (Learn how and when to remove this template message) The Horseshoe Falls, one of the three Niagara Falls. The falls…
Domain of single-celled organisms Archea redirects here. Not to be confused with Archean. For other uses, see Archaea (disambiguation). ArchaeaTemporal range: 3420–0 Ma Pha. Proterozoic Archean Had. Paleoarchean – present Scientific classification Domain: ArchaeaWoese, Kandler & Wheelis, 1990[1] Kingdoms[2][3] DPANN Euryarchaeida Proteoarchaeota Synonyms Archaebacteria Woese & Fox, 1977 Mendosicutes Gibbons & Murray, 1978 Metabacteria Hori and Osawa 1…
Intermediate appellate court of Utah This article needs additional citations for verification. Please help improve this article by adding citations to reliable sources. Unsourced material may be challenged and removed.Find sources: Utah Court of Appeals – news · newspapers · books · scholar · JSTOR (January 2022) (Learn how and when to remove this template message) The Utah Court of Appeals is the intermediate-level appellate court for the state of Utah. …
هذه المقالة يتيمة إذ تصل إليها مقالات أخرى قليلة جدًا. فضلًا، ساعد بإضافة وصلة إليها في مقالات متعلقة بها. (فبراير 2020) نادي العينموسم 2019–20الرئيسمازن بن ردادالمدير الفنيالحبيب بن رمضانالملعبمدينة الملك سعود بن عبد العزيز الرياضيةدوري الدرجة الأولى السعوديالأولكأس خادم الح…
Film festival 58th Venice International Film FestivalFestival posterOpening filmDustLocationVenice, ItalyFounded1932AwardsGolden Lion: Monsoon WeddingFestival date29 August – 8 September 2001WebsiteWebsiteVenice Film Festival chronology59th 57th The 58th annual Venice International Film Festival was held between 29 August to 8 September 2001.[1][2] The opening night film was Dust by Milcho Manchevski. Italian filmmaker Nanni Moretti was the Jury President of the main competitio…
Portuguese footballer In this Portuguese name, the first or maternal family name is Pina and the second or paternal family name is Eugenio. Pedro Eugénio Eugénio playing for Beroe in 2012Personal informationFull name Pedro Miguel Pina Eugénio[1]Date of birth (1990-06-26) 26 June 1990 (age 33)[1]Place of birth Faro, Portugal[1]Height 1.72 m (5 ft 8 in)[1]Position(s) Winger, right-backTeam informationCurrent team Free AgentYouth career1999…
Globalisasi Budaya Demokratis Ekonomi Historis Garis besar Istilah Portal Studi Commons Kategorilbs Lukisan pasar malam, Hessisches Volksfest(Hestgdisian Folk Festival)tfhh1887, Louis Toussaint (1826-1887), Öl auf Leinwand. Globalisasi budaya adalah penyebaran gagasan, makna, dan nilai ke seluruh dunia dengan cara tertentu untuk memperluas dan mempererat hubungan sosial.[1] Proses ini ditandai oleh konsumsi budaya bersama yang dibantu oleh Internet, media budaya masyarakat, dan perjalan…
Serbian general and politician You can help expand this article with text translated from the corresponding article in Serbian. (December 2014) Click [show] for important translation instructions. View a machine-translated version of the Serbian article. Machine translation, like DeepL or Google Translate, is a useful starting point for translations, but translators must revise errors as necessary and confirm that the translation is accurate, rather than simply copy-pasting machine-translat…
Lokasi Pengunjung: 54.175.59.242