C Data types.

 

Variable definition

C has a concept of 'data types' which are used to define a variable before its use.

The definition of a variable will assign storage for the variable and define the type of data that will be held in the location.

So what data types are available?

int

float

double

char

void

enum

Please note that there is not a boolean data type. C does not have the traditional view about logical comparison, but thats another story.

Recent C++ compilers do have a boolean datatype.


int - data type

int is used to define integer numbers.

 

    {

        int Count;

        Count = 5;

    }


float - data type

float is used to define floating point numbers.

 

    {

        float Miles;

        Miles = 5.6;

    }


double - data type

double is used to define BIG floating point numbers. It reserves twice the storage for the number. On PCs this is likely to be 8 bytes.

 

    {

        double Atoms;

        Atoms = 2500000;

    }


char - data type

char defines characters.

 

    {

        char Letter;

        Letter = 'x';

    }

Modifiers

The three data types above have the following modifiers.

  • short
  • long
  • signed
  • unsigned

The modifiers define the amount of storage allocated to the variable. The amount of storage allocated is not cast in stone. ANSI has the following rules:

 

        short int <=    int <= long int

            float <= double <= long double

What this means is that a 'short int' should assign less than or the same amount of storage as an 'int' and the 'int' should be less or the same bytes than a 'long int'. What this means in the real world is:

 

                 Type  Bytes  Bits                Range

 

            short int    2      16          -32,768 -> +32,767          (32kb)

   unsigned short int    2      16                0 -> +65,535          (64Kb)

         unsigned int    4      32                0 -> +4,294,967,295   ( 4Gb)

                  int    4      32   -2,147,483,648 -> +2,147,483,647   ( 2Gb)

             long int    4      32   -2,147,483,648 -> +2,147,483,647   ( 2Gb)

          signed char    1       8             -128 -> +127

        unsigned char    1       8                0 -> +255

                float    4      32

               double    8      64

          long double   12      96

These figures only apply to todays generation of PCs. Mainframes and midrange machines could use different figures, but would still comply with the rule above.

You can find out how much storage is allocated to a data type by using the sizeof operator.

 

 

 

 

Java's Primitive Data Types

boolean

1-bit. May take on the values true and false only.

true and false are defined constants of the language and are not the same as True and False, TRUE and FALSE, zero and nonzero, 1 and 0 or any other numeric value. Booleans may not be cast into any other type of variable nor may any other variable be cast into a boolean.

byte

