More About Variables in C

In C, when one int is divided by another int, the result is also an int. If we divide six by three all’s well, but what if we divide five by three? We find that any fractional part is discarded, this is called truncation.

One answer is to use data types that can handle decimal numbers, we will cover that a little later. In this example program, however, we will use the modulus operator to find the remainder. The modulus operator is specified by the percent sign, %, and using it gives us the remainder of a division operation.

 #include <stdio.h>

int main(void){
 int a = 73;
 int b = 451;

 int answer;

 answer = a + b;
 printf("a + b = %d\n", answer);
 
 answer = a - b;
 printf("a - b = %d\n", answer);
 
 answer = a * b;
 printf("a * b = %d\n", answer);
 
 answer = b / a;
 printf("b / a = %d\n", answer);
 
 /*
  * int data type doesn't handle
  * decimals
  * let's find the remainder
  */
 answer = b % a;
 printf("oh, with a remainder of %d.\n", answer);
 
 
 return 0;
 
 
}

Let’s use the modulus operator to convert a number of inches into yards, feet, and inches.

#include <stdio.h> 


int main(void){
 
 int totalInches, inches, yards, feet;

 totalInches = 150;
 
 inches = totalInches;
 
 yards = inches % 36;
 
 inches = inches % 36;
 
 feet = inches % 12;
 
 feet = inches % 12;
 
 printf("%d inches is %d yards, %d feet and %d inches\n", totalInches, yards, feet, inches);
 
 return 0;
 
 

Having used the int data type quite a bit, it’s time to learn about floating point numbers. Floating point numbers can be either whole or fractional, because they have a fractional portion called the mantissa. They are able to have a fractional portion because they are stored differently in the computer than integers.

We will use the keyword float to declare a variable that will be used to store a floating point value. When we declare a variable as a float, some storage is allocated to store a floating point value that can be referred to using the variable name. We can then use the assignment operator, =, to store a decimal value in that allocated memory space.

When using a float we must use a different conversion specifier in the control string. Instead of embedding an %d, we must instead use a %f.

 #include <stdio.h>

int main(void){
  /*
   * declare a variable called
   * price
   */
  float price = 1.05;
 
  printf("The price is %f\n", price);
 
 
  /*
   * bad things happen
   * when we use the wrong
   * conversion specifier
   */
  printf("not %d\n", price);
 
 return 0;
}

Floating point numbers come in two types, float and double. The double data type has roughly twice the precision as float, but takes up more memory. In terms of calculations, float and double are treated identically, because float variables are automatically converted to double values before they are used in any operation. Note that they both use the same format character, which is %f.

#include <stdio.h>

int main(void){
 
 double x = 2.718;
 double y = 1.618;
 
 double answer;
 
 answer = x + y;
 printf("%f + %f = %f\n", x, y, answer);
 
 answer = x - y;
 printf("%f - %f = %f\n", x, y, answer);
 
 answer = x * y;
 printf("%f - %f = %f\n", x, y, answer);
 
 answer = x / y;
 printf("%f / %f = %f\n", x, y, answer);
 
 
 return 0;
}

 

 

 

 

Advertisements

2 thoughts on “More About Variables in C

  1. Just want to say your article is as surprising.

    The clarity in your post is just great and i could assume you are an expert on this subject.

    Fine with your permission let me to grab your feed to keep up to
    date with forthcoming post. Thanks a million and please carry on the rewarding work.

  2. An outstanding share! I have just forwarded this onto a coworker who was doing a
    little research on this. And he actually bought me lunch due to the fact that I discovered it for
    him… lol. So let me reword this…. Thank YOU for the meal!!
    But yeah, thanx for spending the time to talk
    about this issue here on your website.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s