Integer data types hold whole numbers. C++ has eight different integer data types. They differ based on how large of an integer they can store and whether they can store both positive and negative integers, or only nonnegative integers. The size or range of the data that can be stored in an integer data type is determined by how many bytes are allocated for storage. Because a bit can hold 2 values, 0 or 1, you can calculate the number of possible
values by calculating 2n where n is the number of bits. Given 8 bits per byte, a short integer which is allocated 2 bytes can store 216 (65,536) possible 0 and 1 combinations. If we split those between negative and positive values, the data range for a short is -32,768 to +32,767. Data Type Size* Range short 2 bytes -32,768 to +32,767 unsigned short 2 bytes 0 to +65,535 int 4 bytes -2,147,483,648 to +2,147,483,647 unsigned int 4 bytes 0 to +4,294,967,295 long 4 bytes -2,147,483,648 to +2,147,483,647 unsigned long 4 bytes 0 to +4,294,967,295 long long 8 bytes -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807 unsigned long long 8 bytes 0 to +8,446,744,073,709,551,615 *Do you notice something strange about the size numbers in this table? The int and long data types are the same size so they have the same data ranges. The C++ language specification only requires the following: In some systems, longs will be allocated more bytes than ints. The numbers in the table are for a program compiled by Visual Studio 2013 running on
Microsoft Windows 7. You can determine the memory allocated for a data type or a variable by using the Creating integer variablesAs previously discussed, the variable declaration is comprised of the type followed by the variable name followed by a semicolon. Here are a few examples.
You can declare multiple variables in a single statement. Just enter the data type and follow that with a comma separated list of variable names. End with a semicolon. The declaration statement can span multiple lines and you can assign values to the variables too.
Integer constants/literalsNumeric literals without decimal points are treated by the compiler as int values. An integer literal is also referred to as an integer constant. If you need the compiler to treat an integer constant as a long, unsigned int, or unsigned long, you can add the following suffixes to the literal value.
Floating point data typesFloating point data types are used to store real numbers (numbers that can have a fractional part). There are three C++ floating point data types, float, double, and long double.
Like the integer types, double and long double have the same size in some systems but in others, long doubles are of greater size. All floating point data types store both positive and negative numbers. The double data type is often referred to as double precision as it is twice the size of the float type and has more significant digits. Most if not all higher level mathematical operators will use double floating point data types. Floating point constants/literalsNumeric literals in decimal notation or E (exponent) notation are treated as double values. Floating point literals are often called floating point constants. Examples of floating point constants: 10.1 Note that you can use e or E and you can include or exclude the + for positive exponents. To treat a floating point constant as float or long double, use the following suffixes.
Many compilers will issue a warning if you assign a floating point constant to a float variable since you are assigning a double to a float so the number may be truncated. To prevent this, add an f suffix to the literal.
Assigning floating point values to integer variablesWhen you assign a floating point value (literal or variable) to an integer variable, the decimal part of the number is discarded.
The char data typeThe
A The C++ string classA string is sequence of characters. C++ does not include a built in string data type. However, the standard library defines a string class allowing programmers to create string variables. Using the string classYou must include the string header file.
String literalsString literals are enclosed in double quotes. String literals can include spaces and escape sequences. A simple string example
The bool data typeThe data type
The assignment statements use the words Bool data are stored in memory as int values. False is stored as 0 and true is stored as 1. Wrapping up variablesIn our discussion of variables and data types, we have shown a lot of examples of variable initialization. Variable initialization uses the assignment operator, =. The data that operators work on are called operands. The assignment operator requires two operands. The left operand must be a location in memory whose contents can be changed such as a variable. C++ refers to this as an lvalue. The right side operand is called an rvalue and it must be an expression that has a value. You will sometimes see references to l-value or lvalue in compiler error messages. When you see this, your left hand operand does not refer to a memory location whose contents can be changed. The 1 on the left is a numeric constant whose value cannot be changed. Video - Printing Variables and ConstantsVideo - More Programming, Variables and ConstantsWhat data type is used to represent real numbers?A floating-point data type uses a formulaic representation of real numbers as an approximation so as to support a trade-off between range and precision.
Which data type can store a real number?Floating point data types are used to store real numbers (numbers that can have a fractional part). There are three C++ floating point data types, float, double, and long double.
What data type is real?A 'Real' data type is numerical data which contains decimal numbers. It would be used when extra detail is required and a whole number would not provide enough information.
Which of these data types can you use for numbers?The exact numeric data types are SMALLINT , INTEGER , BIGINT , NUMERIC(p,s) , and DECIMAL(p,s) .
|