Docsity

Data Types in C++: Understanding Variables and Memory Allocation, Lecture notes of Compilers

Toggle sidebar
bg1
ANSARI ACADEMY OF CS & IT
MUHAMMAD IMRAN ANSARI
BS COMPUTER SCIENCE
Chapter No#03
Data Types in C++
While writing program in any language, you need to use various variables to store
various information. Variables are nothing but reserved memory locations to store
values. This means that when you create a variable you reserve some space in memory.
You may like to store information of various data types like character, wide character,
integer, floating point, double floating point, boolean etc. Based on the data type of a
variable, the operating system allocates memory and decides what can be stored in the
reserved memory.
Primitive Built-in Types
C++ offers the programmer a rich assortment of built-in as well as user defined data
types. Following table lists down seven basic C++ data types โˆ’
Type
Keyword
Boolean
bool
Character
char
Integer
int
Floating point
float
Double floating point
double
Valueless
void
Wide character
wchar_t
Several of the basic types can be modified using one or more of these type modifiers โˆ’
pf3
pf4
pf5
pf8

Partial preview of the text

Download Data Types in C++: Understanding Variables and Memory Allocation and more Lecture notes Compilers in PDF only on Docsity!

MUHAMMAD IMRAN ANSARI

Chapter No#

Data Types in C++

While writing program in any language, you need to use various variables to store various information. Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. You may like to store information of various data types like character, wide character, integer, floating point, double floating point, boolean etc. Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory.

Primitive Built-in Types

C++ offers the programmer a rich assortment of built-in as well as user defined data types. Following table lists down seven basic C++ data types โˆ’ Type Keyword Boolean bool Character char Integer int Floating point float Double floating point double Valueless void Wide character wchar_t Several of the basic types can be modified using one or more of these type modifiers โˆ’

MUHAMMAD IMRAN ANSARI

  • signed
  • unsigned
  • short
  • long The following table shows the variable type, how much memory it takes to store the value in memory, and what is maximum and minimum value which can be stored in such type of variables. Type Typical Bit Width Typical Range char 1byte - 127 to 127 or 0 to 255 unsigned char 1byte 0 to 255 signed char 1byte - 127 to 127 int 4bytes - 2147483648 to 2147483647 unsigned int 4bytes 0 to 4294967295 signed int 4bytes - 2147483648 to 2147483647 short int 2bytes - 32768 to 32767 unsigned short int 2bytes 0 to 65, signed short int 2bytes - 32768 to 32767 long int 8bytes - 2,147,483,648 to 2,147,483, signed long int 8bytes same as long int

MUHAMMAD IMRAN ANSARI

When the above code is compiled and executed, it produces the following result which can vary from machine to machine โˆ’ Size of char : 1 Size of int : 4 Size of short int : 2 Size of long int : 4 Size of float : 4 Size of double : 8 Size of wchar_t : 4

typedef Declarations

You can create a new name for an existing type using typedef. Following is the simple syntax to define a new type using typedef โˆ’ typedef type newname; For example, the following tells the compiler that feet is another name for int โˆ’ typedef int feet; Now, the following declaration is perfectly legal and creates an integer variable called distance โˆ’ feet distance;

Enumerated Types

An enumerated type declares an optional type name and a set of zero or more identifiers that can be used as values of the type. Each enumerator is a constant whose type is the enumeration. Creating an enumeration requires the use of the keyword enum. The general form of an enumeration type is โˆ’ enum enum-name { list of names } var-list; Here, the enum-name is the enumeration's type name. The list of names is comma separated. For example, the following code defines an enumeration of colors called colors and the variable c of type color. Finally, c is assigned the value "blue". enum color { red, green, blue } c; c = blue; By default, the value of the first name is 0, the second name has the value 1, and the third has the value 2, and so on. But you can give a name, a specific value by adding an initializer. For example, in the following enumeration, green will have the value 5. enum color { red, green = 5, blue }; Here, blue will have a value of 6 because each name will be one greater than the one that precedes it.

MUHAMMAD IMRAN ANSARI

Variable Types in C++

A variable provides us with named storage that our programs can manipulate. Each variable in C++ has a specific type, which determines the size and layout of the variable's memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable. The name of a variable can be composed of letters, digits, and the underscore character. It must begin with either a letter or an underscore. Upper and lowercase letters are distinct because C++ is case-sensitive โˆ’ There are following basic types of variable in C++ as explained in last chapter โˆ’ Sr.No Type & Description 1 bool Stores either value true or false. 2 char Typically a single octet (one byte). This is an integer type. 3 int The most natural size of integer for the machine. 4 float A single-precision floating point value. 5 double A double-precision floating point value. 6 void Represents the absence of type. 7 wchar_t A wide character type.

MUHAMMAD IMRAN ANSARI

a variable multiple times in your C++ program, but it can be defined only once in a file, a function or a block of code.

Example

Try the following example where a variable has been declared at the top, but it has been defined inside the main function โˆ’ #include using namespace std; // Variable declaration: extern int a, b; extern int c; extern float f; int main () { // Variable definition: int a, b; int c; float f; // actual initialization a = 10 ; b = 20 ; c = a + b; cout << c << endl ; f = 70.0/3.0; cout << f << endl ; return 0 ; } When the above code is compiled and executed, it produces the following result โˆ’ 30

Same concept applies on function declaration where you provide a function name at the time of its declaration and its actual definition can be given anywhere else. For example โˆ’ // function declaration int func(); int main() { // function call int i = func(); }

MUHAMMAD IMRAN ANSARI

// function definition int func() { return 0; }

Lvalues and Rvalues

There are two kinds of expressions in C++ โˆ’

  • lvalue โˆ’ Expressions that refer to a memory location is called "lvalue" expression. An lvalue may appear as either the left-hand or right-hand side of an assignment.
  • rvalue โˆ’ The term rvalue refers to a data value that is stored at some address in memory. An rvalue is an expression that cannot have a value assigned to it which means an rvalue may appear on the right- but not left-hand side of an assignment. Variables are lvalues and so may appear on the left-hand side of an assignment. Numeric literals are rvalues and so may not be assigned and can not appear on the left- hand side. Following is a valid statement โˆ’ int g = 20; But the following is not a valid statement and would generate compile-time error โˆ’ 10 = 20;