Lokale Konstanten in C
main.c
#include <stdio.h>
int main( void )
{ printf( "%d\n", 11 );
int const elf = 11;
printf( "%d\n", elf ); }stdout
11
11main.c
#include <stdio.h>
int main( void )
{ printf( "%d\n", 11 );
int const zehn = 11;
printf( "%d\n", zehn ); }stdout
11
11main.c
#include <stdio.h>
int main( void )
{ double const d = 300;
int const i = 273.99;
printf( "%g\n", d );
printf( "%d\n", i ); }stdout
300
273
Die Nachkommastellen werden abgeschnitten (es wird nicht etwa gerundet).
main.c
#include <stdio.h>
int main( void )
{ double const t_kelvin = 300;
double const versatz = 273.15;
double const t_celsius = t_kelvin - versatz;
printf( "%g\n", t_celsius ); }stdout
26.85
main.c
#include <stdio.h>
int main( void )
{ { int const c = 1; printf( "%d\n", c ); }
{ int const c = 2; printf( "%d\n", c ); }}stdout
1
2main.txt
#include <stdio.h>
int main( void )
{ { int const c = 1; } printf( "%d\n", c ); }- Konsole
- (Fehlermeldung)
main.txt
#include <stdio.h>
int main( void )
{ int const c = 1;
int const c = 2;
printf( "%d\n", c ); }- Konsole
- (Fehlermeldung)
- C 2011, 6.7p3
- “If an identifier has no linkage, there shall be no more than one declaration of the identifier (in a declarator or type specifier) with the same scope and in the same name space ”
main.c
#include <stdio.h>
int main( void )
{ int const c = 9;
int const d = 9;
printf( "%d\n", c );
printf( "%d\n", d );
{ printf( "%d\n", c );
printf( "%d\n", d );
int const c = 8;
printf( "%d\n", c );
printf( "%d\n", d ); }
printf( "%d\n", c );
printf( "%d\n", d ); }stdout
9
9
9
9
8
9
9
9- unnötige doppelte Berechnung
printf( "%g\n", floor( 2.7 ));
printf( "%g\n", floor( 2.7 ));- einmalige Berechnung (gleiches Verhalten wie zuvor, Refaktor)
{ double const fl = floor( 2.7 );
printf( "%g\n", fl );
printf( "%g\n", fl ); }- doppelte Berechnung
printf( "%d\n", rand() );
printf( "%d\n", rand() );- einmalige Berechnung (nicht gleiches Verhalten wie zuvor, kein Refaktor)
{ int const r = rand();
printf( "%d\n", r );
printf( "%d\n", r ); }
? Kugelvolumen
In dem folgenden Programm ist nicht ganz offensichtlich, daß »4 * atan( 1 )« nur zur Berechnung der Kreiszahl ‹π› dienen soll und die zweite »3« der Radius ist.
main.c
#include <stdio.h>
#include <math.h>int main( void )
{ printf( "%g\n", 4./3 * 4 * atan( 1 )* pow( 3, 3 )); }Protokoll
113.097
Durch Einführung eines Namens »pi« wird in dem folgenden Programm verdeutlicht, daß »4 * atan( 1 )« zur Berechnung der Kreiszahl ‹π› dienen soll und die zweite »3« der Radius ist. Der Ausdruck »4./3 * pi * pow( r, 3 )« ähnelt auch mehr dem bekannten Term aus der Schulmathematik.
main.c
#include <stdio.h>
#include <math.h>int main( void )
{ double const pi = 4 * atan( 1 );double const r = 3;
double const v = 4./3 * pi * pow( r, 3 );
printf( "%g\n", v ); }
Protokoll
113.097
Übungsfrage
Der Leser kann jetzt vielleicht vorhersagen, was das folgende Programm ausgibt.
main.c
#include <stdio.h>
int main( void )
{ int const c = 3;
{ int const c = 4;
{ int const c = 5;
printf( "%d\n", c ); }
printf( "%d\n", c ); }
printf( "%d\n", c ); }
Übungsfrage
Die beiden folgenden Programme kommen zum selben Ergebnis. Welches gefällt Ihnen besser? Warum?
main.c
#include <stdio.h>
int main( void )
{ double const t_kelvin = 300;
double const versatz = 273.15;
double const t_celsius = t_kelvin - versatz;
printf( "%g\n", t_celsius ); }stdout
26.85
main.c
#include <stdio.h>
int main( void )
{ printf( "%g\n", 300 - 273.15 ); }stdout
26.85
Übungsfrage
main.c
#include <stdio.h>
int main( void )
{ int const a = 1;
printf( "%d\n", a + 1 );
a + 1;
printf( "%d\n", a ); }
Welchen Wert gibt die letzte Auswertungsanweisung des obigen Quelltextes aus?
Übungsaufgabe
main.c
#include <stdio.h>
#include <math.h>int main( void )
{ printf( "%g\n", sin( 1 + log( 2.178 )));
printf( "%g\n", cos( 1 + log( 2.178 ))); }stdout
0.978526
-0.206122
Schreiben Sie das Programm »main.c« so um, daß derzeit wiederholt vorkommende gleiche Ausdrücke nur einmal vorkommen. Definieren Sie Konstanten, mit jenen Ausdrücken initialisiert werden, und verwenden Sie diese Konstante dann an deren Stellen. Verwenden Sie aber die im Programm »main.c« vorkommenden Literale und Operatoren, ohne die Werte von Ausdrücken dabei schon im Kopf auszurechnen. Es sollte möglichst nur eine einzige Konstante zum Programm hinzugefügt werden! Die Ausgabe des Programmes darf sich nicht ändern.
Übungsaufgabe
main.c
#include <stdio.h>
#include <math.h>int main( void )
{ double const x = 1;
double const y = sin( x );
double const z = sqrt( y );
double const u = pow( y, 0.51 );
printf( "%g\n", z );
printf( "%g\n", u ); }stdout
0.917317
0.915735
Schreiben Sie das Programm »main.c« so um, daß dieselben Werte ausgegeben werden und dieselben Numerale und Funktionen zur Berechnung verwendet werden, aber keine Konstanten mehr in dem Programm vorkommen. Ersetzen Sie zunächst die Konstante »x« überall durch ihre Definition, also durch den Wert »1« und entfernen Sie die Definition der Konstante »x«. Es ergibt sich so zunächst:
main.c
#include <stdio.h>
#include <math.h>int main( void )
{ double const y = sin( 1. );
double const z = sqrt( y );
double const u = pow( y, 0.51 );
printf( "%g\n", z );
printf( "%g\n", u ); }stdout
0.917317
0.915735
Die Konstante »x« wurde also eliminiert, ohne daß sich das Verhalten des Programms verändert hat.
Nun zeigen wir auch noch die Elimination von »y«:
main.c
#include <stdio.h>
#include <math.h>int main( void )
{ double const z = sqrt( sin( 1. ));
double const u = pow( sin( 1. ), 0.51 );
printf( "%g\n", z );
printf( "%g\n", u ); }stdout
0.917317
0.915735
Nach diesem Schema können dann alle anderen Definitionen eliminiert werden, bis nur noch die beiden Ausgabeausdrücke übrigbleiben.