C Programming Under Linux: P2T Course, Martinmas 2003-4 C Lecture 2
C Programming Under Linux: P2T Course, Martinmas 2003-4 C Lecture 2
C Programming Under Linux: P2T Course, Martinmas 2003-4 C Lecture 2
http://www.physics.gla.ac.uk/~kaiser/
One bit is the unit of information, i.e. one 1 or 0. Eight bits are
one byte.
A complete unit of information is called a word and generally
consists of one or several bytes. As a simplified general rule
we can say that memories store data in bytes.
The memory can be imagined as an array of cells (flip-flops or
capacitors) that has 8 columns and a large number of rows. In
this picture a 64 MByte memory has 8 columns and 64 million
rows (actually , more like 67 million rows, as 1 k is 1024 in
memory).
The location of data in a memory array is called its address.
Address Data
011 Register Register 10010110
Address Bus 0 1 0 0 1 0 1 1 1
Address Decoder
1 1 1 0 1 0 1 1 1
2 1 0 1 0 0 0 1 1
3 1 0 0 1 0 1 1 0
Data Bus
4 1 0 0 1 0 1 1 1
5 0 1 1 1 0 0 1 1
6 1 0 0 0 1 1 1 1
7 1 0 0 1 0 1 1 1
In other number systems the position of the digit indicates it’s
value in powers of another value: powers of 2 for binary,
powers of 8 for octal and powers of 16 for hexadecimal
numbers.
Octal number only need the symbols 0..7, hexadecimal
numbers use A..F for the numbers 10..15, e.g. 2003 (decimal)
becomes 7D3 (hexadecimal).
Binary, octal and hexadecimal are the natural number systems
for computers.
P2T: C Lecture 2 – p.6/22
Variable Types
In C data are stored in variables and there are three basic types of
variables:
int
integer variables, whose values are integer numbers such as
4, -128, 147238
float
floating point numbers, corresponding to non-integer numbers
with a decimal point. 5.0 is a floating point number, while 5 is
an integer.
char
character variables with values ’a’ to ’z’, ’A’ to ’Z’, ’0’ to ’9’ plus
punctuation marks, parentheses etc.
-2147483648.
The standard header file limits.h defines constants for the
various numerical limits.
/* Minimum and maximum values a ‘signed int’ can hold. */
# define INT_MIN (-INT_MAX - 1)
# define INT_MAX 2147483647
range of to with a corresponding range of
negative numbers and approximately 7 significant digits.
double variables use 8 bytes, corresponding to a value range
of to with a corresponding range of
negative numbers and approximately 15 significant digits.
floating point numbers can be given in exponential form, e.g.
1.2e34 is
int main()
{
term1 = 1 + 2 4; / yields 2 4=8 8+1=9 /
term2 = (1 + 2) 4; / yields 1+2=3 3 4=12 /
sum = term1 + term2; / yields 9+12=21 /
difference = term1 term2 / yields 9 12= 3 /
modulo = term1 % term2 / yields
9/12=0, remainder is 9 /
product = term1 term2 / yields 9 12=108 /
ratio = 9 / 12 / yields 9/12=0 /
return(sum);
}
/********************************************************
* Question: *
* Why does the following program print: *
* "The value of 1/3 is 0.0" ? *
********************************************************/
#include <stdio.h>
int main()
{
answer = 1/3;
printf("The value of 1/3 is %f\n", answer);
return (0);
}
int main()
{
answer = 2 + 2;
ASCII was defined in 1968 (i.e before most of you were born).
It uses 7 bit and works well with American English, which is
after all what it was meant to do.
ASCII already doesn’t work with Danish, French or German -
but for this there is an extension to 8 bit called Latin1 (aka
ISO-8859-1).
printf will typically understand Latin1, so you can use
printf("One beer is 1.95 xA3"); to print
One beer is 1.95 £.
If you’re German (like me) you might want to print
’Schöne Grüße’, which you can do like this:
printf("Sch 366ne Gr 374 337e n");
Of course this still doesn’t help you much if you’re Thai or
Armenian. Have a look at www.unicode.org for Unicode
(ISO-10646), a 32 bit code for all the letters in the world.
P2T: C Lecture 2 – p.22/22