meta data for this page
  •  

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

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://github.com/kozross/awesome-c#learning-reference-and-tutorials
   * https://de.wikibooks.org/wiki/C-Programmierung   * https://de.wikibooks.org/wiki/C-Programmierung
   * http://www.code-in-c.com/galton-board-in-c/   * http://www.code-in-c.com/galton-board-in-c/
   * http://www.c-howto.de/tutorial/einfuehrung/   * http://www.c-howto.de/tutorial/einfuehrung/
 +  * http://www.vazgames.com/retro/CPROG.htm
 +  * http://ergodic.ugr.es/cphys_pedro/c/c/ccourse.html
 +  * ALGORITHMS http://www-igm.univ-mlv.fr/~lecroq/string/index.html
 +  * Propositional Logic: Introduction https://www.youtube.com/watch?v=qV4htTfow-E&list=PL619166130C21EADA
 +  * https://www.gnu.org/software/libc/manual/html_node/Getopt.html
 +  * Allgemein, MultiOS-Gamedev: https://gist.github.com/flibitijibibo/b67910842ab95bb3decdf89d1502de88
 +  * https://www.reddit.com/r/C_Programming/comments/cep9zl/c_skill_tree_visual_guide_for_c_resources/
 +  * Structure my program https://www.reddit.com/r/C_Programming/comments/cadkxr/c_programming_beginner_needs_help/?sort=top
 +  * https://www.reddit.com/r/C_Programming/comments/b3f9n5/i_created_a_simple_tictactoe_game_and_am_looking/?sort=top
 +  * http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html
 +  * http://rosettacode.org/wiki/Category:Programming_Tasks
 +
 +### Starting a project, planning
 +
 +  * https://www.quora.com/How-do-I-start-my-own-programming-projects-1
 +  * https://www.codeconquest.com/programming-projects/
 +  * https://www.khanacademy.org/computing/computer-programming/programming/good-practices/a/planning-a-programming-project
 +
 +### Games
 +
 +  * https://www.codingame.com/home
 +  * https://www.coderbyte.com/
 +  * https://www.reddit.com/r/C_Programming/comments/c3ap6f/i_just_started_a_new_project_on_github_with_code/?sort=top
 +  * https://projecteuler.net/
 +  * https://www.reddit.com/r/dailyprogrammer/
 +  * https://www.hackerrank.com/
 +  * paid? - https://www.topcoder.com/
 +
 +## Cheat Sheet
 +
 +### Main Function
 +
 +<code c>
 +void main(void) {
 +
 +}
 +</code>
 +
 +### Printf
 +
 +<code c>
 +#include <stdio.h>
 +
 +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 = %d, u = %u, l = %l, f = %f, d = %lf, c = %c, c = %d, uc = %d\n",
 +            i, u, l, f, d, c, c, uc);
 +
 +    printf("print a tab by \t and new line by \n");
 +
 +}
 +</code>
 +
 +### Scanf
 +
 +<code c>
 +#include <stdio.h>
 +
 +void main(void) {
 +   int i;
 +
 +   printf("Print a prompt for i\n");
 +   scanf("%d", &i);
 +
 +}
 +</code>
 +
 +### 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
 +    break;
 +    case 1:  // statements
 +  break;
 +    case 2:  // statements
 +  break;
 +    default:  // statements
 +
 +}
 +</code>
 +
 +### 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
 +}
 +</code>
 +
 +### Math Functions
 +
 +<code c>
 +#include <math.h>
 +
 +void main(void) {
 +    double th = pi/2;    // th is in radians
 +    double x, y;
 +
 +    x = cos(th);
 +    y = sin(th);
 +    th = atan2(y, x);
 +}
 +</code>
 +
 +### 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);
 +}
 +</code>
 +
 +## Input seperated with space or /
 +
 +  * https://stackoverflow.com/questions/15330047/understanding-scanf-dealing-with-formatted-input
  
-===== Input seperated with space or / ===== 
-https://stackoverflow.com/questions/15330047/understanding-scanf-dealing-with-formatted-input \\ 
 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:
 +
 <code> <code>
 if (scanf("%[^ /]%*1[ /]%d%*1[ /]%f", name, &age, &wage) == 3) if (scanf("%[^ /]%*1[ /]%d%*1[ /]%f", name, &age, &wage) == 3)
