*This is part 3 of my series on C programming for beginners.*

See also: part 2.

When you want to store values in your programs you need to declare variables. A variable is simply a name (more formally, we’ll call it an ‘identifier’) to which some value can be assigned. A variable is like the programming equivalent of a labelled box. You might have a box labelled ‘Petty Cash’ or a variable named

**pettycash**. Just as the contents of the box might vary (as money is put into it and taken out again), so the contents of a variable might change as new values are assigned to it. You assign a value using the equals sign (=).

In C a variable is declared by stating its data-type (such as

**int**for an integer variable or

**double**for a floating-point variable) followed by the variable name. You can invent names for your variables and, as a general rule, it is best to make those names descriptive.

This is how to declare a floating-point variable named

**mydouble**with the double data-type:

double mydouble;

You can now assign a floating-point value to that variable:

mydouble = 100.75;

Alternatively, you can assign a value at the same time you declare the variable:

double mydouble = 100.75;

### FLOATING-POINT NUMBERS

There are several data types which can be used when declaring floating point variables in C. The

**float**type represents single-precision numbers;

**double**represents double-precision numbers and

**long double**represents higher precision numbers. In this course, I shall normally use

**double**for floating-point variables.

### INTEGERS AND FLOATS

Now let’s look at a program that uses integer and floating point variables to do a calculation. My intention is to calculate the grand total of an item by starting with its subtotal (minus tax) and then calculating the amount of tax due on it by multiplying that subtotal by the current tax rate. Here I’m assuming that tax rate to be 17.5% or, expressed as a floating point number, 0.175. Then I calculate the final price – the grand total – by adding the tax onto the subtotal. This is my program:

#include <stdio.h>

int main(int argc, char **argv) {

int subtotal;

int tax;

int grandtotal;

double taxrate;

taxrate = 0.175;

subtotal = 200;

tax = subtotal * taxrate;

grandtotal = subtotal + tax;

printf( "The tax on %d is %d, so the grand total is %d.\n",

subtotal, tax, grandtotal );

return 0;

}

Once again, I use

**printf**to display the results. Remember that the three place--markers,

**%d**, are replaced by the values of the three matching variables:

**subtotal**,

**tax**and

**grandtotal**.

When you run the program, this is what you will see:

*The tax on 200 is 34, so the grand total is 234.*

But there is a problem here. If you can’t see what it is, try doing the same calculation using a calculator. If you calculate the tax, 200 * 0.175, the result you get should be 35. But my program shows the result to be 34.

This is due to the fact that I have calculated using a floating-point number (the double variable,

**taxrate**) but I have assigned the result to an integer number (the int variable,

**tax**). An integer variable can only represent numbers with no fractional part so any values after the floating point are ignored. That has introduced an error into the code.

The error is easy to fix. I just need to use floating-point variables instead of integer variables. Here is my rewritten code:

#include <stdio.h>

int main(int argc, char **argv) {

double subtotal;

double tax;

double grandtotal;

double taxrate;

taxrate = 0.175;

subtotal = 200;

tax = subtotal * taxrate;

grandtotal = subtotal + tax;

printf( "The tax on %.2f is %.2f, so the grand total is %.2f.\n",

subtotal, tax, grandtotal );

return 0;

}

This time all the variables are doubles so none of the values is truncated. I have also used the float

**%f**specifiers to display the float values in the string which I have passed to the

**printf**function. In fact, you will see that the format specifiers in the string also include a dot and a number numbers like this:

**%.2f**. This tells

**printf**to display at least two digits to the right of the decimal point.

You can also format a number by specifying its width – that is, the minimum number of characters it should occupy in the string. So if I were to write

**%3.2**that would tell

**printf**to format the number in a space that takes up at least 3 characters with at least two digits to the right of the decimal point. Try entering different numbers in the format specifiers (e.g.

**%10.4f**) to see the effects these numbers have. Here are examples of numeric formatting specifiers that can be used with

**printf**:

### NUMERIC FORMAT SPECIFIERS

%d print as decimal integer

%4d print as decimal integer, at least 4 characters wide

%f print as floating point

%4f print as floating point, at least 4 characters wide

%.2f print as floating point, 2 characters after decimal point

%4.2f print as floating point, at least 4 wide and 2 after decimal point

This series of C programming lessons is based on my book,

**The Little Book Of C**, which is the course text for my online video-based course,

**C Programming For Beginners**, which teaches C programming interactively in over 70 lessons including a source code archive, eBook and quizzes. For information on this courses see HERE.