What is a long data type

Integer data types

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:

  • ints are at least as big as shorts
  • longs are at least as big as ints

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.

Checking memory allocation using the sizeof operator

You can determine the memory allocated for a data type or a variable by using the sizeof operator.

int main()
{
  long myLongVariable = 1000;

  // use sizeof with types
  cout << "bytes for short: " << sizeof(short) << endl;
  cout << "bytes for int: " << sizeof(int) << endl;
  cout << "bytes for long: " << sizeof(long) << endl;

  // use sizeof with variable
  cout << "bytes for myLongVariable: " << sizeof(myLongVariable) << endl;

  return 0;
}

What is a long data type

Creating integer variables

As previously discussed, the variable declaration is comprised of the type followed by the variable name followed by a semicolon. Here are a few examples.

unsigned long myVariable1;
short myVariable2;
unsigned int myVariable3;

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.

int var1, var2, var3, var4; // creating 4 int variables

short sVar1, // variable declaration can span multiple lines
  sVar2,
  sVar3;

long lVar1 = 10, lVar2 = 20, lVar3 = 30; // creating and assigning 3 long variables 

Integer constants/literals

Numeric 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.

Suffix

Type modifier

Example

u or U (case does not matter)

unsigned

5u (unsigned int)

l or L

long

5L

u/U and l/L combinations

unsigned long

5ul, 5lu (order does not matter)

Floating point data types

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.

Data Type

Size*

Range

Significant Digits

float

4 bytes

±3.4E-38 and ±3.4E38

7

double

8 bytes

±1.7E-308 and ±1.7E308

16

long double

8 bytes

±1.7E-308 and ±1.7E308

16

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/literals

Numeric 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
1.5E5
1.5e5
1.5e+5
2.63E-6
2.63e-6

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.

Suffix

Type modifier

Example

f or F (case does not matter)

float

10.1f

l or L

long double

10.1L

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.

What is a long data type

To prevent this, add an f suffix to the literal.

float myFloatVar;

myFloatVar = 10.1f; // add f suffix to define float literal

Assigning floating point values to integer variables

When you assign a floating point value (literal or variable) to an integer variable, the decimal part of the number is discarded.

int demoInt;
  
demoInt = 1.234; // assign a floating point constant to an int variable

cout << "value of demoInt: " << demoInt << endl;

float demoFloat = 4.567;

demoInt = demoFloat; // assign a float to an int variable 

cout << "value of demoInt after assigning demoFloat: " << demoInt << endl;

What is a long data type

The char data type

The char data type is used for single characters. A char literal is a single character enclosed in single quotes.

char demoChar = 'a';   // 'a' is a char literal

A char variable normally uses 1 byte of memory.

The C++ string class

A 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 class

You must include the string header file.

#include <string>

String literals

String literals are enclosed in double quotes. String literals can include spaces and escape sequences.

A simple string example

#include <iostream>
#include <string> // we want to work with strings so we include this header
                  // from standard library
using namespace std;

int main()
{
  string myName = "Tim";  // string literals are enclosed in double quotes

  cout << "My name is: " << myName << endl;
  return 0;
}

The bool data type

The data type bool is for Boolean variables. Boolean data are either true or false.

bool myBooleanVariable = false;  // initialize as false

myBooleanVariable = true; // switch value to true

The assignment statements use the words true or false and not the strings “true” and “false”.

Bool data are stored in memory as int values. False is stored as 0 and true is stored as 1.

bool myBooleanVariable = false;  // initialize as false

cout << "output bool defined as false: " << myBooleanVariable << endl;

myBooleanVariable = true; // switch value to true

cout << "output bool defined as true: " << myBooleanVariable << endl; 

What is a long data type
 

Wrapping up variables

In 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.

1 = 2; // 1 does not reference a changeable memory location so it is not an lvalue

What is a long data type
 

The 1 on the left is a numeric constant whose value cannot be changed.

Video - Printing Variables and Constants

Video - More Programming, Variables and Constants

What is long in datatype?

Long (long integer) variables are stored as signed 32-bit (4-byte) numbers ranging in value from -2,147,483,648 to 2,147,483,647. The type-declaration character for Long is the ampersand (&).

WHAT IS LONG datatype used for?

Remarks. Use the Long data type to contain integer numbers that are too large to fit in the Integer data type. The default value of Long is 0.

What is long data type size?

Windows 64-bit applications.

What is data type long in C?

The long data type stores integers like int , but gives a wider range of values at the cost of taking more memory. Long stores at least 32 bits, giving it a range of -2,147,483,648 to 2,147,483,647. Alternatively, use unsigned long for a range of 0 to 4,294,967,295.