meta data for this page
Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
digital:programmieren:c [2019/01/11 12:21] natrius |
— (current) | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | # C | ||
- | Zusammenfassung für nützliche Sachen beim Programmieren mit C. | ||
- | |||
- | ## Ressourcen | ||
- | * https:// | ||
- | * http:// | ||
- | * http:// | ||
- | |||
- | ## Cheat Sheet | ||
- | |||
- | ### Main Function | ||
- | |||
- | <code c> | ||
- | void main(void) { | ||
- | |||
- | } | ||
- | </ | ||
- | |||
- | ### Printf | ||
- | |||
- | <code c> | ||
- | #include < | ||
- | |||
- | void main(void) { | ||
- | int i = 1; | ||
- | unsigned u = 2; | ||
- | long l = 3; | ||
- | float f = 4.0; | ||
- | double d = 5.0; | ||
- | char c = 6; | ||
- | unsigned char uc = 7; | ||
- | |||
- | printf(" | ||
- | i, u, l, f, d, c, c, uc); | ||
- | |||
- | printf(" | ||
- | |||
- | } | ||
- | </ | ||
- | |||
- | ### Scanf | ||
- | |||
- | <code c> | ||
- | #include < | ||
- | |||
- | void main(void) { | ||
- | int i; | ||
- | |||
- | | ||
- | | ||
- | |||
- | } | ||
- | </ | ||
- | |||
- | ### Conditionals | ||
- | |||
- | <code c> | ||
- | if(flag) { | ||
- | // put some statements here to execute if flag is true (flag != 0) | ||
- | } | ||
- | |||
- | if(flag) { | ||
- | // put some statements here to execute if flag is true (flag != 0) | ||
- | } else { | ||
- | // put some statements here to execute if flag is false (flag == 0) | ||
- | } | ||
- | |||
- | switch(flag) { | ||
- | case 0: // statements | ||
- | | ||
- | case 1: // statements | ||
- | break; | ||
- | case 2: // statements | ||
- | break; | ||
- | default: | ||
- | |||
- | } | ||
- | </ | ||
- | |||
- | ### Looping | ||
- | |||
- | <code c> | ||
- | while(flag) { | ||
- | // make sure there is some statement in here to change flag to become false. | ||
- | } | ||
- | |||
- | for(i = 0; i < LAST; i++) { | ||
- | // statements | ||
- | } | ||
- | </ | ||
- | |||
- | ### Math Functions | ||
- | |||
- | <code c> | ||
- | #include < | ||
- | |||
- | void main(void) { | ||
- | double th = pi/2; // th is in radians | ||
- | double x, y; | ||
- | |||
- | x = cos(th); | ||
- | y = sin(th); | ||
- | th = atan2(y, x); | ||
- | } | ||
- | |||
- | ### Creating Functions | ||
- | |||
- | int functionname(type1 input1, ... , typeN *output1, ...); // this is the function prototype with the ; | ||
- | int functionname(type1 input1, ... , typeN *output1, ...) | ||
- | { | ||
- | *output1 = // some function of the input variables. | ||
- | *output2 = // some function of the input varialbles. | ||
- | ... | ||
- | return(someintvalue); | ||
- | } | ||
- | </ | ||
- | |||
- | ## Input seperated with space or / | ||
- | |||
- | * https:// | ||
- | |||
- | To match both the space-separated and the slash-separated inputs, you'll need a modestly complex format string: | ||
- | |||
- | < | ||
- | if (scanf(" | ||
- | ...data was read properly... | ||
- | else | ||
- | ...something went wrong... | ||
- | </ | ||
- | |||
- | The first conversion specification is a scan set that accepts a sequence of non-blanks, non-slashes (so it will stop at the first blank or slash). It would be best to specify an upper bound on how many characters will be accepted so as to avoid stack overflow; for example, if char name[32];, then %31[^ /] (note the off-by-one). The second conversion specification %*1[ /] accepts a single character (1) that is either a blank or slash [ /], and does not assign it to any variable (*). The third conversion specification is a standard numeric input, skipping leading blanks, allowing for negative numbers to be entered, etc. The fourth conversion specification is the same as the second, and the fifth is a standard format for a float (which means that 34000.25 with 7 significant digits is at the outer end of the range of representable values). | ||
- | |||
- | Note that the ' | ||
- | |||
- | ## Datentypen | ||
- | |||
- | ^ Type ^ Keyword | ||
- | | character | ||
- | | unsigned character | ||
- | | integer | ||
- | | short integer | ||
- | | long integer | ||
- | | unsigned integer | ||
- | | unsigned short integer | ||
- | | unsigned long integer | ||
- | | single-precision floating-point (7 Stellen) | ||
- | | double-precision floating-point (19 Stellen) | ||
- | |||
- | ## Zusammenfassung Tutorial | ||
- | |||
- | ### Bit, Byte | ||
- | |||
- | * 1 Byte = 8 Bit | ||
- | * 100 MB (MegaBit) = 12,5 Megabyte (Mb) | ||
- | |||
- | ### Datentypen bei Deklaration | ||
- | |||
- | * char, int, float, double | ||
- | * char = einzelne Zeichen, keine Zahlen | ||
- | * int = Ganzzahlen | ||
- | * float, double = Kommazahlen | ||
- | |||
- | Variablen am besten immer gleich mit Werten deklarieren, | ||
- | |||
- | ### Konstanten | ||
- | |||
- | Der Compiler bringt eine Warnung, wenn die Variable noch einmal zugewiesen wird. '' | ||
- | |||
- | ### Variablenbenennung | ||
- | |||
- | * Der Name darf nur aus Buchstaben, Ziffern und dem Unterstrich _ bestehen. | ||
- | * Das erste Zeichen muss ein Buchstabe oder der Unterstrich _ sein. | ||
- | * Groß- und Kleinschreibung ist relevant, d.h. „zahl“ und „Zahl“ ist nicht dasselbe. | ||
- | * Der Name darf kein Schlüsselwort sein. | ||
- | |||
- | Üblicherweise mit Kleinschreibung beginnen und Worttrennung über Großschreibung realisieren. CamelCase | ||
- | |||
- | ### Inkrement | ||
- | |||
- | <code c> | ||
- | int a, b=0; | ||
- | // Erst Zuweisung, dann Inkrement, a ist 0, b ist 1 | ||
- | a = b++; | ||
- | // Erst Inkrement, dann Zuweisung, a ist 2, b ist 2 | ||
- | a = ++b; | ||
- | </ | ||
- | |||
- | ### Ein- Ausgabe | ||
- | |||
- | Was rein geht, geht auch raus. | ||
- | |||
- | #### Variablen einlesen | ||
- | |||
- | float variable=32.5; | ||
- | | ||
- | #### Ausgabe | ||
- | |||
- | printf(" | ||
- | |||
- | Integer kann mit double ausgegeben werden. Kommazahlen sind auch möglich. | ||
- | |||
- | ### Zeichen einlesen | ||
- | |||
- | #### Einzelnes Zeichen | ||
- | |||
- | c = getchar(); | ||
- | | ||
- | #### Zahlen einlesen | ||
- | |||
- | scanf(" | ||
- | | ||
- | Double in, double out. Mit %d werden Ganzzahlen eingelesen, mit %f Kommazahlen. Alle Eingaben inklusive dem Enter kommen in den Puffer. Um das Enter abzufangen, könnte das enter in eine Variable &temp abgefangen werden. | ||
- | |||
- | ## Verzweigungen | ||
- | |||
- | ### If Else | ||
- | |||
- | <code c> | ||
- | int zahl=6; | ||
- | if(zahl==5) { | ||
- | printf(" | ||
- | }else { | ||
- | if(zahl==6) { | ||
- | printf(" | ||
- | }else { | ||
- | printf(" | ||
- | } | ||
- | } | ||
- | </ | ||
- | |||
- | #### Kurzfassung | ||
- | |||
- | Kommt nur eine Anweisung in den if Block, so könnte man auch die geschweiften Klammern weglassen. | ||
- | |||
- | <code c> | ||
- | int zahl=6; | ||
- | if(zahl==5) printf(" | ||
- | else if(zahl==6) printf(" | ||
- | else printf(" | ||
- | </ | ||
- | |||
- | ### Vergleichoperatoren | ||
- | |||
- | == Ist gleich | ||
- | != Ist nicht gleich | ||
- | > Größer | ||
- | >= Größer gleich | ||
- | < Kleiner | ||
- | <= Kleiner gleich | ||
- | |||
- | ### Logische Operatoren | ||
- | |||
- | <code c> | ||
- | ! Negation | ||
- | && UND | ||
- | !! ODER | ||
- | </ | ||
- | |||
- | ### Switch case | ||
- | |||
- | <code c> | ||
- | switch(a){ | ||
- | case 1: printf(" | ||
- | case 2: printf(" | ||
- | } | ||
- | </ | ||
- | |||
- | ### While Schleife | ||
- | |||
- | <code c> | ||
- | while(bedingung i < 1000){ | ||
- | printf(" | ||
- | i++; | ||
- | } | ||
- | </ | ||
- | |||
- | ### For Schleife | ||
- | |||
- | <code c>int i; | ||
- | |||
- | for(i=0; i<5; i++) { | ||
- | printf(" | ||
- | } | ||
- | </ | ||
- | |||
- | ### Do while | ||
- | |||
- | <code c> | ||
- | int alter; | ||
- | |||
- | do { | ||
- | printf(" | ||
- | scanf(" | ||
- | } while(alter < 5 || alter > 100); | ||
- | |||
- | printf(" | ||
- | </ | ||
- | |||
- | ## Funktionen | ||
- | |||
- | Die Funktion muss vor dem Main positioniert werden, damit sie dann in der Main aufgerufen werden kann. | ||
- | |||
- | <code c> | ||
- | # | ||
- | |||
- | int addiere(int summand1, int summand2) { | ||
- | return (summand1 + summand2); | ||
- | } | ||
- | |||
- | int main() { | ||
- | int summe = addiere(3, 7); | ||
- | printf(" | ||
- | return 0; | ||
- | } | ||
- | </ | ||
- | |||
- | ### Funktionsprototypen | ||
- | |||
- | Diese kommen vor das Hauptprogramm, | ||
- | |||
- | <code c> | ||
- | |||
- | # | ||
- | |||
- | // Funktions-Prototypen | ||
- | float eingabeZahl(); | ||
- | float multipliziere(float zahl1, float zahl2); | ||
- | void ausgabeErgebnis(float ergebnis); | ||
- | |||
- | // Hauptprogramm | ||
- | int main() { | ||
- | </ | ||
- | |||
- | ### Arrays | ||
- | |||
- | Arrays sind einzeilige Matrixen um viele Daten speichern zu können. Zugegriffen wird auf die einzelnen Werte mit dem Index. | ||
- | |||
- | ### Schleifen | ||
- | |||
- | Setzen von Werten mit Benutzereingabe | ||
- | |||
- | <code c> | ||
- | |||
- | int punkte[5], i; | ||
- | // Werte einlesen | ||
- | for(i=0; i<5; i++) { | ||
- | printf(" | ||
- | scanf(" | ||
- | } | ||
- | |||
- | // Werte auslesen | ||
- | for(i=0; i<5; i++) { | ||
- | printf(" | ||
- | } | ||
- | </ | ||
- | |||
- | ### Initialisierung | ||
- | |||
- | Dazu die Werte eines Feldes einfach in geschweifte Klammern schreiben. Ist die Anzahl der Werte kleiner als die Feldgröße, | ||
- | <code c> | ||
- | int i, punkte[5] = { 1, 3, 5, 7, 9 }; | ||
- | |||
- | // Werte ausgeben | ||
- | for(i=0; i<5; i++) { | ||
- | printf(" | ||
- | } | ||
- | </ | ||
- | |||
- | === Zweidimensionale Felder === | ||
- | <code c> int brett[8][8]; | ||
- | Für eine bessere Vorstellung kann man von | ||
- | <code c>int brett[y][y];</ | ||
- | |||
- | == Initialisierung == | ||
- | Die Initialisierung zweidimensionaler Felder erfolgt nach unserer Definition der Achsen spaltenweise. | ||
- | |||
- | === Mehrdimensionale Felder === | ||
- | Der Dimension sind keine Grenzen gesetzt, ein 3D Feld könnte wie folgt initialisiert werden | ||
- | <code c>int cube[2][2][2];</ | ||
- | |||
- | === Zeigerarithmetik === | ||
- | Der Zugriff erfolgt mittels Zeigern. Der Zugriff mit [] ist nur eine Möglichkeit, | ||
- | |||
- | == Positionszeiger == | ||
- | Ein Positionszeiger kann immer auf einen bestimmten Wert im Array zeigen. Die Adresse des ersten Elements erhalten wir durch den bloßen Feldnamen. | ||
- | <code c> | ||
- | int punkte[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; | ||
- | |||
- | // Positionszeiger | ||
- | int *pos; | ||
- | |||
- | // Position auf erstes Element setzen (punkte[0]) | ||
- | pos = punkte; | ||
- | printf(" | ||
- | |||
- | // Position auf naechstes Element setzen (punkte[1]) | ||
- | pos++; | ||
- | printf(" | ||
- | |||
- | // Position auf 5. Element setzen (punkte[4]) | ||
- | pos = punkte + 4; | ||
- | printf(" | ||
- | |||
- | // Position auf vorheriges Element setzen (punkte[3]) | ||
- | pos--; | ||
- | printf(" | ||
- | </ | ||
- | |||
- | == ermittlung der Index-Nummer == | ||
- | <code c>int punkte[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; | ||
- | |||
- | // Positionszeiger, | ||
- | int *pos = punkte + 5; | ||
- | |||
- | // Index berechnen | ||
- | int index = pos - punkte; | ||
- | |||
- | printf(" | ||
- | printf(" | ||
- | </ | ||
- | |||
- | == Mehrdimensional == | ||
- | <code c> | ||
- | // Array und Spaltengrenze setzen | ||
- | int brett[8][8]={ 0 }, Y_Max=8; | ||
- | |||
- | // Startposition | ||
- | int *posStart = & | ||
- | |||
- | // Testwert setzen | ||
- | brett[2][4] = 7; | ||
- | |||
- | // Beschaffung Testwert mit Zeigerarithmetik | ||
- | int *pos; | ||
- | pos = posStart + (y * Y_Max) + x; | ||
- | |||
- | printf(" | ||
- | </ | ||
- | Die entscheidende Programmzeile ist die, in der der Positionszeiger pos gesetzt wird. Seine Basis ist die Startadresse des Arrays. Benötigt wird der Wert in Zeile 2 und Spalte 4, Index [2][4]. Um dorthin zu kommen, müssen wir uns erst zeilenweise und dann spaltenweise vorarbeiten. Also setzen wir den Zeiger auf das erste Element in Zeile 2. Eine Zeile hat 8 Spalten-Elemente, | ||
- | |||