02 variables

Upload: anidcohen9058

Post on 13-Apr-2018

225 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/27/2019 02 Variables

    1/37

    Structure Variables Comments Floats Simple Expressions

    2. Variables, Identifiers, and Expressions

    24. Juni 2011

    2. Variables, Identifiers, and Expressions

    Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 1 of 35

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 7/27/2019 02 Variables

    2/37

    Structure Variables Comments Floats Simple Expressions

    Outline

    A Movie

    Structure of Simple C Codes

    Variables & Identifiers

    Comments & Documentation

    Built-in Datatypes

    Excursus: Floating Point Precision

    Simple Expressions: Assignments and

    Basic Arithmetics

    2. Variables, Identifiers, and Expressions

    Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 2 of 35

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 7/27/2019 02 Variables

    3/37

    Structure Variables Comments Floats Simple Expressions

    The Golden Age of (Super)Computing

    Chris Johnson,

    head of the Scientific Computing &Imaging Institute in Salt Lake City, and

    member of the PITAC committee. At TUM, theres similar institutes

    (organised as consortia) such as theMunich Centre of AdvancedComputing.

    2. Variables, Identifiers, and Expressions

    Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 3 of 35

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 7/27/2019 02 Variables

    4/37

    Structure Variables Comments Floats Simple Expressions

    2.1.Structure

    # i n c l u d e

    i n t main ( ) {s t d : : c ou t

  • 7/27/2019 02 Variables

    5/37

    Structure Variables Comments Floats Simple Expressions

    An Addendum: C vs. C++

    C is (basically) subset of C++

    Some features were deprecated or augmented (see comments)

    Some functions were replaced, in particular the output functions

    i n t a=10; double b=20;s t d : : c ou t

  • 7/27/2019 02 Variables

    6/37

    Structure Variables Comments Floats Simple Expressions

    2.2.Variables

    2. Variables, Identifiers, and Expressions

    Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 6 of 35

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 7/27/2019 02 Variables

    7/37

    Structure Variables Comments Floats Simple Expressions

    Declaring a Variable

    Computer runs through code step-by-step

    Declaration= define a name/alias for a memory location

    Definition= find a well-suited (free) place in memory

    Variable then is alias for this storage point we work with names instead of addresses

    2. Variables, Identifiers, and Expressions

    Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 7 of 35

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 7/27/2019 02 Variables

    8/37

    Structure Variables Comments Floats Simple Expressions

    Identifiers

    W. Savitch: A C++ identifier must start with either a letter or

    the underscore symbol, and the remaining characters must allbe letters, digits, or underscore symbols. C++ identifiers are casesensitive and have no limit to their length. (p. 7)

    An identifier is (unique) name of a variable

    Must not equal a keyword

    Should have a name with a precise meaning(UNIX-style and Hungarian notation today are considered to be a bad smell)

    Examples:

    userNumber, UserNumber, userNumber, usernumber, user number

    usno, usrn, usrn, nusr, usr1, usr1no iUserNumber, intUserNumber

    2. Variables, Identifiers, and Expressions

    Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 8 of 35

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 7/27/2019 02 Variables

    9/37

    Structure Variables Comments Floats Simple Expressions

    Built-in Datatypes

    A variable corresponds to memory location and holds a value.

    What type of value?char, int, float, anddouble, and so forth.

    Compiler cares for memory layout.

    2. Variables, Identifiers, and Expressions

    Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 9 of 35

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 7/27/2019 02 Variables

    10/37

    Structure Variables Comments Floats Simple Expressions

    What Value Does a Variable Have By Default?

    2. Variables, Identifiers, and Expressions

    Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 10 of 35

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 7/27/2019 02 Variables

    11/37

    Structure Variables Comments Floats Simple Expressions

    Scope of Built-in Datatypes

    E. W. Dijkstra: Once a person has understood the way variables

    are used in programming, he has understood the quintessenceof programming. (Notes on Structured Programming)

    name size range memory footprintbool {,} 1 Byte (?)char 0 . . . 255 1 Byteshort int 32, 768 . . . 32, 767 2 Bytesint 2, 147, 483, 648 . . . 2, 147, 483, 647 4 Bytes (?)long int 2, 147, 483, 648 . . . 2, 147, 483, 647 4 Bytes (?)float 1037 . . . 1038 4 Bytes

    double 10

    307 . . . 10308 8 Bytes

    2. Variables, Identifiers, and Expressions

    Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 11 of 35

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 7/27/2019 02 Variables

    12/37

    Structure Variables Comments Floats Simple Expressions

    Long and Unsigned Modifier

    Long Integers

    l on g i n t a ; / / s iz e ? i n t b ; / / s iz e ?

    C/C++ standard defines at leastnbits.

    On 64 bit architectures,int and long inttypically 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 modifiers such as longand short.

    Unsigned Integers

    u ns ig ne d i n t a ; / / s iz e ?

    i n t b ; / / s iz e ?

    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, Identifiers, and Expressions

    Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 12 of 35

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 7/27/2019 02 Variables

    13/37

    Structure Variables Comments Floats Simple Expressions

    What is an Overflow?

    An excursus into binary number systems:

    00000000b = 010

    00000001b = 110

    00000010b = 210

    00000011b = 310

    ALU internal (for an inc): 00110111b+ 00000001b 00110111b 00110110b 00110100b 00111000b

    Your turn: Increment 11111111b(it is an unsigned integer)!

    2. Variables, Identifiers, and Expressions

    Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 13 of 35

    St t V i bl C t Fl t Si l E i

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 7/27/2019 02 Variables

    14/37

    Structure Variables Comments Floats Simple Expressions

    What is an Overflow?

    C++ does not check for overflows(unlike all the interpreted languagessuch as Java and C# that are slower inturn).

    C++ does not check for underflows

    (unlike all the interpreted languagessuch as Java and C# that are slower inturn).

    Now, how would you define/encodeyour signed integer in terms of bitsand what do the operations look like?

    2. Variables, Identifiers, and Expressions

    Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 14 of 35

    Structure Variables Comments Floats Simple Expressions

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 7/27/2019 02 Variables

    15/37

    Structure Variables Comments Floats Simple Expressions

    Codes for Signed Integers

    1-2:

    00000001b- 00000001b- 00000001b 00000001b- 00000001b 00000000b- 00000001b 11111111b

    -1+1:

    11111111b+ 00000001b- 00000001b

    11111111b 11111110b 10000000b 00000000b

    Still, the first bit is the sign, but an overflow is just running through zero.

    Theres more negative values than positive ones.

    2. Variables, Identifiers, and Expressions

    Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 15 of 35

    Structure Variables Comments Floats Simple Expressions

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 7/27/2019 02 Variables

    16/37

    Structure Variables Comments Floats Simple Expressions

    Syntax vs. Semantics

    Syntax= rules for the constructing of sentences in the languages

    Semantics= meaning of sentences, words, or fragments.

    The syntax is (more or less) prescribed by C, but

    the semantics is what you had in mind, i.e. the reader has to reconstruct it fromyour program.

    So, use meaningful names, and,

    if that is not sufficient, add documentation, documentation, and documentation.

    If code is not documented, it does not exist!

    2. Variables, Identifiers, and Expressions

    Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 16 of 35

    Structure Variables Comments Floats Simple Expressions

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 7/27/2019 02 Variables

    17/37

    Structure Variables Comments Floats Simple Expressions

    2.3.Comments

    # i n c l u d e

    / T h i s i s t h e famous main o p e r a t i on . Here t he a p p l i c a t i o n s t a r t s ./

    i n t main ( ) { / / hey dude , h er e we go / w r i t e yourname t o t h e c o ns ol e /s t d : : c ou t

  • 7/27/2019 02 Variables

    18/37

    Structure Variables Comments Floats Simple Expressions

    Facts About Comments

    Sad But True

    C code is difficult to understandit aint literate programming (Knuths TEXexperience)

    Documentation (design drafts, Ph.D. theses, papers, webpages) never isup-to-date (quick-fix 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!

    document everything in the code, otherwise its lost or wont be read!

    Tools

    Best Practices

    2. Variables, Identifiers, and Expressions

    Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 18 of 35

    Structure Variables Comments Floats Simple Expressions

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 7/27/2019 02 Variables

    19/37

    p p

    Documentation Tools

    Screenshot from Doxygen

    Sad But True document everything in the code,

    otherwise its lost or wont be read!

    Tools

    Idea: Extract documentation fromsource code

    Format: Webpages, PDF, TEX

    Content: Diagrams,documentation text, mathematicalformulas, images

    Tools: JavaDoc, Doxygen (bothfor C/C++)

    Best Practices

    2. Variables, Identifiers, and Expressions

    Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 19 of 35

    Structure Variables Comments Floats Simple Expressions

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 7/27/2019 02 Variables

    20/37

    p p

    Documentation Best Practices

    Sad But True

    document everything in the code, otherwise its lost or wont be read! Tools

    Best Practices

    Document everything in the code (first place)beforeyou write or alter thecode.

    If you have to work with a third-party code, document it in the first place andsend 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 yourdocumentation.

    Document what, why, rationale, alternatives, and bugs fixed.

    2. Variables, Identifiers, and Expressions

    Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 20 of 35

    Structure Variables Comments Floats Simple Expressions

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 7/27/2019 02 Variables

    21/37

    Screenshot

    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

    PassiveLeafEventPassiveNewEvent

    PassivePersistentRefinedE.

    PassiveRefinedEvent

    PersistentRefinedEvent

    RefinedEvent

    RootEvent

    StaticLeafEvent

    Description Source Call Graph

    peano::grid::AbstractVertex::isPassive ( Const Public Method )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 thewhole surrounding support are outside of the domain.

    Be careful to makeany vertex outside the computational domain a passive vertex. If you want to plot boundary cells, e.g., you have to ensure that all thevertices are plotted before you plot your cells. Yet, the touch operations are not invoked for passive vertices.

    In the parallelmode, 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() .

    Peano-Sources - isPassive http://www5.in.tum.de/peano/src/src/grid/isPassive3949054044_member_description.html

    1 von 3 07.06.2010 16:07

    2. Variables, Identifiers, and Expressions

    Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 21 of 35

    Structure Variables Comments Floats Simple Expressions

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 7/27/2019 02 Variables

    22/37

    2.4.Floats

    # i n c l u d e

    i n t main ( ) { / / hey dude , h er e we go double a ;double b ;double c ;a = 0 .1 45 e07;b = 2 3. 24 e09 ;c = a + b ;

    s t d : : c ou t

  • 7/27/2019 02 Variables

    23/37

    A GedankenexperimentFixed-point notation

    A number in a computer has to have afinite number of bits.

    This means a finite number of digits.

    Lets assume we have four digits in theformxx.yy.

    a = 0 1 . 5 0 ;b = 0 0 . 5 0 ;c = a / b ;d = c / 3 ;

    2. Variables, Identifiers, and Expressions

    Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 23 of 35

    Structure Variables Comments Floats Simple Expressions

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 7/27/2019 02 Variables

    24/37

    Floating-point notation

    A dynamic scheme (with a header, e.g.) cannot be fast (although Maple

    e.g. supports this if we need arbitary number of significant/valid digits). Define number into significant digitsand exponent.

    Make the representation unique, i.e.ab.cd 104 =a.bcd 105. This is anormalisation.

    In a binary system, base is 2 and first digit before comma always is 0 (or 1respectively).

    So, we need one bit for the sign, sbits for the significant digits, one bit for the signof the exponent, andebits for the exponent.

    Type Sign Exponent Significand Total bits

    Single 1 8 23 32Double 1 11 52 64

    This is a at least standard! And theres two additional/special values: nanand inf.

    2. Variables, Identifiers, and Expressions

    Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 24 of 35

    Structure Variables Comments Floats Simple Expressions

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 7/27/2019 02 Variables

    25/37

    Normalisation & round-off errors

    # i n c l u d e

    i n t main ( ) {/ / hey dude , here we go

    double a ;double b ;double c ;a = 0 .1 45 e07;b = 2 3. 24 e09 ;c = a + b ;s t d : : c ou t

  • 7/27/2019 02 Variables

    26/37

    Normalisation & round-off errors

    C1 =N

    i=1

    i

    C2 =

    N/2

    i=1

    (i+N i)

    Which variant is the better one?

    What means stability in this context?

    Why is the condition number importantwithin this context?

    We have to study all our algorithms in de-

    tail!

    2. Variables, Identifiers, and Expressions

    Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 26 of 35

    Structure Variables Comments Floats Simple Expressions

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 7/27/2019 02 Variables

    27/37

    Some remarks on performance

    A Flop is a floating point operation.

    A MFlop is a . . . ? How many Flops does the SuperMUC provide?

    If one of these GPGPU guys tells you something about performance, ask himabout which precision he is using!

    2. Variables, Identifiers, and Expressions

    Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 27 of 35

    Structure Variables Comments Floats Simple Expressions

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 7/27/2019 02 Variables

    28/37

    2.5.Simple Expressions

    The Assignment Statement

    The assignment operator takes expressionfrom the right-hand side, evaluates it, andstores the result in the variable on the left-hand side.From a mathematical point of view, choo-sing = as assignment operator is a poorchoice. Pascal, e.g., uses the := operator,which, from the authors point of view, is abetter symbol.A declaration can directly be combined withan assignemnt.

    2. Variables, Identifiers, and Expressions

    Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 28 of 35

    Structure Variables Comments Floats Simple Expressions

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 7/27/2019 02 Variables

    29/37

    Arithmetic Expressions (for Numbers)

    c=10+2; assign variablecthe value 12.

    a=10; b=a; a=2; afterwards,aholds 2 andbholds 10, i.e. a=b.

    c=10*2; assign variablecthe value 20.

    b=3; a=(b+2)*10; assign variablebthe value 3.abecomes 50.

    a=2; a=a*2; this aint a fixpoint formula, i.e.aholds the value 4 afterwards.

    a=3; a++; incrementoperator makesahold 4.

    (unary operator)

    a=3; a--; decrementoperator.

    a=2; a+=4; shortcut fora=a+4.

    a=2; a-=4; shortcut fora=a-4.

    a=2; a*=4; shortcut fora=a*4.

    a=a/2; divide value ofa by 2.

    a=4; a/=2; shortcut fora=a/2.

    2. Variables, Identifiers, and Expressions

    Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 29 of 35

    Structure Variables Comments Floats Simple Expressions

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 7/27/2019 02 Variables

    30/37

    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

    char x=a;

    char x=40;

    Comparsions

    bool x=a>4;

    bool x=a>b;

    bool x=a==b;

    bool x=a!=b;

    2. Variables, Identifiers, and Expressions

    Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 30 of 35

    Structure Variables Comments Floats Simple Expressions

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 7/27/2019 02 Variables

    31/37

    Fancy Initialisation

    : In C++, you can initialise all variables with brackets!

    int a = 10; int a(10);

    2. Variables, Identifiers, and ExpressionsEinfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 31 of 35

    Structure Variables Comments Floats Simple Expressions

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 7/27/2019 02 Variables

    32/37

    Initialisation Pitfalls IMultiple Declarations

    int a;

    int b;

    int a,b; Shortcut int a,b =2 ; What does this

    mean?

    2. Variables, Identifiers, and ExpressionsEinfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 32 of 35

    Structure Variables Comments Floats Simple Expressions

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 7/27/2019 02 Variables

    33/37

    Initialisation Pitfalls IMultiple Declarations

    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, Identifiers, and ExpressionsEinfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 32 of 35

    Structure Variables Comments Floats Simple Expressions

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 7/27/2019 02 Variables

    34/37

    Initialisation Pitfalls IIImplicit Type Conversion

    double a;

    double a=0.3;

    double a=10.0 / 4.0;

    double a=10.0 / 4;

    double a=10 / 4;

    2. Variables, Identifiers, and ExpressionsEinfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 33 of 35

    Structure Variables Comments Floats Simple Expressions

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 7/27/2019 02 Variables

    35/37

    Initialisation Pitfalls IIImplicit Type Conversion

    double a;

    double a=0.3;

    double a=10.0 / 4.0;

    double a=10.0 / 4;

    double a=10 / 4;

    If you use floating point arithmetics always write .0 for natural numbers. If you are interested in the underlying techniques, study the field oftype inference. If you wanna have a more dynamic feeling, use a language with a dynamic typesystem.

    2. Variables, Identifiers, and ExpressionsEinfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 33 of 35

    Structure Variables Comments Floats Simple Expressions

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 7/27/2019 02 Variables

    36/37

    Initialisation Pitfalls IIIAssignment Operators

    i n t a = 3

    bool b = (a==3);

    bool c = (a=3);i n t d = 0

    bool e = (d==0);

    bool f = (d=0);

    2. Variables, Identifiers, and ExpressionsEinfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 34 of 35

    Structure Variables Comments Floats Simple Expressions

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl
  • 7/27/2019 02 Variables

    37/37

    Initialisation Pitfalls IVFreaky Collaborators

    int a = 3; int b = ++a; (that is want we want)int c = 3; int d = c++; (that is something different)

    int e = (d+=a)++;(also very funny)int f = ((d=4)+a)--;

    C/C++ offer many strange constructs. This is good luck for posers, freaks, andpeople giving lectures. Others should avoid them.

    2. Variables, Identifiers, and ExpressionsEinfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 35 of 35

    http://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierlhttp://www5.in.tum.de/wiki/index.php/Dr._rer._nat._Tobias_Weinzierl