You are on page 1of 37

Structure

Variables

Comments

Floats

Simple Expressions

2. Variables, Identiers, and Expressions 24. Juni 2011

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

The Golden Age of (Super)Computing

Chris Johnson, head of the Scientic Computing &

Imaging Institute in Salt Lake City, and


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

(organised as consortia) such as the Munich Centre of Advanced Computing.

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; }

The computer runs through the code step-by-step

(left to right, top-down)


In each step, it executes the commands

(a code is a cooking instruction)


A step is terminated by a semicolon main identies the jump-in point std::cout plots something on the console return 0 makes the program terminate

(return to the command line prompt)

2. Variables, Identiers, and Expressions Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 4 of 35

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; std : : cout < < a is < < a< < and b i s < < b ; / / C++ s t y l e p r i n t f ( a i s %i and b i s %f , a , b ) ; / / C style

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;

21: 22; 23; 24; 25; 26; 27; 28:

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

we work with names instead of addresses


2. Variables, Identiers, and Expressions Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 7 of 35

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

(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, 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

int x; char y; float z;

21: 22; 23; 24; 25; 26; 27; 28:

y // one byte z // four bytes

x // two bytes?

A variable corresponds to memory location and holds a value. What type of value?

char, int, float, and double, and so forth.


Compiler cares for memory layout.
2. Variables, Identiers, and Expressions Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 9 of 35

Structure

Variables

Comments

Floats

Simple Expressions

What Value Does a Variable Have By Default?

2. Variables, Identiers, and Expressions Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 10 of 35

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 quintessence of programming. (Notes on Structured Programming)

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

Long and Unsigned Modier


Long Integers long i n t a ; / / size ? int b ; / / size?

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.

Unsigned Integers unsigned i n t a ; / / s i z e ? int b; / / size?

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:

00000000b 00000001b 00000010b 00000011b


ALU internal (for an inc):

= = = =

010 110 210 310

00110111b + 00000001b 00110111b 00110110b 00110100b 00111000b

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

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?

C++ does not check for overows

(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

Codes for Signed Integers


1-2:

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

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 from

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

Facts About Comments


Sad But True

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!

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


Tools Best Practices

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

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


Tools

Screenshot from Doxygen

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

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 (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

Call Graph ( Const Public Method )

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

A number in a computer has to have a

nite number of bits.


This means a nite number of digits. Lets assume we have four digits in the

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

e.g. supports this if we need arbitary number of signicant/valid digits).


Dene number into signicant digits and exponent. Make the representation unique, i.e. ab .cd 104 = a.bcd 105 . This is a

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

Normalisation & round-off errors

# 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

Normalisation & round-off errors

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

within this context? We have to study all our algorithms in detail!

2. Variables, Identiers, and Expressions Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 26 of 35

Structure

Variables

Comments

Floats

Simple Expressions

Some remarks on performance


A Flop is a oating 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 him

about which precision he is using!

2. Variables, Identiers, and Expressions Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 27 of 35

Structure

Variables

Comments

Floats

Simple Expressions

2.5. Simple Expressions The Assignment Statement


Memory

int x; x = 10;

21: 22; 23; 24; 25; 26; 27; 28:

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

Arithmetic Expressions (for Numbers)


c=10+2; assign variable c the value 12. a=10; b=a; a=2; afterwards, a holds 2 and b holds 10, i.e. a = b . c=10*2; assign variable c the value 20. b=3; a=(b+2)*10; assign variable b the value 3. a becomes 50. a=2; a=a*2; this aint a xpoint formula, i.e. a holds the value 4 afterwards. a=3; a++; increment operator makes a hold 4.

(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

char x=a; char x=40;

Comparsions

bool bool bool bool

x=a>4; x=a>b; x=a==b; x=a!=b;

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

Initialisation Pitfalls IMultiple Declarations

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

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

Structure

Variables

Comments

Floats

Simple Expressions

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, Identiers, and Expressions Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 33 of 35

Structure

Variables

Comments

Floats

Simple Expressions

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 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

Initialisation Pitfalls IIIAssignment Operators


int a = 3 bool b = (a==3); bool c = (a=3); int d = 0 bool e = (d==0); bool f = (d=0);

2. Variables, Identiers, and Expressions Einfuhrung in die ProgrammierungIntroduction to C/C++, Tobias Weinzierl page 34 of 35

Structure

Variables

Comments

Floats

Simple Expressions

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, 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

You might also like