C

Page content

some stuff i learned about the “C” Language

-> https://www.c-howto.de/tutorial/einfuehrung/

DataTypes

Char

to Store one single Character

#include<stdio.h>

int main() {
	char zeichen1;
	char zeichen2;
	zeichen1 = 'A';
	zeichen2 = 66;
	printf("Zeichen: %c, %c\n", zeichen1, zeichen2 );
	return 0;
}
Zeichen: A, B

Short / unsigned short

#include<stdio.h>

int main() {
// kleine Zahl mit Vorzeichen deklarieren (erstellen)
short kleineZahl;

// kleine Zahl auf Grenzwert setzen
kleineZahl = 32767;

// kleineZahl ausgeben
printf("Wert von kleineZahl: %d\n", kleineZahl);

// eins hochzählen, also 32767+1, was eigentlich 32768 ergibt
kleineZahl++;

// kleineZahl ausgeben
printf("Wert von kleineZahl: %d\n", kleineZahl);

return 0;
}
Wert von kleineZahl: 32767
Wert von kleineZahl: -32768

int

16 or 32 Bit, depending on Architectur

signed:   +/- 2.147.483.647
unsigned: 0 - 4.294.967.295

long / longlong

32 or 64 Bit

signed:     +/- 9.223.372.036.854.775.807
unsigned:   0 - 18.446.744.073.709.551.615
#include<stdio.h>

int main() {
	// Deklaration von int Variable 
	int zahl;
	
	// Wert-Zuweisung
	zahl = -7;

	// Deklaration von int Variable ohne Vorzeichen
	unsigned int zahl2;

	// Deklaration von long long Variable ohne Vorzeichen
	unsigned long long grosseZahl;

	return 0;
}

Float

#include<stdio.h>

int main() {
	// Deklaration von normalgroßer Kommazahl
	float kommazahl;

	// Wert zuweisen
	kommazahl = 12.3456;

	// Ausgabe
	printf("Kommazahl: %f\n", kommazahl);
	
	return 0;
}
Kommazahl: 12.3456

Sizes

// Dieses Programm gibt die Größen der Datentypen in Bit aus

#include<stdio.h>

int main() {
	printf("short:        %zu Bit\n", sizeof(short)*8);
	printf("int:          %zu Bit\n", sizeof(int)*8);
	printf("long:         %zu Bit\n", sizeof(long)*8);
	printf("long long:    %zu Bit\n", sizeof(long long)*8);
	printf("float:        %zu Bit\n", sizeof(float)*8);
	printf("double:       %zu Bit\n", sizeof(double)*8);
	printf("long double:  %zu Bit\n", sizeof(long double)*8);
	return 0;
}

OpenBSD 7.0 amd64

short:        16 Bit
int:          32 Bit
long:         64 Bit
long long:    64 Bit
float:        32 Bit
double:       64 Bit
long double:  128 Bit

Variable / Const

Letters, Numbers, Underscore First Char is Letter or Underscore Case Sensitive ! no Key Words

int a, b, c;
int a=1, b=2, c=a+b;
const int raeder = 4;
float preisKartoffeln = 0.89;

Increment

int a=0, b=0;

// a mit Inkrement-Operator hochzählen
a++;

// b ohne Inkrement-Operator hochzählen
b = b + 1;

// a dekrementieren, a ist wieder 0
a--;

// b ohne Inkrement-Operator dekrementieren
b = b - 1;

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;

Basic Math

int a=0, b=2, c=5;

a = b + c;  // a ist 7
a = b - c;  // a ist -3
a = c / b;  // a ist 2
a = c * b;  // a ist 10

// Rest aus Division berechnen
a = c % b;  // 5 / 2 ist 2 Rest 1, a ist 1
a = c % 3;  // 5 / 3 ist 1 Rest 2, a ist 2

// Prioritäten mit Klammern setzen
a = 1 + b * c;    // Punkt vor Strich, a ist 11
a = (1 + b) * c;  // 1+2 ist 3, 3*5 ist 15, a ist 15

int a=1, b=2;

a += 1;      // wie a=a+1 oder a++, a ist 2
a += b * 4;  // a ist 10
a /= 2;      // a ist 5
a %= 2;      // a ist 1

Bit Manipulation

And

int a=10, b=7, c;
c = a & b;
printf("c: %d\n", c);
c: 2

Zugehörige Rechnung der UND-Verknüpfung

a: 10 dez => 1010 binär
b:  7 dez => 0111 binär

      1010
 UND  0111
-----------
      0010 

c: 10 binär => 2 dezimal

Or

int a=10, b=6, c;
c = a | b;
printf("c: %d\n", c);
c: 14

Zugehörige Rechnung der ODER-Verknüpfung

a: 10 dez => 1010 binär
b:  6 dez => 0110 binär

      1010
 OR   0110
-----------
      1110 

