Está en la página 1de 30

Einfhrung in die Programmierung Vorlesung 2 W I S S E N 1

T E C H N I K

L E I D E N S C H A F T

Einfhrung in die Programmierung


DI Dr. Christian Safran, IICM DI Michael Steurer, IICM Univ.-Prof. DI Dr. techn. Frank Kappe, IICM

www.tugraz.at

Einfhrung in die Programmierung Vorlesung 2 2

Inhalt Teil 2:

Variablen Die Funktion printf Operatoren

DI Dr. Christian Safran, IICM

Einfhrung in die Programmierung Vorlesung 2 3

Variablen
Symbolische Namen fr Speicherstellen
"! "! "! "! "! Mssen vor Verwendung definiert werden Namen beliebig lang, bestehend aus Buchstaben, Ziffern und _ Drfen nicht mit Ziffer beginnen Keine reservierten Wrter (z.B. if, while, short, return, !) Case-sensitiv! (counter " Counter)

Inhalt kann verndert werden

DI Dr. Christian Safran, IICM

Einfhrung in die Programmierung Vorlesung 2 4

Datentypen
Verschiedene Datentypen
"! Bestimmen Speicherbedarf "! Bestimmen Verhalten von Operationen auf diesen Daten

int ganze Zahl (natrliche Gre) float Gleitkomma-Zahl (einfache Genauigkeit) double Gleitkomma-Zahl (doppelte Genauigkeit) char Zeichen (character) void keine (oder beliebige) Daten
DI Dr. Christian Safran, IICM

Einfhrung in die Programmierung Vorlesung 2 5

Modifier
signed unsigned short long Z.B.:
"! unsigned char, unsigned long int, short int, ...

signed ist default (d.h. kann weggelassen werden) int kann auch weggelassen werden:
"! unsigned, unsigned long, short
DI Dr. Christian Safran, IICM

Einfhrung in die Programmierung Vorlesung 2 6

Problem: wie gro ist natrliche Gre?


frher (16-bit Prozessoren) char short int long float double 8 bits 16 bits 16 bits 32 bits 32 bits 64 bits 32-bit Prozessoren char short int long long long float double 8 bits 16 bits 32 bits 32 bits 64 bits 32 bits 64 bits

long double 80-128 bits


DI Dr. Christian Safran, IICM

Einfhrung in die Programmierung Vorlesung 2 7

Problem: wie gro ist natrliche Gre?


64 bit Windows char short int long long long 8 bits 16 bits 32 bits 32 bits 64 bits 64 bit Linux / Mac OS char short int long long long 8 bits 16 bits 32 bits 64 bits 64 bits

!! Lsung:

sizeof Operator

DI Dr. Christian Safran, IICM

Einfhrung in die Programmierung Vorlesung 2 8

Programm test_types.c
1 2 3 4 5 6 7 8 9 10 11 12 // test_types.c - prints the size of primitive C datatypes #include <stdio.h> char a_char = '\0'; int an_int = 0; short a_short = 0; long a_long = 0; long long a_long_long = 0; float a_float = 0.0f; double a_double = 0.0; long double a_long_double = 0.0;

DI Dr. Christian Safran, IICM

Einfhrung in die Programmierung Vorlesung 2 9

Programm test_types.c
13 int main() 14 { 15 printf("Size 16 printf("Size 17 printf("Size 18 printf("Size 19 printf("Size 20 printf("Size 21 printf("Size 22 printf("Size a_long_double); 23 return 0; 24 }

of of of of of of of of

char: int: short: long: long long: float: double: long double:

%2lu %2lu %2lu %2lu %2lu %2lu %2lu %2lu

Byte(s)\n", Byte(s)\n", Byte(s)\n", Byte(s)\n", Byte(s)\n", Byte(s)\n", Byte(s)\n", Byte(s)\n",

sizeof sizeof sizeof sizeof sizeof sizeof sizeof sizeof

a_char); an_int); a_short); a_long); a_long_long); a_float); a_double);

DI Dr. Christian Safran, IICM

Einfhrung in die Programmierung Vorlesung 2 10

Output (Max OS)


Size Size Size Size Size Size Size Size of of of of of of of of char: 1 int: 4 short: 2 long: 8 long long: 8 float: 4 double: 8 long double: 12 Byte(s) Byte(s) Byte(s) Byte(s) Byte(s) Byte(s) Byte(s) Byte(s)

DI Dr. Christian Safran, IICM

Einfhrung in die Programmierung Vorlesung 2 11

Die Funktion printf


