Вы находитесь на странице: 1из 19

Scientific Programming in C

II. Data types


Susi Lehtola

30 October 2012

Data types in C
The basic data types in C are
I

char, a single byte, capable of holding a character in the


current character set

int, an integer

float, single precision floating point number

double, double precision floating point number

There are also modifiers


I

short and long for integers

signed (default) or unsigned for characters and integers

Scientific Programming in C, fall 2012

Susi Lehtola

Data types

2/19

Data types in C
The basic data types in C are
I

char, a single byte, capable of holding a character in the


current character set

int, an integer

float, single precision floating point number

double, double precision floating point number

There are also modifiers


I

short and long for integers

signed (default) or unsigned for characters and integers

... but what are these, really, and why do we need them?

Scientific Programming in C, fall 2012

Susi Lehtola

Data types

3/19

Data types in C
The basic data types in C are
I

char, a single byte, capable of holding a character in the


current character set

int, an integer

float, single precision floating point number

double, double precision floating point number

There are also modifiers


I

short and long for integers

signed (default) or unsigned for characters and integers

... but what are these, really, and why do we need them?
Lets return to that in a bit.
Scientific Programming in C, fall 2012

Susi Lehtola

Data types

4/19

Constants
I

1 is an integer type constant

1u or 1U is an unsigned integer

1l or 1L is a long integer

1ul or 1UL is an unsigned long integer

1.0 is a double precision floating point constant

1.0f is a single precision floating point constant

1.0l is a long double

I 0 x0
I

is a character constant

xyz is a string constant (a constant character array)

Scientific Programming in C, fall 2012

Susi Lehtola

Data types

5/19

Representation of integers

To represent integers we need to define the maximum size of the


number we use. For example, with 8 digits
1

xxxxxxxx

we are able to represent numbers from


1

00000000

to
1

99999999

in the 10-base system.

Scientific Programming in C, fall 2012

Susi Lehtola

Data types

6/19

Binary system

Computers use the binary system, in which


000000002 = 010
and
111111112 = 25510
If we use the first bit to define the sign, our range is from 128 to
127.
We can extend or reduce the range we span by adding or removing
more bits from the data type.

Scientific Programming in C, fall 2012

Susi Lehtola

Data types

7/19

Representation of real numbers

Extending the logic of the integer system, we can construct real


numbers by fixing a decimal point somewhere. Naturally, as there
is an infinite number of real numbers in any given finite interval,
we introduce rounding errors.
For example, in the convention that the last 3 digits are decimals,
the raw-data value 12957203 would correspond to the number
12957.203.
This is known as the fixed-point system. Its not available in any
(standard) programming languages but it can be manually
implemented using integers if necessary.

Scientific Programming in C, fall 2012

Susi Lehtola

Data types

8/19

Representation of decimal numbers, contd

As practical applications often use real numbers with a huge range,


usage of the fixed-point system is not computationally feasible.
Computing the value of simple functions such as y = x 2 would be
difficult.
In practice scientists dont write out all the zeros and the decimals,
but use the scientific notation instead. For instance the Bohr
radius is
a0 0.0000000000052917721 m
=

Scientific Programming in C, fall 2012

5.2917721 1011 m

Susi Lehtola

Data types

9/19

Floating-point numbers
Applying the same principle to represent real numbers on the
computer, we end up with the floating-point system.
Numbers are represented in the format
x = mantissa baseexponent
Type
float
double
80-bit double
quad

Sign
1
1
1
1

Exp.
8
11
15
15

Mantissa
23
52
64
112

Total bits
32
64
80
128

Decimal digits
7.2
15.9
19.2
34.0

Source: http://en.wikipedia.org/wiki/Floating_point

Scientific Programming in C, fall 2012

Susi Lehtola

Data types

10/19

Roundoff errors
Because of the floating-point system, the accuracy is not universal
anymore. Whereas the result of, e.g., additions in the fixed-point
system is independent of the order of summation, in the
floating-point system the result may depend on the order the
elements are summed in.
The machine epsilon is inextricably linked to this. It is defined as
the smallest number for which
1 +  > 1.
For example:


