02 Variables
02 Variables
Variables
Comments
Floats
Simple Expressions
2. Variables, Identiers, and Expressions Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 1 of 35
Structure
Variables
Comments
Floats
Simple Expressions
Outline
A Movie Structure of Simple C Codes Variables & Identiers Comments & Documentation Built-in Datatypes Excursus: Floating Point Precision Simple Expressions: Assignments and
Basic Arithmetics
2. Variables, Identiers, and Expressions Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 2 of 35
Structure
Variables
Comments
Floats
Simple Expressions
2. Variables, Identiers, and Expressions Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 3 of 35
Structure
Variables
Comments
Floats
Simple Expressions
2.1. Structure
# i n c l u d e <iostream > i n t main ( ) { std : : cout < < H e l l o yourname ; return 0; }
2. Variables, Identiers, and Expressions Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 4 of 35
Structure
Variables
Comments
Floats
Simple Expressions
Other functions: malloc (became new), free (became delete), and structs (need a name before the bracket opens). Furthermore, some shortcuts were removed (main always needs a return type). The C++ typically are safer (type checks) and more powerful.
2. Variables, Identiers, and Expressions Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 5 of 35
Structure
Variables
Comments
Floats
Simple Expressions
2.2. Variables
2. Variables, Identiers, and Expressions Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 6 of 35
Structure
Variables
Comments
Floats
Simple Expressions
Declaring a Variable
Memory
int x; x = 10;
10
x is alias
Computer runs through code step-by-step Declaration = dene a name/alias for a memory location Denition = nd a well-suited (free) place in memory Variable then is alias for this storage point
Structure
Variables
Comments
Floats
Simple Expressions
Identiers
W. Savitch: A C++ identier must start with either a letter or the underscore symbol, and the remaining characters must all be letters, digits, or underscore symbols. C++ identiers are case sensitive and have no limit to their length. (p. 7)
An identier is (unique) name of a variable Must not equal a keyword Should have a name with a precise meaning
userNumber, UserNumber, userNumber, usernumber, user number usno, usrn, usrn, nusr, usr1, usr1no iUserNumber, intUserNumber
2. Variables, Identiers, and Expressions Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 8 of 35
Structure
Variables
Comments
Floats
Simple Expressions
Built-in Datatypes
Memory
x // two bytes?
A variable corresponds to memory location and holds a value. What type of value?
Structure
Variables
Comments
Floats
Simple Expressions
2. Variables, Identiers, and Expressions Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 10 of 35
Structure
Variables
Comments
Floats
Simple Expressions
name bool char short int int long int oat double
size range { , } 0 . . . 255 32, 768 . . . 32, 767 2, 147, 483, 648 . . . 2, 147, 483, 647 2, 147, 483, 648 . . . 2, 147, 483, 647 1037 . . . 1038 10307 . . . 10308
memory footprint 1 Byte (?) 1 Byte 2 Bytes 4 Bytes (?) 4 Bytes (?) 4 Bytes 8 Bytes
2. Variables, Identiers, and Expressions Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 11 of 35
Structure
Variables
Comments
Floats
Simple Expressions
C/C++ standard denes at least n bits. On 64 bit architectures, int and long int typically are the same. Some 32 bit architectures support 64 bit integers due to a tailored compiler. Others dont (such as the RZGs BlueGene/P system with the IBM compiler). Recommendation: Avoid modiers such as long and short.
That sign consumes one bit of the representation. If you are sure you dont need the sign, you can squeeze out an additional bit, however, be careful with this!
2. Variables, Identiers, and Expressions Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 12 of 35
Structure
Variables
Comments
Floats
Simple Expressions
What is an Overow?
An excursus into binary number systems:
= = = =
2. Variables, Identiers, and Expressions Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 13 of 35
Structure
Variables
Comments
Floats
Simple Expressions
What is an Overow?
(unlike all the interpreted languages such as Java and C# that are slower in turn).
C++ does not check for underows
(unlike all the interpreted languages such as Java and C# that are slower in turn).
Now, how would you dene/encode
your signed integer in terms of bits and what do the operations look like?
2. Variables, Identiers, and Expressions Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 14 of 35
Structure
Variables
Comments
Floats
Simple Expressions
00000001b - 00000001b - 00000001b 00000001b - 00000001b 00000000b - 00000001b 11111111b 11111111b + 00000001b - 00000001b 11111111b 11111110b 10000000b 00000000b
-1+1:
Still, the rst bit is the sign, but an overow is just running through zero. Theres more negative values than positive ones.
2. Variables, Identiers, and Expressions Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 15 of 35
Structure
Variables
Comments
Floats
Simple Expressions
your program.
So, use meaningful names, and, if that is not sufcient, add documentation, documentation, and documentation. If code is not documented, it does not exist!
2. Variables, Identiers, and Expressions Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 16 of 35
Structure
Variables
Comments
Floats
Simple Expressions
2.3. Comments
# i n c l u d e <iostream > / T h i s i s t h e famous main o p e r a t i o n . Here t h e a p p l i c a t i o n s t a r t s . / i n t main ( ) { / / hey dude , here we go / w r i t e yourname t o t h e console / std : : cout < < H e l l o yourname ; / / Return t o t h e console return 0; / The comments are n o t exectuded by t h e machine / }
C/C++ is an instruction for a computer not for a human Often, we (or others) do not understand from a code what is happening Insert comments which annotate the code They are thrown away by the compiler Syntax alternatives: C/C++/C++ (JavaDoc)
2. Variables, Identiers, and Expressions Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 17 of 35
Structure
Variables
Comments
Floats
Simple Expressions
C code is difcult to understandit aint literate programming (Knuths TEX experience) Documentation (design drafts, Ph.D. theses, papers, webpages) never is up-to-date (quick-x disease) After a few days, the author is not familiar with the code anymore Every developer has a different style of writing and a different style of thinking Ill comment when it is runningthat is a lie!
2. Variables, Identiers, and Expressions Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 18 of 35
Structure
Variables
Comments
Floats
Simple Expressions
Documentation Tools
Sad But True
Idea: Extract documentation from source code Format: Webpages, PDF, TEX Content: Diagrams, documentation text, mathematical formulas, images Tools: JavaDoc, Doxygen (both for C/C++)
Best Practices
2. Variables, Identiers, and Expressions Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 19 of 35
Structure
Variables
Comments
Floats
Simple Expressions
Document everything in the code (rst place) before you write or alter the code. If you have to work with a third-party code, document it in the rst place and send it back to the authors. Theyll love you! Add a description before each complicated part (operations, methods, . . . ). Make yourself familiar with the tools and add formulas and images to your documentation. Document what, why, rationale, alternatives, and bugs xed.
2. Variables, Identiers, and Expressions Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 20 of 35
Structure
Variables
Comments
Floats
Simple Expressions
Screenshot
Peano-Sources - isPassive http://www5.in.tum.de/peano/src/src/grid/isPassive3949054044_member_description.html
isPassive
Peano - Source Directory and Architecture grid AbstractVertex<> isPassive
Search Help
grid Directories adapter checkpoint configuration integration-tests multicore records runners statistics tests Classes AbstractCell<> AbstractLeafEvent<> AbstractNewEvent<> AbstractPersistentRefined. AbstractRefinedEvent<> AbstractRootEvent AbstractStackBasedRefined. AbstractVertex<> Block<> BlockContainer<> Event<> EventHelper GridCell GridVertex LeafEvent<> NewEvent<> PassiveLeafEvent<> PassiveNewEvent<> PassivePersistentRefinedE. PassiveRefinedEvent<> PersistentRefinedEvent<> RefinedEvent<> RootEvent<> StaticLeafEvent<>
Description
Source
peano::grid::AbstractVertex::isPassive
Author: Tobias Weinzierl Is Vertex Passive.
Syntax / parameters
Return value Description Is Vertex Passive. If a vertex is passive, the event handler is not called for this vertex. A vertex is set passive by the NewEvent::createVertex() operation if the vertex and the whole surrounding support are outside of the domain. Be careful to make any vertex outside the computational domain a passive vertex. If you want to plot boundary cells, e.g., you have to ensure that all the vertices are plotted before you plot your cells. Yet, the touch operations are not invoked for passive vertices. In the parallel mode, each vertex is set passive that does not hold the actual node as adjacent element. Thus, vertices outside the computational partition are set passive although they might be inside the computational domain. This switch is implemented within derivePersistentVertexDataFromCoarseGrid().
1 von 3
07.06.2010 16:07
2. Variables, Identiers, and Expressions Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 21 of 35
Structure
Variables
Comments
Floats
Simple Expressions
2.4. Floats
# i n c l u d e <iostream > i n t main ( ) { / / hey dude , here we go double a ; double b ; double c ; a = 0.145e 07; b = 23.24 e09 ; c = a+b ; std : : cout < < c; }
2. Variables, Identiers, and Expressions Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 22 of 35
Structure
Variables
Comments
Floats
Simple Expressions
A GedankenexperimentFixed-point notation
form xx .yy .
a b c d = = = = 01.50; 00.50; a / b; c / 3;
2. Variables, Identiers, and Expressions Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 23 of 35
Structure
Variables
Comments
Floats
Simple Expressions
Floating-point notation
A dynamic scheme (with a header, e.g.) cannot be fast (although Maple
normalisation.
In a binary system, base is 2 and rst digit before comma always is 0 (or 1
respectively).
So, we need one bit for the sign, s bits for the signicant digits, one bit for the sign
of the exponent, and e bits for the exponent. Type Single Double Sign 1 1 Exponent 8 11 Signicand 23 52 Total bits 32 64
This is a at least standard! And theres two additional/special values: nan and inf.
2. Variables, Identiers, and Expressions Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 24 of 35
Structure
Variables
Comments
Floats
Simple Expressions
# i n c l u d e <iostream > i n t main ( ) { / / hey dude , here we go double a ; double b ; double c ; a = 0.145e 07; b = 23.24 e09 ; c = a+b ; std : : cout < < c; }
Use your pocket calculator and assume theres eight signicant bits!
2. Variables, Identiers, and Expressions Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 25 of 35
Structure
Variables
Comments
Floats
Simple Expressions
C1
=
i =1 N /2
C2
=
i =1
(i + N i )
Which variant is the better one? What means stability in this context? Why is the condition number important
2. Variables, Identiers, and Expressions Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 26 of 35
Structure
Variables
Comments
Floats
Simple Expressions
2. Variables, Identiers, and Expressions Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 27 of 35
Structure
Variables
Comments
Floats
Simple Expressions
int x; x = 10;
10
x is alias
The assignment operator takes expression from the right-hand side, evaluates it, and stores the result in the variable on the lefthand side. From a mathematical point of view, choosing = as assignment operator is a poor choice. Pascal, e.g., uses the := operator, which, from the authors point of view, is a better symbol. A declaration can directly be combined with an assignemnt.
2. Variables, Identiers, and Expressions Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 28 of 35
Structure
Variables
Comments
Floats
Simple Expressions
(unary operator)
a=3; a--; decrement operator. a=2; a+=4; shortcut for a=a+4. a=2; a-=4; shortcut for a=a-4. a=2; a*=4; shortcut for a=a*4. a=a/2; divide value of a by 2. a=4; a/=2; shortcut for a=a/2.
2. Variables, Identiers, and Expressions Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 29 of 35
Structure
Variables
Comments
Floats
Simple Expressions
Further Expressions
For booleans
bool a=true; bool a=false; bool a=0; bool a=1; bool a=2; a=!a; a=!(a | a);
For characters
Comparsions
2. Variables, Identiers, and Expressions Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 30 of 35
Structure
Variables
Comments
Floats
Simple Expressions
Fancy Initialisation
: In C++, you can initialise all variables with brackets!
int a = 10;
int a(10);
2. Variables, Identiers, and Expressions Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 31 of 35
Structure
Variables
Comments
Floats
Simple Expressions
int a;
int b;
int a,b; Shortcut int a,b =2 ; What does this
mean?
2. Variables, Identiers, and Expressions Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 32 of 35
Structure
Variables
Comments
Floats
Simple Expressions
int a;
int b;
int a,b; Shortcut int a,b =2 ; What does this
mean?
either declare one variable per line or use C++ initialisation with brackets.
2. Variables, Identiers, and Expressions Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 32 of 35
Structure
Variables
Comments
Floats
Simple Expressions
double a; double a=0.3; double a=10.0 / 4.0; double a=10.0 / 4; double a=10 / 4;
2. Variables, Identiers, and Expressions Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 33 of 35
Structure
Variables
Comments
Floats
Simple Expressions
double a; double a=0.3; double a=10.0 / 4.0; double a=10.0 / 4; double a=10 / 4;
If you use oating point arithmetics always write .0 for natural numbers. If you are interested in the underlying techniques, study the eld of type inference. If you wanna have a more dynamic feeling, use a language with a dynamic type system.
2. Variables, Identiers, and Expressions Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 33 of 35
Structure
Variables
Comments
Floats
Simple Expressions
2. Variables, Identiers, and Expressions Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 34 of 35
Structure
Variables
Comments
Floats
Simple Expressions
C/C++ offer many strange constructs. This is good luck for posers, freaks, and people giving lectures. Others should avoid them.
2. Variables, Identiers, and Expressions Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 35 of 35