C tutor
Typedef mit Struct und Funktionen
Code #include <stdio.h> /* structs and functions Written by Matthias Fechner */ typedef struct { int ktnr; int blz; char *name; float betrag; } konto; void ausgabe(konto); int main(void) { konto matthias,willy; matthias.ktnr=1122; matthias.blz=98721; matthias.name="Matthias"; matthias.betrag=2.35; willy=matthias; willy.name="Willy"; willy.betrag++; willy.ktnr=7643; ausgabe(matthias); ausgabe(willy); return 0; } void ausgabe(konto daten) { printf("Name=%s\n",daten.
C tutor
Zeiger bei Funktionsaufruf
Code #include <stdio.h> /* programm to see who a pointer work with functions Written by Matthias Fechner */ void callByValue(int); void callByReference(int*); int main(void) { int i; i=1; printf("i=%d\n",i); callByValue(i); printf("i=%d\n",i); callByReference(&i); printf("i=%d\n",i); return 1; } void callByValue(int i) { printf("i=%d\n",i); i=8; printf("i=%d\n",i); return; } void callByReference(int *i) { printf("i=%d\n",*i); *i=13; printf("i=%d\n",*i); return; } Result ~/c_tutor> gcc pointer_func.
C tutor
Break und Continue
Code #include <stdio.h>/* Programm to use break and continue to break loops */ int main(void) { int i,j; for(i=0;i<10;i++) { if(i>3) continue; printf("i=%d\n",i); } for(j=0;j<3;j++) { printf("j=%d\n",j); for(i=0;i<100;i++) { if(i>1) break; printf("i=%d\n",i); } } return 1; } Result ~/c_tutor> gcc break_continue.c idefix@idefix_mobil ~/c_tutor> ./a.out idefix@idefix_mobil i=0 i=1 i=2 i=3 j=0 i=0 i=1 j=1 i=0 i=1 j=2 i=0 i=1 Source File Output
C tutor
Zeiger allgemein
Code #include <stdio.h> /* programm to see who a pointer works Written by Matthias Fechner */ int main(void) { int i,*ptr; i=3; ptr=&i; printf("i=%d\n",i); printf("ptr=0x%X\n",ptr); printf("*ptr=%d\n",*ptr); i=5; printf("i=%d\n",i); printf("ptr=0x%X\n",ptr); printf("*ptr=%d\n",*ptr); *ptr=8; printf("i=%d\n",i); return 1; } Result ~/c_tutor> gcc pointer.c idefix@idefix_mobil ~/c_tutor> ./a.out idefix@idefix_mobil i=3 ptr=0xBFFFF8E4 *ptr=3 i=5 ptr=0xBFFFF8E4 *ptr=5 i=8 Source File Output
C tutor
Gültigkeit von Variablen
Code #include <stdio.h> /* Gueltigkeit von Variablen Matthias Fechner tutor@fechner.net 21.04.2004 */ void func1(int); int func2(int); int main(void) { int i=10,j; printf("i=%d\n",i); func1(i); printf("i=%d\n",i); i=5; printf("i=%d\n",i); i=func2(i); printf("i=%d\n",i); for(j=0;j<3;j++) { int i=2; printf("i=%d\n",i); } printf("i=%d\n",i); } void func1(int i) { printf("i=%d\n",i); i=100; printf("i=%d\n",i); return; } int func2(int i) { printf("i=%d\n",i); return(55); } Result ~/c_tutor> gcc gueltigkeit.