+1+ =
1
2
2


+ +1= 1+
2 2
For float  is normally around 107 and for double around 1016 .
Scientific Programming in C, fall 2012

Susi Lehtola

Data types

11/19

Comparison of floating-point numbers

Due to the finite precision and roundoff errors, its rather rare that
floating-point arithmetic gives you exactly the number you expect.
If you expect x to be equal to y , instead of using the comparison
x y
use
|x y | 10 max{x, y } 
to find out if this is true.

Scientific Programming in C, fall 2012

Susi Lehtola

Data types

12/19

Loops and if statements


One of the most common structures in programming are loops and
if statements. The structure of these in C is the following
int i ;
i n t x =0;
f o r ( i =0; i <10; i ++)
x+=i i ;
The first argument to for is ran at the start of the iteration. The
second argument is checked before each iteration - if true, the
iteration is ran. The last argument is ran at the end of each
iteration.
i f ( statement i s true )
dosomething ( ) ;
else
dootherthing ();
You can also leave out the else part, if you dont need one.
Scientific Programming in C, fall 2012

Susi Lehtola

Data types

13/19

Loops and if statements, contd


The scope of if and for is the following statement, so if you want
to run more statements, enclose them in braces.
i n t i =0;
double j = 0 . 0 ;
double k = 0 . 0 ;
f o r ( i =0; i <10; i ++) {
j+=i 0 . 3 ;
k+=j j ;
}
i f ( statement i s true ) {
dofirstthing ();
dosecondthing ( ) ;
} else {
dootherthing ();
domorestuff ( ) ;
}
Scientific Programming in C, fall 2012

Susi Lehtola

Data types

14/19

If statements
If you have if statements within if statements, with else
statements, you really should use braces to be clear.
i f ( i ==0)
i f ( j ==0)
k =0;
else
k =1;
Here the else statement refers to the last if statement (which is
made clear by the indentation). But compilers often warn about
these kinds of statements, since they might lead to unexpected
behavior.
i f ( i ==0) {
i f ( j ==0)
k =0;
else
k =1;
}
Scientific Programming in C, fall 2012

Susi Lehtola

Data types

15/19

Loop example
Connection between Fahrenheit and centigrade temperature scale
is F = 1.8 C + 32
#i n c l u d e < s t d i o . h>
float celcius to fahr ( float c) {
r e t u r n 1 . 8 f c +32.0 f ;
}
i n t main ( v o i d ) {
int i ;
f o r ( i =0; i <=100; i +=10)
p r i n t f ( %i %f \n , i , c e l c i u s t o f a h r ( c ) ) ;
return 0;
}
Scientific Programming in C, fall 2012

Susi Lehtola

Data types

16/19

Loop example, contd


$ . / a . out
0 32.000000
10 5 0 . 0 0 0 0 0 0
20 6 8 . 0 0 0 0 0 0
30 8 6 . 0 0 0 0 0 0
40 1 0 4 . 0 0 0 0 0 0
50 1 2 2 . 0 0 0 0 0 0
60 1 4 0 . 0 0 0 0 0 0
70 1 5 8 . 0 0 0 0 0 0
80 1 7 6 . 0 0 0 0 0 0
90 1 9 4 . 0 0 0 0 0 0
100 2 1 2 . 0 0 0 0 0 0

Scientific Programming in C, fall 2012

Susi Lehtola

Data types

17/19

If example
The realization of the function
(
a, x a
y (x) =
x, x > a
is
double c u t o f f ( double x , double a ) {
i f ( x<=a )
return a ;
else
return x ;
}

Scientific Programming in C, fall 2012

Susi Lehtola

Data types

18/19

Operators
The operators that are available in C are
=

/
+
%

assignment
multiplication
division
addition
substraction
modulus division

There are also the following comparison operators


==
>
>=
<
<=
Scientific Programming in C, fall 2012

equality
greater than
at least
smaller than
at most
Susi Lehtola

Data types

19/19

Вам также может понравиться