Home > Articles > Programming

  • Print
  • + Share This
This chapter is from the book

Numeric Variable Types

C provides several different types of numeric variables. You need different types of variables because different numeric values have varying memory storage requirements and differ in the ease with which certain mathematical operations can be performed on them. Small integers (for example, 1, 199, and –8) require less memory to store, and your computer can perform mathematical operations (addition, multiplication, and so on) with such numbers very quickly. In contrast, large integers and floating-point values (123,000,000, 3.14, or 0.000000871256, for example) require more storage space and more time for mathematical operations. By using the appropriate variable types, you ensure that your program runs as efficiently as possible.

C's numeric variables fall into the following two main categories:

  • Integer variables hold values that have no fractional part (that is, whole numbers only). Integer variables come in two flavors: signed integer variables can hold positive or negative values, whereas unsigned integer variables can hold only positive values (and 0).

  • Floating-point variables hold values that have a fractional part (that is, real numbers).

Within each of these categories are two or more specific variable types. These are summarized in Table 3.2, which also shows the amount of memory, in bytes, generally required to hold a single variable of each type.

Table 3.2 C's numeric data types

Variable Type

Keyword

Bytes Required

Range

Character

char

1

–128 to 127

Short integer

short

2

–32767 to 32767

Integer

int

4

–2,147,483,647 to 2,147,438,647

Long integer

long

4

–2,147,483,647 to 2,147,438,647

Long long integer

long long

8

–9,223,372,036,854,775,807 to 9,223,372,036,854,775,807

Unsigned character

unsigned char

1

0 to 255

Unsigned short integer

unsigned short

2

0 to 65535

Unsigned integer

unsigned int

4

0 to 4,294,967,295

Unsigned long integer

unsigned long

4

0 to 4,294,967,295

Unsigned long long integer

unsigned long long

8

0 to 18,446,744,073,709,551,615

Single-precision floating-point

float

4

1.2E–38 to 3.4E381

Double-precision floating-point

double

8

2.2E–308 to 1.8E3082

1Approximate range; precision = 7 digits.

2Approximate range; precision = 19 digits.


NOTE

Approximate range means the highest and lowest values a given variable can hold. (Space limitations prohibit listing exact ranges for the values of these variables.) Precision is the accuracy with which a variable is stored. (For example, if you evaluate 1/3, the answer is 0.33333... with 3s going to infinity. A variable with a precision of 7 stores seven 3s.)

CAUTION

Compilers that don't support the C-99 standard may not support long long and an unsigned long long values.

Looking at Table 3.2, you might notice that the variable types int and short are identical. Why are two different types necessary? The int and short variable types are indeed identical on 32-bit Intel systems (PCs), but they might be different on other types of hardware. For example, on a VAX system, a short and an int aren't the same size. Instead, a short is 2 bytes, whereas an int is 4 bytes. Remember that C is a flexible, portable language, so it provides different keywords for the two types. If you're working on a PC, you can use int and short interchangeably.

No special keyword is needed to make an integer variable signed; integer variables are signed by default. You can, however, include the signed keyword if you wish. The keywords shown in Table 3.2 are used in variable declarations, which are discussed in the next section.

Listing 3.1 will help you determine the size of variables on your particular computer. Don't be surprised if your output doesn't match the output presented after the listing.

Listing 3.1 sizeof.c—A program that displays the size of variable types

1:  /* sizeof.c—Program to tell the size of the C variable */
2:  /*      type in bytes */
3:  
4:  #include <stdio.h>
5: 
6:  int main(void)
7:  {
8:   printf( "\nA char   is %d bytes", sizeof( char ));
9:   printf( "\nAn int   is %d bytes", sizeof( int ));
10:   printf( "\nA short   is %d bytes", sizeof( short ));
11:   printf( "\nA long   is %d bytes", sizeof( long ));
12:   printf( "\nA long long is %d bytes\n", sizeof( long long));
13:   printf( "\nAn unsigned char is %d bytes", sizeof( unsigned char ));
14:   printf( "\nAn unsigned int  is %d bytes", sizeof( unsigned int ));
15:   printf( "\nAn unsigned short is %d bytes", sizeof( unsigned short ));
16:   printf( "\nAn unsigned long is %d bytes", sizeof( unsigned long ));
17:   printf( "\nAn unsigned long long is %d bytes\n",
18:                  sizeof( unsigned long long));
19:   printf( "\nA float   is %d bytes", sizeof( float ));
20:   printf( "\nA double  is %d bytes\n", sizeof( double ));
21:   printf( "\nA long double is %d bytes\n", sizeof( long double ));
22: 
23:   return 0;
24:  }
A char   is 1 bytes
An int   is 4 bytes
A short   is 2 bytes
A long   is 4 bytes
A long long is 8 bytes

An unsigned char is 1 bytes
An unsigned int  is 4 bytes
An unsigned short is 2 bytes
An unsigned long is 4 bytes
An unsigned long long is 8 bytes

