logo

 

     
 
Home
Site Map
Search
 
:: Bitwise Courses ::
 
Bitwise Dusty Archives
 
 
 

rss

 
 

ruby in steel

learn aikido in north devon

Learn Aikido in North Devon

 


Section :: C

- Format For Printing...

A Little Course in C. Part 2 – Variables and Types

Getting started with C programming
Friday 15 November 2013.
 

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;

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. 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.

This course is based on my eBook, The Little Book Of C, which is provided with my multimedia online course, C Programming For Beginners.

Previous Lesson: C Programming Fundamentals

Next lesson: Constants and #define

AddThis Social Bookmark Button


Home