Polymorphe Funktion mit beliebig vielen Parametern 1. Parameter ist sog. format string mit Platzhaltern Wichtigste Platzhalter:
"! "! "! "! "! "! "! "! %d %u %o %x %f %c %s %% Dezimaldarstellung (signed) Dezimaldarstellung (unsigned) Oktal (unsigned) Hexadezimal (unsigned) float oder double char (Zeichen) string (Zeichenkette) %-Zeichen

Achtung: Zahl und Typ der Parameter muss mit format string bereinstimmen (Compiler bemerkt hier keine Fehler) ! Weitere Optionen (width, precision, ...): man 3 printf
DI Dr. Christian Safran, IICM

Einfhrung in die Programmierung Vorlesung 2 12

Wahrheitswerte in C
Keine boolschen Variablen Interpretation von Integervariablen
"! 0 .. False "! > 0 .. true

DI Dr. Christian Safran, IICM

Einfhrung in die Programmierung Vorlesung 2 13

Der Zuweisungsoperator =
Links von = steht immer eine Variable, der der Wert des Ausdrucks rechts von = zugewiesen wird Beispiele:
int sum, counter; sum = counter; counter = sum = 0; // uninitialized variable !

int sum = 0; float radius = 0.5; char newline = '\n';

Variablen mglichst gleich initialisieren


DI Dr. Christian Safran, IICM

Einfhrung in die Programmierung Vorlesung 2 14

Arithmetische Operatoren
+ - * / % Beispiele:
int counter = 10; int sum = 0; sum = sum + counter; counter = counter - 1; float pi = 3.14159265; float radius = 1.5; float circumference = 2 * pi * radius;
DI Dr. Christian Safran, IICM

Einfhrung in die Programmierung Vorlesung 2 15

Typ der Operanden bestimmt Ergebnis-Typ !


Beispiele:
int one = 1; float half half = 1 / half = 1.0 half = one = one / 2; 2; / 2.0; / 2.0; // // is // // is 0 ! 0 ! now it is 0.5 also 0.5

int sum = one + half; sum = one half; sum = one * half;

// is only 1 ! // is 0 ! // is 0 !

DI Dr. Christian Safran, IICM

Einfhrung in die Programmierung Vorlesung 2 16

Modulo-Operator %
Nicht fr float und double Beispiel:
unsigned seconds = 12345; unsigned unsigned unsigned unsigned // seconds since midnight // 3 // 205 // 25 // 45

hours = seconds / 3600; minutes = seconds / 60; minutes_left = minutes % 60; seconds_left = seconds % 60;

printf("%02d:%02d:%02d\n", hours, minutes_left, seconds_left);


DI Dr. Christian Safran, IICM

Einfhrung in die Programmierung Vorlesung 2 17

Klammern ( )
int what_is_this = 1 + 2 / 3 4 * 5; int number_1 = 2; int number_2 = 3; float average = number_1 + number_2 / 2.0; // 3.5 (!) average = (number_1 + number_2) / 2.0; // 2.5 average = (number_1 + number_2) / 2; // 2.0 (!) // ???

Faustregel: Lieber einmal zuviel als einmal zuwenig Beliebig verschachtelbar


DI Dr. Christian Safran, IICM

Einfhrung in die Programmierung Vorlesung 2 18

Increment / Decrement: ++ -Sehr hufig werden in Variablen Dinge gezhlt, z.B:


counter = counter + 1;

Kurzformen:
counter++; ++counter; counter--; --counter;

Prefix / Postfix bestimmt Zeitpunkt der Operation:


"! vor oder nach der Auswertung des ganzen Statements "! nicht durch Klammern beeinflussbar! sum = counter++; sum = ++counter;
DI Dr. Christian Safran, IICM

// first =, then ++ // first ++, then =

Einfhrung in die Programmierung Vorlesung 2 19

Demo
// test++.c - demonstrates post- und preincrement and -decrement! ! #include <stdio.h>! ! int result = 0;! int number = 2;! ! int main()! {! result = number++;! printf("Post Increment: %d , %d \n",result, number);! result = ++number;! printf("Pre Increment: %d , %d \n",result, number);! result = number--;! printf("Post Decrement: %d , %d \n",result, number);! result = --number;! printf("Pre Decrement: %d , %d \n",result, number); ! return 0;! DI }!Dr. Christian Safran, IICM

Einfhrung in die Programmierung Vorlesung 2 20

Weitere Kurzformen: += -= *= /=
Kombination aus Zuweisung und arithmetischen Operator Beispiele:
seconds += 60; ist dasselbe wie: seconds = seconds + 60; product *= --counter; ist dasselbe wie: counter = counter 1; product = product * counter;
DI Dr. Christian Safran, IICM