A float   is 4 bytes
A double  is 8 bytes
A long double is 12 bytes

As the preceding output shows, Listing 3.1 tells you exactly how many bytes each variable type on your computer takes. If you're using a standard 32-bit PC, your numbers should match those in Table 3.2.

Don't worry about trying to understand all the individual components of the program. Although some items are new, such as sizeof, others should look familiar. Lines 1 and 2 are comments about the name of the program and a brief description. Line 4 includes the standard input/output header file to help print the information on-screen. This is a simple program, in that it contains only a single function, main() (lines 7 through 24). Lines 8 through 21 are the bulk of the program. Each of these lines prints a textual description with the size of each of the variable types, which is done using the sizeof operator. Line 23 of the program returns the value 0 to the operating system before ending the program.

Although I said the size of the data types can vary depending on your computer platform, C does make some guarantees. There are five things you can count on:

  • The size of a char is one byte.

  • The size of a short is less than or equal to the size of an int.

  • The size of an int is less than or equal to the size of a long.

  • The size of an unsigned is equal to the size of an int.

  • The size of a float is less than or equal to the size of a double.

NOTE

Table 3.2 listed the common keyword used to identify the different variable types. The following table (Table 3.3) lists the full name of each of the data types.

As you can see from this table, short and long types are really just variations on the int type. Most programmers don't use the full name of the variable types, rather thy use the shorter version.

Table 3.3 Full names of data types

Full name

commonly used keyword

char

signed char

short

signed short int

int

signed int

long

signed long int

long long

signed long long int

unsigned char

unsigned char

unsigned short

unsigned short int

unsigned int

unsigned int

unsigned long

unsigned long int

unsigned long long

unsigned long long int


Variable Declarations

Before you can use a variable in a C program, it must be declared. A variable declaration tells the compiler the name and type of a variable. The declaration may also initialize the variable to a specific value. If your program attempts to use a variable that hasn't been declared, the compiler generates an error message. A variable declaration has the following form:

typename varname;

typename specifies the variable type and must be one of the keywords listed in Table 3.2. varname is the variable name, which must follow the rules mentioned earlier. You can declare multiple variables of the same type on one line by separating the variable names with commas:

int count, number, start;  /* three integer variables */
float percent, total;    /* two float variables */

On Day 12, "Understanding Variable Scope," you'll learn that the location of variable declarations in the source code is important, because it affects the ways in which your program can use the variables. For now, you can place all the variable declarations together just before the start of the main() function.

The typedef Keyword

The typedef keyword is used to create a new name for an existing data type. In effect, typedef creates a synonym. For example, the statement

typedef int integer;

creates integer as a synonym for int. You then can use integer to define variables of type int, as in this example:

integer count;

Note that typedef doesn't create a new data type; it only lets you use a different name for a predefined data type. The most common use of typedef concerns aggregate data types, as explained on Day 11, "Structures." An aggregate data type consists of a combination of data types presented today.

Initializing Variables

When you declare a variable, you instruct the compiler to set aside storage space for the variable. However, the value stored in that space—the value of the variable—isn't defined. It might be zero, or it might be some random "garbage" value. Before using a variable, you should always initialize it to a known value. You can do this independently of the variable declaration by using an assignment statement, as in this example:

int count;  /* Set aside storage space for count */
count = 0;  /* Store 0 in count */

Note that this statement uses the equal sign (=), which is C's assignment operator and is discussed further on Day 4, "Statements, Expressions, and Operators." For now, you need to be aware that the equal sign in programming is not the same as the equal sign in algebra. If you write

x = 12

in an algebraic statement, you are stating a fact: "x equals 12." In C, however, it means something quite different. In C it means "Assign the value 12 to the variable named x."

You can also initialize a variable when it's declared. To do so, follow the variable name in the declaration statement with an equal sign and the desired initial value:

int count = 0;
double percent = 0.01, taxrate = 28.5;

The first statement declares a variable called count as an integer and initializes it to zero. The second statement declares two variables as doubles and initializes them. The first, percent, is initialized to 0.01. The second, taxrate, is initialized to 28.5.

Be careful not to initialize a variable with a value outside the allowed range. Here are two examples of out-of-range initializations:

int weight = 100000;
unsigned int value = -2500;

The C compiler may not catch such errors. Your program might compile and link, but you might get unexpected results when the program is run.

DO understand the number of bytes that variable types take for your computer.

DO use typedef to make your programs more readable.

DO initialize variables when you declare them whenever possible.

DON'T use a variable that hasn't been initialized. Results can be unpredictable.

DON'T use a float or double variable if you're only storing integers. Although they will work, using them is inefficient.

DON'T try to put numbers that are too big or too small into a variable if its type won't hold them.

DON'T put negative numbers into variables with an unsigned type.

  • + Share This
  • 🔖 Save To Your Account

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020