1 signed byte (two's complement). Covers values from -128 to 127.

short

2 bytes, signed (two's complement), -32,768 to 32,767

int

4 bytes, signed (two's complement). -2,147,483,648 to 2,147,483,647. Like all numeric types ints may be cast into other numeric types (byte, short, long, float, double). When lossy casts are done (e.g. int to byte) the conversion is done modulo the length of the smaller type.

long

8 bytes signed (two's complement). Ranges from -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807.

float

4 bytes, IEEE 754. Covers a range from 1.40129846432481707e-45 to 3.40282346638528860e+38 (positive or negative).

Like all numeric types floats may be cast into other numeric types (byte, short, long, int, double). When lossy casts to integer types are done (e.g. float to short) the fractional part is truncated and the conversion is done modulo the length of the smaller type.

double

8 bytes IEEE 754. Covers a range from 4.94065645841246544e-324d to 1.79769313486231570e+308d (positive or negative).

char

2 bytes, unsigned, Unicode, 0 to 65,535

Chars are not the same as bytes, ints, shorts or Strings.

 

 

 

 

 

 

 

 

 

 

Data Types in Turbo Pascal

(Preliminary)

Follow these fast links:

 

Char

The Char data type is used for individual 8-bit codes, many of which are used to represent printable characters. Each Char requires one byte of memory and can have a value from 0 to 255 (or 0 to 11111111 in binary). This data type, along with Integer and Boolean, belongs to the class of data types known as ordinals. An ordinal is one of a set of values, each of which has both a predecessor and a successor except for the first and the last. In Pascal, a Char literal is delimited by single quotes, e.g., 'C'.

A String can be viewed as an array of Chars.

Many different encoding schemes have been used to represent characters on computers. Below are two of the most widely used, ASCII and EBCDIC.

Back to Top

ASCII

The original American Standard Code for Information Interchange (ASCII) used only 7 of the bits, with values from 010 to 12710 or 016 to 7F16. The first 32 codes and the last one are for controling communication and peripherals. The other 95 codes are for printable characters. They were intended to handle English only and they do not even do that well. Note that the hexadecimal value for a character is composed of the column value as the high-order digit and the row value as the low-order digit, e.g., the value for lowercase 'a' is 6116. The space character is 2016. Click here to see what the control code mnemonics for the codes 0016 to 1F16 stand for.

ASCII CHART

 

0

1

2

3

4

5

6

7

0

NUL

DLE

 

0

@

P

`

p

1

SOH

DC1

!

1

A

Q

a

q

2

STX

DC2

"

2

B

R

b

r

3

ETX

DC3

#

3

C

S

c

s

4

EOT

DC4

$

4

D

T

d

t

5

ENQ

NAK

%

5

E

U

e

u

6

ACK

SYN

&

6

F

V

f

v

7

BEL

ETB

'

7

G

W

g

w

8

BS

CAN

(

8

H

X

h

x

9

HT

EM

)

9

I

Y

i

y

A

LF

SUB

*

:

J

Z

j

z

B

VT

ESC

+

;

K

[

k

{

C

FF

FS

,

<

L

\

l

|

D

CR

GS

-

=

M

]

m

}

E

SO

RS

.

>

N

^

n

~

F

SI

US

/

?

O

_

o

DEL

 

EBCDIC

An earlier, but now rarely used, encoding system was the Extended Binary Coded Decimal Interchange Code (EBCDIC), which was used on IBM mainframes. It reflected the system of holes used on punch cards. It has fallen into disfavor partially because of the use of nonsequential codes for the alphabetic characters. Note that the hexadecimal value for a character is composed of the row value as the high-order digit and the column value as the low-order digit, e.g., the value for lowercase 'a' is 8116. The space character is 4016. The nonindented sections of the table indicate unused codes. Click here to see what the control code mnemonics for the codes 0016 to 3F16 stand for.

 

 

 

 

 

 

 

 

EBCDIC CHART

 

0

1

2

3

4

5

6

7

8

9

A

B

C

D

E

F

0

NUL

SOH

STX

ETX

PF

HT

LC

DEL

SMM

VT

FF

CR

SO

SI

1

DLE

DC1

DC2

DC3

RES

NL

BS

IL

CAN

EM

CC

IFS

IGS

IRS

IUS

2

DS

SOS

FS

BYP

LF

ETB

ESC

SM

ENQ

ACK

BEL

3

SYN

PN

RS

UC

EOT

DC4

NAK

SUB

4

 

¢

.

<

(

+

|

5

&

!

$

*

)

;

¬

6

-

/

^

,

%

_

>

?

7

:

#

@

'

=

"

8

a

b

c

d

e

f

g

h

i

9

j

k

l

m

n

o

p

q

r

A

s

t

u

v

w

x

y

z

B

C

{

A

B

C

D

E

F

G

H

I

D

}

J

K

L

M

N

O

P

Q

R

E

\

S

T

U

V

W

X

Y

Z

F

0

1

2

3

4

5

6

7

8

9

 

 

 

Boolean

A variable of the Boolean type can have only two values, TRUE and FALSE. A good compiler, therefore, can use a single bit for a boolean variable, packing up to eight such variables into a single byte of memory. Zero (0) is used to represent FALSE and one (1) is used for TRUE.

A boolean is one of the ordinal types. Internally, FALSE is treated like 0 and TRUE like 1. So TRUE is the successor of FALSE and FALSE is the predecessor of TRUE.

The result of an evaluation of a Boolean expression can be assigned to a Boolean variable, e.g.,

boo : Boolean;
 
boo := 5 < 4;
writeln(boo);

will result in the word FALSE being printed.

Booleans are typically used as flags to store what a current condition is. This can be seen in a reworking of the program on the User-Controlled Repetition with the Repeat-Until Loop page.

QuitProgram : boolean; {Loop control variable}
 
Repeat
   .
   .
   .
   {-------------------User Control------------------}
   write('Do you want to do another? (Y/N) ');
   readln(response);
   if (upcase(response) = 'N') then
      QuitProgram := TRUE
Until QuitProgram

 

 

Integer

An Integer is a two-byte ordinal data type. The range possible is from -32768 to 32767. The largest value is assigned to the Pascal constant MaxInt. In the binary representation, the highest-order bit of the 16-bit value is known as the sign bit -- if it is a one (1), the value is negative.

A error which frequently occurs in programming is the choice of a data type which is too small to hold a particular value. Consider the following code, for example.

x : Integer;
 
x := MaxInt;
x := x + 1;
writeln(x)

This code will print the value -32768. This is known as overflow. Examine the following table.

Integer Range of Values

Smallest Value

Largest Value

Decimal

-32768

32767

Binary

1000 0000 0000 0000

0111 1111 1111 1111

Hexadecimal

8000

7FFF

The following binary addition demonstrates how the value stored in x became negative.

 0111 1111 1111 1111
                  +1
--------------------
 1000 0000 0000 0000

The result of the addition is negative, as can be seen by the highest-order bit being a 1. The resultant value is the binary representation of the smallest value that can be stored in an Integer variable.

Longint

An LongInt is a four-byte ordinal data type, making it twice as long as an Integer in terms of bits. The range possible is from -2147483648 to 2147483647. The largest value is assigned to the Pascal constant MaxLongInt. In the binary representation, the highest-order bit of the 32-bit value is known as the sign bit -- if it is a one (1), the value is negative.

x : LongInt;
 
x := MaxLongInt;
x := x + 1;
writeln(x)

This code will print the value -2147483648. This is known as overflow. The following table shows the smallest and largest values in decimal, binary and hexadecimal that can be stored in a LongInt.

LongInt Range of Values

 

Smallest Value

Largest Value

Decimal

-2147483648

2147483647

Binary

1000 0000 0000 0000 0000 0000 0000 0000

0111 1111 1111 1111 1111 1111 1111 1111

Hexadecimal

80000000

7FFFFFFF

Back to Top

 

Real

A Real is a six-byte non-ordinal data type. This means that it cannot be used as the loop control variable in a for-to-do loop.

The range of a positive Real variable is from 2.9 x 10-39 to 1.7 x 1038; approximately the same holds for negative values. A Real maintains about 11 significant digits.

Consider the following code fragment:

x : Real;
 
x := 13.2;
writeln(x);

This will result in 1.3200000000E+01 being printed. This is know as E-notation and is equivalent to 1.32 x 101 in scientific notation.

Since normal people find reading such notation bothersome, Pascal allows you to specify the number of decimal places to be used in the output. For example, the following

writeln(x:1:2)

would print 13.20 as output. The value after the first colon specifies the field width and the second specifies the number of decimal places to be used. If the first value is less than the total length of the string being printed, the string is printed quad left (left justified); if the first value is more than the total length, the string is printed quad right (right justified) within the field. Thus, decimal values such as those used for money can be made to line up:

x : Real;
y : Real;
 
x := 13.2;
y := 11;
writeln(x:7:2);
writeln(y:7:2);

would result in the following output, where the carets are used to indicate wordspaces.

^^13.20
^^11.00
 

ASCII CONTROL CODES

00

NUL

NULl

10

DLE

Data Link Escape

01

SOH

Start Of Heading

11

DC1

Device Control 1

02

STX

end of heading or Start of TeXt

12

DC2

Device Control 2

03

ETX

End of TeXt

13

DC3

Device Control 3

04

EOT

End Of Transmission

14

DC4

Device Control 4

05

ENQ

ENQuiry (to request identification)

15

NAK

Negative AcKnowledge

06

ACK

ACKnowledge

16

SYN

SYNchronouos idle

07

BEL

ring BELl

17

ETB

End of Transmission Block

08

BS

BackSpace

18

CAN

CANcel previous transmission

09

HT

Horizontal Tab

19

EM

End of Medium

0A

LF

Line Feed

1A

SUB

SUBstitute a character for another

0B

VT

Vertical Tab

1B

ESC

ESCape

0C

FF

Form Feed

1C

FS

File Separator

0D

CR

Carriage Return

1D

GS

Group Separator

0E

SO

Shift Out (begin non-ASCII bit string)

1E

RS

Record Separator

0F

SI

Shift In (end non-ASCII bit string)

1F

US

Unit Separator

 

 

EBCDIC CONTROL CODES

00

NUL

NULl

10

DLE

Data Link Escape

20

DS

Digit Select

30

 

 

01

SOH

Start Of Heading

11

DC1

Device Control 1

21

SOS

Start Of Significance

31

 

 

02

STX

end of heading or Start of TeXt

12

DC2

Device Control 2

22

FS

File Separator

32

SYN

SYNchronous idle

03

ETX

End of TeXt

13

DC3

Device Control 3

23

 

 

33

 

 

04

PF

Punch ofF

14

RES

REStore

24

BYP

BYPass

34

PN

Punch oN

05

HT

Horizontal Tab

15

NL

NewLine

25

LF

Line Feed

35

RS

Record Separator

06

LC

Lower Case

16

BS

BackSpace

26

ETB

End of Transmission Block

36

UC

Upper Case

07

DEL

DELete

17

IL

IdLe

27

ESC

ESCape

37

EOT

End Of Transmission

08

 

 

18

CAN

CANcel previous transmission

28

 

 

38

 

 

09

 

 

19

EM

End of Medium

29

 

 

39

 

 

0A

SMM

repeat

1A

CC

unit backspace

2A

SM

Start Message

3A

 

 

0B

VT

Vertical Tab

1B

 

 

2B

 

 

3B

 

 

0C

FF

Form Feed

1C

IFS

Interchange File Separator

2C

 

 

3C

DC4

Device Control 4

0D

CR

Carriage Return

1D

IGS

Interchange Group Separator

2D

ENQ

ENQuiry (to request identification)

3D

NAK

Negative AcKnowledge

0E

SO

Shift Out (begin non-EBCDIC bit string)

1E

IRS

Interchange Record Separator

2E

ACK

ACKnowledge

3E

 

 

0F

SI

Shift In (end non-EBCDIC bit string)

1F

IUS

Interchange Unit Separator

2F

BEL

ring BELl

3F

SUB

SUBstitute a character for another


 

 

Data Types

 

PERL has basic three data types. Scalars, arrays of scalars, and associative arrays of scalars (hashes). All three will be mentioned here with more detailed examples as the course progresses.

  • Scalars

Scalar variables always begin with a $. They can be any combination of letters, numbers, or underscores. Names that start with a digit may only contain more digits. Names that do not start with a letter, digit, or underscore are limited to one character besides the $ ($*, etc).

A scalar variable can only hold one value.

Example:

$name = "Bob";
# variable identifer is "name", the $ denotes
# that this will be a scalar value.
$number = 123; #another scalar

Scalars aren't really one type or another. You never specify that a variable will be a number, string, or whatever. All scalars are intrepreted as boolean TRUE as long as its value is not NULL or zero. This makes it very handy to use in the following sense:

$do_this = 1; #set do_this to non-NULL/zero
if ($do_this) { BLOCK }

That is, if $do_this is defined to non-NULL or zero then execute this block of statements.

  • Arrays of scalars

Arrays of scalars are much more exiciting. Entire arrays or array slices are denoted with the @ symbol. They are indexed always starting at 0 (zero - like C subscripts).

@array = (1, 2, 3);
@array = ("Bob", "John", "Mark");
@array = ($name, $number, "many things");

If you wanted to reference some data out of an array, you would reference the scalar equivalent. For example, suppose you had an array consisting of students Mark, Bob, Alan, and David. You want to know who the third student in the array is so....

@students = ("Mark", "Bob", "Alan", "David);
print "$students[2]\n";
# would print Alan. remember that the array
# starts at 0, not 1, so postition #2 is really
# the third student.

You can also assign and extract from an array by using its subscripts.

($top, $bottom) = @students[1,3];
print "$top, $bottom\n";

This example would print Bob, David since Bob and David are the 1st and 3rd positions (respectively). Notice that the array was not referenced with a $ but an @ symbol. It's because we were not using the array in a scalar context.

@students[1,3] = ("Rico", "Suave");
print "@students\n";

This replaces Bob and David in the array and the print statement prints all elements of the array.

  • Associative Arrays of Scalars (Hashes)

Hashes are denoted by % signs. A hash is comprised of a key and a corresponding value. That is,

%days = (0, Sunday, 1, Monday);
print "$days{0}\n";

would print Sunday. Since we are referencing one scalar value of %days we use $days{some key value}. The curly braces when used with an array expect a key to map to the hash. In this example, 0 maps to Sunday. Another example,

%sports = (Sunday, Football, Monday, Baseball);
print "$sports{Monday}\n";

would print Baseball. Since the above assignment to %sports isn't very clear, PERL provides "yet another way":

%sports = (Sunday => Football, Monday => Baseball);

 

 

 

 

 

 

 

 

 

 

 

تهیه کننده ابراهیم قمی