Line 16: Line 160:
     ...something went wrong...     ...something went wrong...
 </code> </code>
 +
 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 'something went wrong' part has a difficult time reporting the error coherently to the user. This is why many people, myself included, recommend against using scanf() or fscanf() and prefer to use fgets() or perhaps POSIX getline to read a line from the user and then use sscanf() to analyze it. You can report the problems much more easily. Also note that the return value from scanf() is the number of successful assignments; it does not count the conversion specifications that include *. Note that the 'something went wrong' part has a difficult time reporting the error coherently to the user. This is why many people, myself included, recommend against using scanf() or fscanf() and prefer to use fgets() or perhaps POSIX getline to read a line from the user and then use sscanf() to analyze it. You can report the problems much more easily. Also note that the return value from scanf() is the number of successful assignments; it does not count the conversion specifications that include *.
  
 +##  Datentypen
  
-===== Datentypen ===== 
 ^ Type                                          ^ Keyword         | Bytes  | Range                               | ^ Type                                          ^ Keyword         | Bytes  | Range                               |
 | character                                     | char            | 1      | -128 .. 127                       | | character                                     | char            | 1      | -128 .. 127                       |
Line 34: Line 179:
 | double-precision floating-point (19 Stellen)  | double          | 8      | 2.2E-308 .. 1.8E308               | | double-precision floating-point (19 Stellen)  | double          | 8      | 2.2E-308 .. 1.8E308               |
  
-===== 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) 12,5 Megabyte (Mb) 
 + 
 +### 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, damit keine Zufallswerte verwendet werden. 
-    int iZahl=0; 
  
-=== Konstanten ===+Variablen am besten immer gleich mit Werten deklarieren, damit keine Zufallswerte verwendet werden. ''int iZahl=0;'' 
 + 
 +### Konstanten  
 + 
 +Der Compiler bringt eine Warnung, wenn die Variable noch einmal zugewiesen wird. ''const int raeder 4;''
  
-    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:
 </code> </code>
  
-==== 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("Laenge: %5.2f", variable);     printf("Laenge: %5.2f", variable);
  
 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("%d",&alter);     scanf("%d",&alter);
-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:
 </code> </code>
  
-== 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:
 </code> </code>
  
-=== 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:
 </code> </code>
  
-=== Switch case ===+### Switch case 
 <code c> <code c>
   switch(a){   switch(a){
Line 135: Line 298:
 </code> </code>
  
-=== While Schleife ===+### While Schleife 
 <code c> <code c>
 while(bedingung i < 1000){ while(bedingung i < 1000){
Line 143: Line 307:
 </code> </code>
  
-=== For Schleife ===+### For Schleife 
 <code c>int i; <code c>int i;
  
Line 150: Line 315:
   }   }
 </code> </code>
-=== Do while ===+ 
 +### Do while 
 <code c> <code c>
 int alter; int alter;
Line 161: Line 328:
   printf("Danke.\n");   printf("Danke.\n");
 </code> </code>
-==== 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:
 </code> </code>
  
-=== Funktionsprototypen ===+### Funktionsprototypen 
 Diese kommen vor das Hauptprogramm, wobei der Funktionskörper dann an einer beliebigen Stelle im Script sein darf. Diese kommen vor das Hauptprogramm, wobei der Funktionskörper dann an einer beliebigen Stelle im Script sein darf.
 +
 <code c> <code c>
  
Line 193: Line 364:
 </code> </code>
  
-==== 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:
 </code> </code>
  
-=== Initialisierung ===+### Initialisierung 
 Dazu die Werte eines Feldes einfach in geschweifte Klammern schreiben. Ist die Anzahl der Werte kleiner als die Feldgröße, werden die restlichen Werte auf 0 gesetzt. Ohne Größenangabe wird das Array die Größe durch die Anzahl der Initialisierungswerte bestimmt. Dazu die Werte eines Feldes einfach in geschweifte Klammern schreiben. Ist die Anzahl der Werte kleiner als die Feldgröße, werden die restlichen Werte auf 0 gesetzt. Ohne Größenangabe wird das Array die Größe durch die Anzahl der Initialisierungswerte bestimmt.
 <code c> <code c>