Einfhrung in die Programmierung Vorlesung 2 21

Vergleiche: == != > < >= <=


Liefern 1 wenn Vergleich zutrifft, 0 wenn nicht Beispiele:
float x = -3.5; int i = 12; char is_negative = (x < 0); int sign = (x >= 0) * 2 - 1; int is_even = i % 2 != 1; is_even = ((i % 2) == 0); unsigned is_zero = (x == 0); // // // // // 1 (or 0) -1 (or +1) 1 (ok but unreadable!) better 0 or 1 (x is unchanged)

Beliebter Programmierfehler: = statt == !!!


unsigned is_zero = (x = 0);
DI Dr. Christian Safran, IICM

// 0 (and x is set to 0!)

Einfhrung in die Programmierung Vorlesung 2 22

Logische Operatoren: ! && ||


! NOT
int is_odd = !is_even;

&& AND
int ok = (month >= 1) && (month <= 12);

|| OR
int invalid_month = (month < 1) || (month > 12); ok = !((month < 1) || (month > 12)); // all the same: ok = !(month < 1) && !(month > 12); ok = (month > 0) && !(month >= 13);
DI Dr. Christian Safran, IICM

Einfhrung in die Programmierung Vorlesung 2 23

Short Circuit Evaluation


Bei && und || werden die Ausdrcke von links nach rechts berechnet:
"! Wenn bei einem && der linke Ausdruck 0 ist, wird der rechte gar nicht mehr berechnet! "! Wenn bei einem || der linke Ausdruck !=0 ist, wird der rechte gar nicht mehr berechnet!

Beispiel:
// does not crash if x == 0 int ok = (x != 0) && (1 / x < 10);

vor allem im Umgang mit Pointern oft verwendet (spter)


DI Dr. Christian Safran, IICM

Einfhrung in die Programmierung Vorlesung 2 24

Ternrer Operator: ? :
spter

DI Dr. Christian Safran, IICM

Einfhrung in die Programmierung Vorlesung 2 25

Bit-Operationen
Bitweises NOT/AND/OR/XOR Shift-Operatoren spter

DI Dr. Christian Safran, IICM

Einfhrung in die Programmierung Vorlesung 2 26

C Operator Precedence
Operator-Typ
Primre Auswertung Unre Operatoren

Operator
( ) [ ] . -> expr++ expr-* & + - ! ~ ++expr --expr (typecast) sizeof * / % + >> << < > <= >=

Assoziativitt
links nach rechts rechts nach links

Binre Operatoren

== != & ^ | && ||

links nach rechts

Ternrer Operator Zuweisungs-Operatoren Komma DI Dr. Christian Safran, IICM

?: = += -= *= /= %= >>= <<= &= ^= |= ,

rechts nach links rechts nach links links nach rechts

Einfhrung in die Programmierung Vorlesung 2 27

Demonstration
// test_precedence.c - demonstrates operator precedence! #include <stdio.h>! ! int result = 0;! int number = 5;! ! int main()! {! result = (1 < number < 4);! printf("<: %d\n",result);! number = 0; ! result = number == result;! printf("<: %d\n",result); ! return 0;! }!
DI Dr. Christian Safran, IICM

Einfhrung in die Programmierung Vorlesung 2 28

1.a. Hausbung
Rechnen mit der Matrikelnummer
"! Bsp: 1330218 "! sum = 1+3+3 = 7 "! product = 0+2+1+8 = 11

"! result = (Pr-Inkrement von product Post-Inkrement von sum) * sum "! result / (letzte Stelle der Matrikelnummer + 1)
"! Integerdivision "! Rest "! Gleitkommadivision
DI Dr. Christian Safran, IICM

Einfhrung in die Programmierung Vorlesung 2 29

1.a. Hausbung
Ausgabe:
! "Result = [Ergebnis der Subtraktion und Multiplikation]\n"! "Integer division = [Ganzzahl Divisionsergebnis]\n"! "Remainder = [Rest]\n"! "Division = [dezimales Divisionsergebnis]\n"!

DI Dr. Christian Safran, IICM

Einfhrung in die Programmierung Vorlesung 2 30

1.a. Hausbung
Aufgabenstellung im Wiki
"! Halten Sie sich genau an die Anforderungen! "! Ihre Abgabe wird automatisiert getestet. "! Feedback: Anzahl der bestandenen Testflle.

Fragen von allgemeinem Interesse


"! Newsgroup tu-graz.lv.ep "! Zuerst lesen, dann posten

DI Dr. Christian Safran, IICM

También podría gustarte