C

From XennisWiki
Jump to: navigation, search

C is a general-purpose, imperative computer programming language, supporting structured programming, lexical variable scope and recursion, while a static type system prevents many unintended operations. By design, C provides constructs that map efficiently to typical machine instructions, and therefore it has found lasting use in applications that had formerly been coded in assembly language, including operating systems, as well as various application software for computers ranging from supercomputers to embedded systems. (Wikipedia)

Beispielprogramm

HelloWorld.c

#include <stdio.h>

int main(int argc, char** argv)
{
	printf("Hello world\n");
	return 0;
}

Präprozessor

#include

Sucht im Compiler Standardpfad

#include <file>

Sucht relativ zum akutellen Pfad

#include "file"

#define

#define BUFFER_SIZE 1020
#define RADTODEG(x) x * 57.295736
/* Ändert test(...) zu TEST(...) */
#define test TEST
#define DEBUGLEVEL 4
#if (DEBUGLEVEL > 4)
	<many debugging code>
#elif (DEBUGLEVEL <= 4)
	<debugging code>
#else
	<something else>
#endif
/* #ifdef ist die Kurzform von #if defined und
   #ifndef ist die von #if !defined */
#define DEBUGLEVEL
#ifdef DEBUGLEVEL
	<debugging code for all debug levels>
#endif
/* Headerdatei: foo/bar/stuff.h */
#ifndef __FOO_BAR_STUFF_H__
	#define __FOO_BAR_STUFF_H__
	<content of the header file>
#endif

Syntax

Kommentar

/* MEIN KOMMENTAR */

Operatoren

  • Logische Operatoren
    • &&
    • ||
    •  !
  • Vergleichende Operatoren
    • ==
    •  !=
    • < und <=
    • > und >=
  • Bit Manipulation
    • & (bitweises Und)
    • | (bitweises Oder)
    • << und >> (binärer Links- bzw. Rechtsshift)
    • ^ (XOR)
    • ~ (bitweises Komplement)

Bedingungsoperator

<Bedingung> ? <Ausdruck1> : </Ausdruck2>
return a > b ? a : b;

Funktionen

int add( int x, int y) { return x+y; }

Datentypen

  • void
  • int (signed und unsigned)
  • float (signed und unsigned)
  • double (signed und unsigned)
  • char (signed und unsigned)

2E3 entspricht 2 * 10 ^ 3 = 2000

Octal

0454

Hexadezimal

0xABCD oder 0XABCD

Cast

int i = (int)5.6; /* -> i = 5 */

Abgleitete Typen

Enumeration

enum Color { RED, ORANGE, YELLOW, BLUE };
enum Color color1 = RED, color2 = GREEN;

Arrays

int p[5] = { 2, 3, 7, 11 }, a[4];
p[4] = 12;

Struct

struct Point {int x; int y; };
struct Point p;
p.x = 10;
p.y = 20;
struct Bitfield {
	unsigned int day : 5;
	unsigned int month : 4;
	unsigned int year : 11;
} birthday;
birthday.day = 23; birthday.month = 12; birthday.year = 1976;

Union

union AorB { int a; float b; };
union AorB ab;
ab.a = 10;
ab.b = 2.0;

Statements

Block

#include <stdio.h>
int main(int argc, char** argv)
{
	printf("Hello world\n");
	{
		int I;
		printf(“I am inside a new block\n”);
		printf(“containing multiple statements\n”);
	}
	return 0;
}

Selection

If-Bedingung

if (<expression>)<statement>

If-Else-Bedingung

if (<expression>)<statement> else<statement>

Switch-Anweisung

switch( some_character_variable )
{
	case „a„: case „A„: function_a(); break;
	case „b„: case „B„: function_b(); break;
	default: warning(“Invalid choice\n“);
}

While-Schleife

while (<expression>) <statement>;

Do-While-Schleife

do <statement> while (<expression>);

For-Schleife

for (<statement>; <expression>; <statement>) <statement>

Labels

int main(int argc, char** argv) {
	beginning:printf(“infinite loop\n“);
	goto beginning;return 0;
}

Jump

  • continue
  • break
  • return <Ausdruck>

Semantik

typedef

typedef char* String;
String s = “text“;
typedef enum {FALSE=0, TRUE} Boolean; 
Boolean b = FALSE;
b = TRUE;
typedef struct Person{int age; char name[100];} Person;

sizeof

sizeof(int)
struct TwoInts { int a; int b; };
sizeof(struct TwoInts)

Pointer

  • * (Dereferenzieren)
  • & (Adresse)
  • -> (Dereferenzieren für Elementzugriff)

Definition

<data type>*<identifier>;
int*i, *i2;
void *v1;

Belegung

&<identifier>

*ptr wird die Adresse (nicht der Wert) von i zugewiesen

int i = 123;
int *ptr = &i;

Dereferenzieren

*<identifier>

*ptr = 124 bewirkt dasselbe wie i = 124

int i = 123;
int *ptr = &i;
*ptr = 124;

p->x = 3; ist die Kurzform von (*p).x = 3;

struct Point p = { 1,2 }, *ptr = &p;
p->x = 3;
int p[10]
int *array_ptr = p;
int *array_ptr = & p[3];

Operationen auf Pointer anwenden

inti= 1, *p = &i;
p++;      /* p = p + sizeof(int) */
p--;      /* p = p - sizeof(int) */
P += 3;   /* p = p + 3 * sizeof(int) */

Speichermanagement

malloc

Speicher reservieren

free

Speicher freigeben

calloc

Reserviert Speicher und initialisiert den Inhalt mit Null.

Regel: Speicher der reserviert wurde, muss auch wieder freigegeben werden.

Beispiel:

void* a1 = malloc(1000);
void* a2 = malloc(2000);
free(b1);
free(b2);

See also