The float and double Data Types
and the sizeof Operator

Prev: The int Data Type
Next: Arithmetic Operators



The float Data Type

To store variables correct to six decimal places, you can use the float data type. This also allows you to store numbers greater than 4294967295, if an unsigned long int is still insufficient!

Floats are relatively easy to use but problems tend to occur when performing division.

In general:

An int divided by an int returns an int.
An int divided by a float returns a float.
A float divided by an int returns a float.
A float divided by a float returns a float.

As an example, 3 is considered as an int, but 3.0 is considered as a float.

If you want to store the result of a division as a floating-point (decimal) number, make sure you store it in a float declared variable.

If you want the decimal result of the division between two integers, you can use a method called COERSION (or CASTING). This involves placing (float) before an expression you wish to cast.

#include <stdio.h>
int main() {

  float a,b,c,d,e,f;
  
  a = 1/3;
  b = 1/3.0;
  c = 1.0/3;
  d = 1.0/3.0;
  e = (float)1/3;
  f = (float)(1/3);
  
  printf("1 divided by 3 is %f\n", a);
  printf("1 divided by 3.0 is %f\n", b);
  printf("1.0 divided by 3 is %f\n", c);
  printf("1.0 divided by 3.0 is %f\n\n", d);

  printf("The float-casting of \n");
  printf(" 1, divided by 3 is %f\n\n", e);

  printf("f equals %f\n", f);
  
  return 0;
}

The output of the example is:

1 divided by 3 is 0.000000
1 divided by 3.0 is 0.333333
1.0 divided by 3 is 0.333333
1.0 divided by 3.0 is 0.333333

The float-casting of
   1, divided by 3 is 0.333333

f equals 0.000000

Firstly, six variables of the float data type are declared and initialized with the return values of various calculations:

a stores the result of an int divided by an int. The integer value of 0.333333... is zero, so 0.000000 is assigned to a.

b stores the result of an int divided by a float, so 0.333333 as expected.

c stores the result of a float divided by an int, so 0.333333 as expected.

d stores the result of a float divided by a float, so 0.333333 as expected.

With e, we've used casting. (float) occurs just before the 1, so 1 is effectively 1.0, therefore the result of 1.0 divided by 3 gets assigned to e, that is, 0.333333.

We've also used casting for f, but not in the correct manner. Because of the brackets, 1 divided by 3 gets evaluated first, then the result gets casted to a float. Since 1 divided by 3 returns 0, 0.000000 gets assigned to f.

Now, the printf functions prints out most of the text between the double quote marks with a few exceptions.

\n is known as the NEWLINE character - it acts like a line break, and is commonly used to make the output look neater.

%f is a FORMAT SPECIFIER and is replaced with the float-value of specified expressions.

Format specifiers will be discussed at a later date - for now you need to understand why a and f gets assigned 0.000000 and b to e gets assigned 0.333333.

Finally, notice that we are unable to store certain fractions like a third, exactly in C.



The double Data Type

A long int allows you to store integers within a very large range. If this is insufficient you can store very very large / small numbers and decimals correct to ten decimal places using the double data type. However, with larger numbers accuracy can be lost.

Also, variables of the double data type takes up twice the amount of memory as a variable of the float data type (hence the name).



Scientific Notation

It is possible to express numbers in scientific notation, which is handy if the numbers get very large or small.

In C, scientific notation is of the form xey - you should replace x and y with numbers (y must be an integer).

Basically, xey translates to "x times 10 to the power of y".

For example, 1.2e3 is 1.2*1000 = 1200
1.23e4 is 1.23*10000 = 12300
4.5e-2 is 4.5*0.01 = 0.045

We won't be using scientific notation in these tutorials.



What is an Operator?

An OPERATOR is like a function, where things get worked out, or EVALUATED. As a simple example, in x+y the addition operator causes the evaluation of x plus y. For an operator to work, it requires one or more OPERAND. So in the previous example, x and y are both operands. Without one or the other, the addition operator won't work.

Like functions, operators RETURN values, which is the result of the evaluation of the operation. So 2+4 will return 6.



The sizeof Operator

The sizeof operator enables you to find out how many BYTES a variable or data type occupies. A byte is defined as EIGHT BINARY DIGITS (or "8-bits"). Binary numbers are covered later. For now, think of a byte as a container in the computer's memory.

The sizeof operator takes one operand, for example, sizeof(int) would return the size of the int data type. Notice the strange bracketing required for the sizeof operator. This causes confustion as to what sizeof really is - a function or an operator? I've been taught that it's an operator.

Anyway, you can use a data type as an operand, to find out how much memory is required to store variables of that data type, as demonstrated by this example:

#include <stdio.h>

int main() {
  printf("Size of int is %d bytes\n",sizeof(int));
  printf("Size of short int is %d bytes\n",sizeof(short int));
  printf("Size of long int is %d bytes\n\n", sizeof(long int));
  
  printf("Size of signed int is %d bytes\n",sizeof(signed int));
  printf("Size of signed short int is %d bytes\n",
          sizeof(signed short int));
  printf("Size of signed long int is %d bytes\n\n", 
          sizeof(signed long int));
  
  printf("Size of unsigned int is %d bytes\n",sizeof(signed int));
  printf("Size of unsigned short int is %d bytes\n",
          sizeof(unsigned short int));
  printf("Size of unsigned long int is %d bytes\n\n",
          sizeof(unsigned long int));
  
  printf("Size of char is %d byte\n",sizeof(char));
  printf("Size of float is %d bytes\n",sizeof(float));
  printf("Size of double is %d bytes\n", sizeof(double));

  return 0;
}

Notice the use of extra white space to add readability to the code.

The output shows that the amount of memory required is not affected by the signed or unsigned type modifiers:

Size of int is 2 bytes
Size of short int is 2 bytes
Size of long int is 4 bytes

Size of signed int is 2 bytes
Size of signed short int is 2 bytes
Size of signed long int is 4 bytes

Size of unsigned int is 2 bytes
Size of unsigned short int is 2 bytes
Size of unsigned long int is 4 bytes

Size of char is 1 byte
Size of float is 4 bytes
Size of double is 8 bytes

You can also pass the function a declared variable, to find out how much memory that variable occupies.



Prev: The int Data Type
Next: Arithmetic Operators

www.iota-six.co.uk Copyright © 2001-2003
Unauthorized copying not permitted

Designed and Developed Using Macromedia Studio MX