C++ Modifier & Type Qualifiers: signed, unsigned, long, short, const, volatile, Lecture notes of Compilers

An overview of data type modifiers and type qualifiers in C++. Data type modifiers include signed, unsigned, long, and short, which can be applied to integer and char base types. Type qualifiers provide additional information about variables and include const, volatile, restrict, and storage classes such as auto, register, static, extern, and mutable.

Typology: Lecture notes

2020/2021

Uploaded on 01/24/2022

Ansari_01
Ansari_01 🇵🇰

10 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Chapter No#05
Modifier Types in C++
C++ allows thechar, int,anddoubledata types to have modifiers preceding them.
A modifier is used to alter the meaning of the base type so that it more precisely fits
the needs of various situations.
The data type modifiers are listed here −
signed
unsigned
long
short
The modifierssigned, unsigned, long,andshortcan be applied to integer base
types. In addition,signedandunsignedcan be applied to char, andlongcan be
applied to double.
The modifierssignedandunsignedcan also be used as prefix
tolongorshortmodifiers. For example,unsigned long int.
C++ allows a shorthand notation for declaringunsigned, short,orlongintegers.
You can simply use the wordunsigned, short,orlong,withoutint. It automatically
impliesint. For example, the following two statements both declare unsigned
integer variables.
unsigned x;
unsigned int y;
To understand the difference between the way signed and unsigned integer
modifiers are interpreted by C++, you should run the following short program −
#include <iostream>
using namespace std;
/* This program shows the difference between
* signed and unsigned integers.
*/
int main() {
short int i; // a signed short integer
short unsigned int j; // an unsigned short integer
j = 50000;
i = j;
cout << i << " " << j;
return 0;
}
When this program is run, following is the output −
-15536 50000
pf3
pf4
pf5

Partial preview of the text

Download C++ Modifier & Type Qualifiers: signed, unsigned, long, short, const, volatile and more Lecture notes Compilers in PDF only on Docsity!

Chapter No#

Modifier Types in C++

C++ allows the char, int, and double data types to have modifiers preceding them. A modifier is used to alter the meaning of the base type so that it more precisely fits the needs of various situations. The data type modifiers are listed here −  signed  unsigned  long  short The modifiers signed, unsigned, long, and short can be applied to integer base types. In addition, signed and unsigned can be applied to char, and long can be applied to double. The modifiers signed and unsigned can also be used as prefix to long or short modifiers. For example, unsigned long int. C++ allows a shorthand notation for declaring unsigned, short, or long integers. You can simply use the word unsigned, short, or long, without int. It automatically implies int. For example, the following two statements both declare unsigned integer variables. unsigned x; unsigned int y; To understand the difference between the way signed and unsigned integer modifiers are interpreted by C++, you should run the following short program − #include using namespace std; /* This program shows the difference between

  • signed and unsigned integers. */ int main() { short int i; // a signed short integer short unsigned int j; // an unsigned short integer j = 50000 ; i = j; cout << i << " " << j; return 0 ; } When this program is run, following is the output − -15536 50000

The above result is because the bit pattern that represents 50,000 as a short unsigned integer is interpreted as -15,536 by a short.

Type Qualifiers in C++

The type qualifiers provide additional information about the variables they precede. Sr.N o Qualifier & Meaning 1 const Objects of type const cannot be changed by your program during execution. 2 volatile The modifier volatile tells the compiler that a variable's value may be changed in ways not explicitly specified by the program. 3 restrict A pointer qualified by restrict is initially the only means by which the object it points to can be accessed. Only C99 adds a new type qualifier called restrict.

Storage Classes in C++

// Function declaration void func(void); static int count = 10 ; /* Global variable */ main() { while(count--) { func(); } return 0 ; } // Function definition void func( void ) { static int i = 5 ; // local static variable i++; std::cout << "i is " << i ; std::cout << " and count is " << count << std::endl; } When the above code is compiled and executed, it produces the following result − i is 6 and count is 9 i is 7 and count is 8 i is 8 and count is 7 i is 9 and count is 6 i is 10 and count is 5 i is 11 and count is 4 i is 12 and count is 3 i is 13 and count is 2 i is 14 and count is 1 i is 15 and count is 0

The extern Storage Class

The extern storage class is used to give a reference of a global variable that is visible to ALL the program files. When you use 'extern' the variable cannot be initialized as all it does is point the variable name at a storage location that has been previously defined. When you have multiple files and you define a global variable or function, which will be used in other files also, then extern will be used in another file to give reference of defined variable or function. Just for understanding extern is used to declare a global variable or function in another file. The extern modifier is most commonly used when there are two or more files sharing the same global variables or functions as explained below.

First File: main.cpp

#include

int count ; extern void write_extern(); main() { count = 5 ; write_extern(); }

Second File: support.cpp

#include extern int count; void write_extern(void) { std::cout << "Count is " << count << std::endl; } Here, extern keyword is being used to declare count in another file. Now compile these two files as follows − $g++ main.cpp support.cpp -o write This will produce write executable program, try to execute write and check the result as follows − $./write 5

The mutable Storage Class

The mutable specifier applies only to class objects, which are discussed later in this tutorial. It allows a member of an object to override const member function. That is, a mutable member can be modified by a const member function.