c: 1110 binär => 14 dezimal

Xor

int a=10, b=6, c;
c = a ^ b;
printf("c: %d\n", c);
c: 12

Zugehörige Rechnung der XOR-Verknüpfung

      1010
XOR   0110
-----------
      1100 

c: 1100 binär => 12 dezimal

Negation

int b=6;
char str[100];
printf("b binaer: %s\n",  itoa(b, str, 2));
printf("b dezimal: %d\n", b);
b = ~b;
printf("b binaer: %s\n",  itoa(b, str, 2));
printf("b dezimal: %d\n", b);
b++;
printf("b binaer: %s\n",  itoa(b, str, 2));
printf("b dezimal: %d\n", b);
b binaer: 110
b dezimal: 6
b binaer: 11111111111111111111111111111001
b dezimal: -7
b binaer: 11111111111111111111111111111010
b dezimal: -6

Bit Shift

int b=5, c, d;
c = b << 1;
d = b >> 1;
printf("c: %d, d: %d\n", c, d);
c: 10, d: 2

b:      5 dez => 0101 binär

c:  0101 << 1 => 1010 binär => 10 dezimal
d:  0101 >> 1 => 0010 binär =>  2 dezimal

TypeCast / Cast / Casting

int i;

char c = 'A';
i = (int)c;
printf("char c nach int i: %d\n", i);

i = 67;
c = (char)i;
printf("int i nach char c: %c\n", i);

float f = 2.345;
i = (int)f;
printf("float f nach int i: %d\n", i);
char c nach int i: 65
int i nach char c: C
float f nach int i: 2

Output

Format

%c  Character
%s  String (character array)
%i  Integer
%li Long
%o  Octal representation
%x  Hexadecimal representation
%f  Float
%lf Double
%e  Scientific notation
%a  Binary hexadecimal representation %% Literal % character

String

printf("Hello World");
Hello World

Character

char alphabetEnde = 'Z';
printf("Das Alphabet endet mit: %c\n", alphabetEnde);
Das Alphabet endet mit: Z

Numbers

int laenge=312500, breite=50;
printf("\nLaenge: %d cm\nBreite: %d cm\n", laenge, breite);
printf("\nLaenge: %10d cm\nBreite: %10d cm\n", laenge, breite);
Laenge: 312500 cm
Breite: 50 cm

Laenge:     312500 cm
Breite:         50 cm

Float

float laenge=312.5789, breite=5.6;
printf("\nLaenge: %f cm\nBreite: %f cm\n", laenge, breite);
printf("\nLaenge: %10.2f cm\nBreite: %10.2f cm\n", laenge, breite);
Laenge: 312.578888 cm
Breite: 5.600000 cm

Laenge:     312.58 cm
Breite:       5.60 cm

Formated

int tag=3, monat=9, jahr=2007;
printf("Ein Datum: %02d.%02d.%4d\n", tag, monat, jahr);
Ein Datum: 03.09.2007

Input

Char

char c;
printf("Mit welchem Buchstaben beginnt ihr Vorname? ");
c = getchar();
printf("\nIch weiss jetzt, dass Ihr Vorname mit '%c' beginnt.\n", c);
Mit welchem Buchstaben beginnt ihr Vorname? K

Ich weiss jetzt, dass Ihr Vorname mit 'K' beginnt.

Numbers

int alter;
printf("Wie alt sind sie? ");
scanf("%d", &alter);
printf("\nIn %d Jahren sind Sie 100!\n", 100-alter);
Wie alt sind sie? 33
In 67 Jahren sind Sie 100!

Formated

int tag, monat, jahr;
printf("Bitte geben Sie ihr Geburtsdatum ein [TT.MM.JJJJ]: ");
scanf("%d.%d.%d", &tag, &monat, &jahr);
printf("\nIhr internationales Geburtsdatum: %04d-%02d-%02d\n", jahr, monat, tag);
Bitte geben Sie ihr Geburtsdatum ein [TT.MM.JJJJ]: 11.12.1980

Ihr internationales Geburtsdatum: 1980-12-11

MultiLines

char a, b, temp;

printf("\nGeben sie ein Zeichen ein: ");
scanf("%c%c", &a, &temp);

printf("\nGeben sie ein Zeichen ein: ");
scanf("%c%c", &b, &temp);

printf("\nDie ASCII-Codes ihrer Zeichen sind %d und %d\n", a, b);
Geben sie ein Zeichen ein: a
Geben sie ein Zeichen ein: b
Die ASCII-Codes ihrer Zeichen sind 97 und 98

Loops

IF - ELSE

int einwurf=0;
printf("Getraenke Automat | Bitte werfen sie 1 Euro ein: ");
scanf("%d", &einwurf);

