This post is the first of my new section “Chaff’s Code”, and I hope you will find it useful as an addition to a current project or perhaps inspiration toward hitting that keyboard again and writing that program you have had stuck in your head for what feels like months.

The entire source is quite small, although very efficient for demonstration. The source is below:

/*
* Integer Vs. Floating Point Number Demonstration.c
*
* Created on: 19/02/2011
* Author: Dillon Chaffey
*/
#include stdio.h
int main()
{
int int_num1, int_num2, int_num3; /* Line to the left declares three integers by using “int” and then naming the integer variables beside it, which are only allowed to be whole numbers, and are separated by commas of course and ended with a semicolon */
float float_num1, float_num2, float_num3; /* Declares floating point numbers (integer variables with a decimal, i.e 1.39 as opposed to an int which would support whole numbers only, separated by commas) */
int_num1 = 30 / 10; /* Both the divisor and dividend are integers. — Declares that the integer variable “int_num1” is now equal to (30 divided by ten), which is 3 */
float_num1 = 30 / 10;
int_num2 = 46.0 * 75; /* Only the dividend is an integer, the divisor is a float (floating point number) */
float_num2 = 46.0 * 75;
int_num3 = 40 / 8.0; /* Declares the equation “40 divided by 8.0”, which equals ‘5.0’ */
float_num3 = 40 / 8.0;
printf(“The integer representation of the equation ’30 / 10′ is: %d\n”, int_num1);
printf(“The float (floating point) representation of the equation ’30 / 10′ is: %f\n”, float_num1);
printf(“The integer representation of the equation ‘46.0 * 75’ is: %d\n”, int_num2);
printf(“The float representation of the equation ‘46.0 * 75′ is: %f\n”, float_num2);
printf(“The integer representation of the equation ’40 / 8.0′ is: %d\n”, int_num3);
printf(“The float representation of the equation ’40 * 8.0’ %f\n”, float_num3);
return 0;
}

 

The above code was written by myself in a very short period of time, although the potential assistance it could render to a person new to the C language is limitless, assuming they are interested in the mathematical side of programming.

I hope that this source will help you and others in some way or another. Enjoy your newfound C knowledge.

Advertisements