256
C Programmierung de.wikibooks.org

CProgrammierung - · PDF file1 3. Grundlagen GNU-C-Compilerimplementiert.MicrosoftundBorland,diezudenwichtigstenCompi-lerherstellernzählen,

  • Upload
    ledung

  • View
    216

  • Download
    3

Embed Size (px)

Citation preview

  • C Programmierung

    de.wikibooks.org

  • 21. November 2015

    On the 28th of April 2012 the contents of the English as well as German Wikibooks and Wikipediaprojects were licensed under Creative Commons Attribution-ShareAlike 3.0 Unported license. A URIto this license is given in the list of figures on page 245. If this document is a derived work fromthe contents of one of these projects and the content was still licensed by the project under thislicense at the time of derivation this document has to be licensed under the same, a similar or acompatible license, as stated in section 4b of the license. The list of contributors is included in chapterContributors on page 237. The licenses GPL, LGPL and GFDL are included in chapter Licenses onpage 249, since this book and/or parts of it may or may not be licensed under one or more of theselicenses, and thus require inclusion of these licenses. The licenses of the figures are given in the list offigures on page 245. This PDF was generated by the LATEX typesetting software. The LATEX sourcecode is included as an attachment (source.7z.txt) in this PDF file. To extract the source fromthe PDF file, you can use the pdfdetach tool including in the poppler suite, or the http://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/ utility. Some PDF viewers may also let you savethe attachment to a file. After extracting it from the PDF file you have to rename it to source.7z.To uncompress the resulting archive we recommend the use of http://www.7-zip.org/. The LATEXsource itself was generated by a program written by Dirk Hnniger, which is freely available underan open source license from http://de.wikibooks.org/wiki/Benutzer:Dirk_Huenniger/wb2pdf.

    http://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/http://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/http://www.7-zip.org/http://de.wikibooks.org/wiki/Benutzer:Dirk_Huenniger/wb2pdf

  • Inhaltsverzeichnis

    0.1 Vorwort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

    1 Grundlagen 31.1 Historisches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31.2 Was war / ist das Besondere an C . . . . . . . . . . . . . . . . . . . . . . . 41.3 Der Compiler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41.4 Hello World . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51.5 Ein zweites Beispiel: Rechnen in C . . . . . . . . . . . . . . . . . . . . . . 71.6 Kommentare in C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

    2 Variablen und Konstanten 112.1 Was sind Variablen? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112.2 Deklaration, Definition und Initialisierung von Variablen . . . . . . . . . . 122.3 Ganzzahlen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152.4 Erweiterte Zeichenstze . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172.5 Kodierung von Zeichenketten . . . . . . . . . . . . . . . . . . . . . . . . . 172.6 Fliekommazahlen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182.7 Speicherbedarf einer Variable ermitteln . . . . . . . . . . . . . . . . . . . . 182.8 Konstanten . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192.9 Sichtbarkeit und Lebensdauer von Variablen . . . . . . . . . . . . . . . . . 21

    3 static & Co. 233.1 static . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233.2 volatile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243.3 register . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25

    4 Einfache Ein- und Ausgabe 274.1 printf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274.2 scanf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314.3 getchar und putchar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324.4 Escape-Sequenzen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33

    5 Operatoren 355.1 Grundbegriffe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355.2 Inkrement- und Dekrement-Operator . . . . . . . . . . . . . . . . . . . . . 365.3 Rangfolge und Assoziativitt . . . . . . . . . . . . . . . . . . . . . . . . . . 375.4 Der Shift-Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395.5 Ein wenig Logik . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405.6 und noch etwas Logik . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425.7 Bedingungsoperator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45

    III

  • Inhaltsverzeichnis

    6 Kontrollstrukturen 476.1 Bedingungen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 476.2 Schleifen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 526.3 Sonstiges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61

    7 Funktionen 637.1 Funktionsdefinition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 637.2 Prototypen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 667.3 Inline-Funktionen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 687.4 Globale und lokale Variablen . . . . . . . . . . . . . . . . . . . . . . . . . . 687.5 exit() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71

    8 Eigene Header 73

    9 Zeiger 759.1 Beispiel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 759.2 Zeigerarithmetik . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 779.3 Zeiger auf Funktionen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 789.4 void-Zeiger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 799.5 Call by Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 809.6 Verwendung . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81

    10 Arrays 8310.1 Eindimensionale Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8310.2 Mehrdimensionale Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . 8410.3 Arrays initialisieren . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8910.4 Syntax der Initialisierung . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9010.5 Eindimensionales Array vollstndig initialisiert . . . . . . . . . . . . . . . 9010.6 Eindimensionales Array teilweise initialisiert . . . . . . . . . . . . . . . . . 9010.7 Mehrdimensionales Array vollstndig initialisiert . . . . . . . . . . . . . . 9110.8 Mehrdimensionales Array teilweise initialisiert . . . . . . . . . . . . . . . . 9310.9 Arrays und deren Speicherplatz . . . . . . . . . . . . . . . . . . . . . . . . 9410.10 bergabe eines Arrays an eine Funktion . . . . . . . . . . . . . . . . . . . 9510.11 Zeigerarithmetik . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9710.12 Zeigerarithmetik auf Char-Arrays . . . . . . . . . . . . . . . . . . . . . . . 9910.13 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101

    11 Strings und Zeichenkettenfunktionen 10311.1 Zeichenkettenfunktionen . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10311.2 Gefahren . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11211.3 Iterieren durch eine Zeichenkette (Ersetzen eines bestimmten Zeichens

    durch ein anderes in einem String) . . . . . . . . . . . . . . . . . . . . . . 11311.4 Die Bibliothek ctype.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114

    12 Komplexe Datentypen 11712.1 Strukturen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11712.2 Unions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11912.3 Aufzhlungen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12112.4 Variablen-Deklaration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122

    IV

  • Inhaltsverzeichnis

    13 Typumwandlung 12513.1 Implizite Typumwandlung . . . . . . . . . . . . . . . . . . . . . . . . . . . 12513.2 Explizite Typumwandlung . . . . . . . . . . . . . . . . . . . . . . . . . . . 12513.3 Verhalten von Werten bei Typumwandlungen . . . . . . . . . . . . . . . . 126

    14 Speicherverwaltung 12714.1 Ort und Art der Speicherreservierung (Speicherklasse) . . . . . . . . . . . 12714.2 Zeitpunkt der Speicherreservierung . . . . . . . . . . . . . . . . . . . . . . 128

    15 Verkette Listen 13115.1 Die einfach verkettete Liste . . . . . . . . . . . . . . . . . . . . . . . . . . 131

    16 Fehlerbehandlung 133

    17 Prprozessor 13517.1 Direktiven . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135

    18 Dateien 13918.1 Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13918.2 Echte Dateien . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14218.3 Streams und Dateien . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144

    19 Rekursion 14519.1 Rekursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14519.2 Beseitigung der Rekursion . . . . . . . . . . . . . . . . . . . . . . . . . . . 14519.3 Weitere Beispiele fr Rekursion . . . . . . . . . . . . . . . . . . . . . . . . 146

    20 Programmierstil 14720.1 Kommentare . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14720.2 Globale Variablen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14720.3 Goto-Anweisungen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14720.4 Namensgebung . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14820.5 Gestaltung des Codes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14820.6 Standard-Funktionen und System-Erweiterungen . . . . . . . .