Home > Tutorials > C Tutorials > Programming in C: Short and Long Variables

Programming in C: Short and Long Variables

Short and Long Integers, Double and Long Double Floating Points

C provides special integer and floating point variables to handle short and long numbers. How you use these depends on how your computer’s memory stores information.

Short and Long Integers

When declaring an integer, you can use one of three variable types:

 

Type

int

short

long

 

 

Value (range of numbers it can hold)

-32,768 to +32,767 or -2,147,438,648 to +2,147,438,647

-32,768 to +32,767

-2,147,438,648 to +2,147,438,647

 

The int value is strange, because it depends on your computer. If your system holds integers in 2 bytes of memory, then your range is -32,768 to +32,767. If your system holds integers in 4 bytes of memory, then your range is -2,147,438,648 to +2,147,438,648. To be safe, use the types short and long if you know how small or big your number will be.

Here is an example that uses both long and short integers. Notice that the format specifier for a long integer is %ld, whereas we use a regular %d for a short integer:

#include <stdio.h>
void main( )
{
long num;
short digit;
long total;
printf(“\nType a number between 300000 and 600000: “);
scanf(“%ld”, &num);
printf(“\nNow type a number between 1 and 9: “);
scanf(“%d”, &digit);
total = num / digit;
printf(“\n%ld divided by %d is %ld.\n”, num, digit, total);
 

Save this source code as eighth.c then compile, link, and run it. If you answered the questions with a long number of 45678 and a short digit of 7, then your output will be:

45678 divided by 7 is 6525.  

Double and Long Double Floating Points

Like integers, floating point variables have three sizes:

 

Type

Value

Sample

Exponent

Written in C

 

float up to 6 digits

5.3

0.53×101

0.53E1

 

double up to 15 digits

123.5678

0.12345678×103

0.12345678E3

 

long double up to 19 digits

.0000000001235678

0.12345678×10-9

0.12345678E-9

 

In C we use the E to mean “exponent”. We use the floating point format specifier %f with each of these floating point variables.

Using %.f with Very Large and Very Small Numbers

If you are working with very large or small numbers, you must specify how many decimal and integer places you want to print on the screen. For example:

 

Type

Number

Format Specifier

Result

Explanation

 

Large Number 0.2E7

%7.0f

2000000

7 integer places
and 0 decimal places

 

Small Number 0.36E-4

%.6f

0.000036

4 decimal places
plus 2 more (for the .36)

 

Example Program

Here is an example that uses both a regular integer and a double floating point number:

#include <stdio.h>
void main( )
{
double acid = 1.3E-9;
int water = 5;
printf(“\nWhat do you get when you mix:”);
printf(“\n%.10f liters of sulfuric acid into %d liters of water?”, acid, water);
printf(“\nA terrible smell!\n”);
 

In this example, we use %.10f because 9 decimal places plus 1 (for the .3) equals 10. Save your code as acid.c then compile, link, and execute it.

You only have to worry about special integers and floating point numbers if you are working with extremely large or small numbers, for example, in scientific research. For everyday use,int and float work perfectly well.

Continue on to Variables & Strings.

About Michael Boguslavskiy

Michael Boguslavskiy is a full-stack developer & online presence consultant based out of New York City. He's been offering freelance marketing & development services for over a decade. He currently manages Rapid Purple - and online webmaster resources center; and Media Explode - a full service marketing agency.

Check Also

How To Populate A DIV via an HTML SelectBox, PDO, and jQuery

Imagine you have a database full of information, and you want to let users browse ...

Like every other website, this site uses cookies to analyze our traffic. Cookies may also be utilized by our advertisers and partners. By using this website you agree to the use of said cookies. More Information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings, continuing to navigate past this message, or you click "Accept" below then you are consenting to the use of cookies on the Rapid Purple website.

Close