// überpruefe Geldstueck
if(einwurf == 1) {
	printf("\nVielen Dank, bitte entnehmen sie ihr Getraenk.\n");
}else {
	printf("\nSie haben kein 1 Euro Stueck eingeworfen.\n");
}
Getraenke Automat | Bitte werfen sie 1 Euro ein: 1

Vielen Dank, bitte entnehmen sie ihr Getraenk.

IF - ELSE - ELSE

int zahl=6;

if(zahl==5) {
	printf("fuenf\n");
}else {
    if(zahl==6) {
        printf("sechs\n");
    }else {
    	printf("nicht fuenf und nicht sechs\n");
	}
}
sechs

comparison operators

if(1) printf("1 ist wahr\n");
if(0) printf("0 ist wahr\n");
if(4711) printf("4711 ist wahr\n");
if(1-1) printf("1-1 ist wahr\n");
1 ist wahr
4711 ist wahr

Equal

int a=5;
if(a == 5) printf("a ist fuenf\n");
if(a != 5) printf("a ist nicht fuenf\n");
a ist fuenf

Greater - Greater Equal

int a=5;
if(a > 5) printf("a ist groesser fuenf\n");
if(a >= 5) printf("a ist fuenf oder groesser fuenf\n");
a ist fuenf oder groesser fuenf

Less - Less Equal

int a=5;
if(a < 5) printf("a ist kleiner fuenf\n");
if(a <= 5) printf("a ist fuenf oder kleiner fuenf\n");
a ist fuenf oder kleiner fuenf

conditions

NOT

int a=5;
if(!0) printf("aus falsch wird wahr, 0 -> 1\n");
if(!a) printf("aus wahr wird falsch, 5 -> 0\n");
aus falsch wird wahr, 0 -> 1

AND

int a=0, b=3;

if(!a && b > 1) {
	printf("a ist nicht wahr und b ist groesser 1\n");
}
a ist nicht wahr und b ist groesser 1

OR

int a=0, b=1;

if(a || b) {
	printf("a oder b ist wahr\n");
}
a oder b ist wahr

Short Version

int a=3, b=5, max;
if(a > b) {
	max = a;
}else {
	max = b;
}
printf("Maximum: %d\n", max);

---

int a=3, b=5, max;
max = (a > b)?a:b;
printf("Maximum: %d\n", max);
Maximum: 5

Switch Case

int a=2;

switch(a) {
	case 1: printf("a ist eins\n"); break;
	case 2: printf("a ist zwei\n"); break;
	case 3: printf("a ist drei\n"); break;
	default: printf("a ist irgendwas\n"); break;
}
a ist zwei

Loops

https://www.c-howto.de/tutorial/schleifen/while-schleife/

Functions

https://www.c-howto.de/tutorial/funktionen/

#include<stdio.h>

int addiere(int summand1, int summand2) {
	return (summand1 + summand2);
}

int main() {
	int summe = addiere(3, 7);
	printf("Summe von 3 und 7 ist %d\n", summe);
	return 0;
}
Summe von 3 und 7 ist 10

Pointers

Function

How to Call a Function with Pointers only

#include<stdio.h>

void addiere(int *summand1, int *summand2, int *summe) {
	*summe = *summand1 + *summand2;
}

int main() {
	int zahl1 = 666666666;
	int zahl2 = 333333333;
	int summe;
	
	addiere(&zahl1, &zahl2, &summe);
	
	printf("Summe von zahl1 und zahl2: %d\n", summe);
	return 0;
}
Summe von zahl1 und zahl2: 999999999

Example

int zahl=7;
int *zeiger = &zahl;

printf("Adresse zahl: %p\n", &zahl);
printf("Wert zahl: %d\n\n", zahl);
printf("Adresse zeiger: %p\n", &zeiger);
printf("Wert zeiger: %p\n", zeiger);
printf("Wert von Adresse %p: %d\n", zeiger, *zeiger);
Adresse zahl: 0xbfefbcf0
Wert zahl: 7

Adresse zeiger: 0xbfefbcec
Wert zeiger: 0xbfefbcf0
Wert von Adresse 0xbfefbcf0: 7

Map.c

https://github.com/soveran/map/blob/master/map.c

/*
 * Copyright (c) 2018, Michel Martens <mail at soveran dot com>
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *  *  Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *  *  Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {

	if (argc != 3) {
		printf("usage: %s <variable> <command>\n", argv[0]);
		exit(1);
	}

	char   *line = NULL;
	size_t  lcap = 0;
	ssize_t llen;

	while ((llen = getline(&line, &lcap, stdin)) > 0) {
		if (llen > 1) {
			if (line[llen-1] == '\n') {
				line[llen-1] = '\0';
			}

			setenv(argv[1], line, 1);
			system(argv[2]);
		}
	}

	return 0;
}

Any Comments ?

sha256: 759be67779ae9efcbe8f136f162bc57d8ebc338bd04175b26c8733b8cc2c84e1