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/06/06 00:01] superadmin |
— (current) | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== C ====== | ||
- | Zusammenfassung für nützliche Sachen beim Programmieren mit C. | ||
- | ===== 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, | ||
- | int iZahl=0; | ||
- | |||
- | === Konstanten === | ||
- | const int raeder = 4; | ||
- | 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. | ||
- | |||
- | ==== Inkrement ==== | ||
- | 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 === | ||
- | 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. \\ | ||
- | |||
- | 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 === | ||
- | ! Negation | ||
- | && UND | ||
- | !! ODER | ||
- | |||
- | === Switch case === | ||
- | switch(a){ | ||
- | case 1: printf(" | ||
- | case 2: printf(" | ||
- | } | ||
- | |||
- | === While Schleife === | ||
- | while(bedingung i < 1000){ | ||
- | printf(" | ||
- | i++; | ||
- | } | ||
- | |||
- | === For Schleife === | ||
- | int i; | ||
- | |||
- | for(i=0; i<5; i++) { | ||
- | printf(" | ||
- | } | ||
- | |||
- | === Do while === | ||
- | int alter; | ||
- | |||
- | do { | ||
- | printf(" | ||
- | scanf(" | ||
- | } while(alter < 5 || alter > 100); | ||
- | |||
- | printf(" | ||
- | |||
- | ==== Funktionen ==== | ||
- | |||