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 [2018/10/12 10:48] natrius |
digital:programmieren:c [2022/02/22 16:44] natrius removed |
||
---|---|---|---|
Line 1: | Line 1: | ||
# C | # C | ||
+ | |||
Zusammenfassung für nützliche Sachen beim Programmieren mit C. | Zusammenfassung für nützliche Sachen beim Programmieren mit C. | ||
## Ressourcen | ## Ressourcen | ||
+ | |||
+ | * https:// | ||
* https:// | * https:// | ||
* http:// | * http:// | ||
* http:// | * http:// | ||
+ | * http:// | ||
+ | * http:// | ||
+ | * ALGORITHMS http:// | ||
+ | * Propositional Logic: Introduction https:// | ||
+ | * https:// | ||
+ | * Allgemein, MultiOS-Gamedev: | ||
+ | * https:// | ||
+ | * Structure my program https:// | ||
+ | * https:// | ||
+ | * http:// | ||
+ | * http:// | ||
+ | |||
+ | ### Starting a project, planning | ||
+ | |||
+ | * https:// | ||
+ | * https:// | ||
+ | * https:// | ||
+ | |||
+ | ### Games | ||
+ | |||
+ | * https:// | ||
+ | * https:// | ||
+ | * https:// | ||
+ | * https:// | ||
+ | * https:// | ||
+ | * https:// | ||
+ | * paid? - https:// | ||
+ | |||
+ | ## 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 | ||
+ | |||
+ | <code c> | ||
+ | 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:// | ||
- | ===== 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: | To match both the space-separated and the slash-separated inputs, you'll need a modestly complex format string: | ||
+ | |||
< | < | ||
if (scanf(" | if (scanf(" | ||
Line 16: | Line 160: | ||
...something went wrong... | ...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). | 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 ' | Note that the ' | ||
+ | ## Datentypen | ||
- | ===== Datentypen ===== | ||
^ Type ^ Keyword | ^ Type ^ Keyword | ||
| character | | character | ||
Line 34: | Line 179: | ||
| double-precision floating-point (19 Stellen) | | double-precision floating-point (19 Stellen) | ||
- | ===== Zusammenfassung Tutorial | + | ## Zusammenfassung Tutorial |
- | ==== Bit, Byte ==== | + | |
- | 1 Byte = 8 Bit \\ | + | |
- | 100 MB (MegaBit) = 12,5 Megabyte (Mb) \\ | + | |
- | ==== Datentypen bei Deklaration | + | ### Bit, Byte |
+ | |||
+ | * 1 Byte = 8 Bit | ||
+ | * 100 MB (MegaBit) | ||
+ | |||
+ | ### Datentypen bei Deklaration | ||
* char, int, float, double | * char, int, float, double | ||
Line 45: | Line 192: | ||
* int = Ganzzahlen | * int = Ganzzahlen | ||
* float, double = Kommazahlen | * float, double = Kommazahlen | ||
- | Variablen am besten immer gleich mit Werten deklarieren, | ||
- | int iZahl=0; | ||
- | === Konstanten === | + | Variablen am besten immer gleich mit Werten deklarieren, |
+ | |||
+ | ### Konstanten | ||
+ | |||
+ | Der Compiler bringt eine Warnung, wenn die Variable noch einmal zugewiesen wird. '' | ||
- | const int raeder = 4; | + | ### Variablenbenennung |
- | Der Compiler bringt eine Warnung, wenn die Variable noch einmal zugewiesen wird. | + | |
- | === Variablenbenennung === | ||
* Der Name darf nur aus Buchstaben, Ziffern und dem Unterstrich _ bestehen. | * Der Name darf nur aus Buchstaben, Ziffern und dem Unterstrich _ bestehen. | ||
* Das erste Zeichen muss ein Buchstabe oder der Unterstrich _ sein. | * Das erste Zeichen muss ein Buchstabe oder der Unterstrich _ sein. | ||
Line 61: | Line 208: | ||
Üblicherweise mit Kleinschreibung beginnen und Worttrennung über Großschreibung realisieren. CamelCase | Üblicherweise mit Kleinschreibung beginnen und Worttrennung über Großschreibung realisieren. CamelCase | ||
- | ==== Inkrement | + | ### Inkrement |
<code c> | <code c> | ||
int a, b=0; | int a, b=0; | ||
Line 70: | Line 218: | ||
</ | </ | ||
- | ==== Ein- Ausgabe | + | ### Ein- Ausgabe |
Was rein geht, geht auch raus. | Was rein geht, geht auch raus. | ||
- | === Variablen einlesen | + | |
+ | #### Variablen einlesen | ||
float variable=32.5; | float variable=32.5; | ||
- | === Ausgabe | + | |
+ | #### Ausgabe | ||
printf(" | printf(" | ||
Integer kann mit double ausgegeben werden. Kommazahlen sind auch möglich. | Integer kann mit double ausgegeben werden. Kommazahlen sind auch möglich. | ||
- | === Zeichen einlesen | + | ### Zeichen einlesen |
- | == Einzelnes Zeichen | + | |
+ | #### Einzelnes Zeichen | ||
c = getchar(); | c = getchar(); | ||
- | == Zahlen einlesen | + | |
+ | #### Zahlen einlesen | ||
scanf(" | scanf(" | ||
- | Double in, double out. \\ | + | |
- | \\ | + | 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. |
- | 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 | ||
- | ==== Verzweigungen ==== | ||
- | === If Else === | ||
<code c> | <code c> | ||
int zahl=6; | int zahl=6; | ||
Line 103: | Line 261: | ||
</ | </ | ||
- | == Kurzfassung | + | #### Kurzfassung |
- | Kommt nur eine Anweisung in den if Block, so könnte man auch die geschweiften Klammern weglassen. | + | |
+ | Kommt nur eine Anweisung in den if Block, so könnte man auch die geschweiften Klammern weglassen. | ||
<code c> | <code c> | ||
int zahl=6; | int zahl=6; | ||
Line 112: | Line 272: | ||
</ | </ | ||
- | === Vergleichoperatoren | + | ### Vergleichoperatoren |
== Ist gleich | == Ist gleich | ||
!= Ist nicht gleich | != Ist nicht gleich | ||
Line 120: | Line 281: | ||
<= Kleiner gleich | <= Kleiner gleich | ||
- | === Logische Operatoren | + | ### Logische Operatoren |
<code c> | <code c> | ||
! Negation | ! Negation | ||
Line 127: | Line 289: | ||
</ | </ | ||
- | === Switch case === | + | ### Switch case |
<code c> | <code c> | ||
switch(a){ | switch(a){ | ||
Line 135: | Line 298: | ||
</ | </ | ||
- | === While Schleife | + | ### While Schleife |
<code c> | <code c> | ||
while(bedingung i < 1000){ | while(bedingung i < 1000){ | ||
Line 143: | Line 307: | ||
</ | </ | ||
- | === For Schleife | + | ### For Schleife |
<code c>int i; | <code c>int i; | ||
Line 150: | Line 315: | ||
} | } | ||
</ | </ | ||
- | === Do while === | + | |
+ | ### Do while | ||
<code c> | <code c> | ||
int alter; | int alter; | ||
Line 161: | Line 328: | ||
printf(" | printf(" | ||
</ | </ | ||
- | ==== Funktionen | + | |
+ | ## Funktionen | ||
Die Funktion muss vor dem Main positioniert werden, damit sie dann in der Main aufgerufen werden kann. | Die Funktion muss vor dem Main positioniert werden, damit sie dann in der Main aufgerufen werden kann. | ||
Line 178: | Line 347: | ||
</ | </ | ||
- | === Funktionsprototypen | + | ### Funktionsprototypen |
Diese kommen vor das Hauptprogramm, | Diese kommen vor das Hauptprogramm, | ||
+ | |||
<code c> | <code c> | ||
Line 193: | Line 364: | ||
</ | </ | ||
- | ==== Arrays | + | ### Arrays |
Arrays sind einzeilige Matrixen um viele Daten speichern zu können. Zugegriffen wird auf die einzelnen Werte mit dem Index. | Arrays sind einzeilige Matrixen um viele Daten speichern zu können. Zugegriffen wird auf die einzelnen Werte mit dem Index. | ||
- | === Schleifen | + | ### Schleifen |
Setzen von Werten mit Benutzereingabe | Setzen von Werten mit Benutzereingabe | ||
+ | |||
<code c> | <code c> | ||
Line 213: | Line 387: | ||
</ | </ | ||
- | === Initialisierung | + | ### Initialisierung |
Dazu die Werte eines Feldes einfach in geschweifte Klammern schreiben. Ist die Anzahl der Werte kleiner als die Feldgröße, | Dazu die Werte eines Feldes einfach in geschweifte Klammern schreiben. Ist die Anzahl der Werte kleiner als die Feldgröße, | ||
<code c> | <code c> |