Fundamental types of Constants and Variable in C++

Page Contents:

Fundamental types
void
bool type
What are the variable types in C?
The Signed and Unsigned modifier
Unsigned vs Signed integer in C++
Character type
wchar_t
What is a floating-point data type?
Type float
String type

Fundamental types:

It is divided into three parts:1) floating-point, integral and void.

void:

  • The void is syntactically a fundamental type
  • It is an empty set of values where No variable of the void can have a return value
  • It is used either to specify that a function does not return a value or as the base type for pointers to objects of unknown type
void x;  //error:there are no void object void f(); // function f does not return a value void *pv;  //pointer to object of unknown type

bool type:

  • We have seen that the variable of type int can have billions of possible values, and those of type char can have 256.
  • Variable of type bool can have only two possible value: true and false
  • bool type requires only one bit(not byte) of storage, but in practice compilers often store them as bytes because a byte can be quickly accessed, while an individual bit must be extracted from a byte, which requires additional time

What are the variable types in C?

Fundamental Types
Fundamental Types

The Signed and Unsigned modifier:

  • The short, int and long types are normally interpreted assigned with the highest bit representing the sign
  • However integral types can be preceded by the keyword unsigned
  • The amount of memory required remains unaltered but the range of values changed due to the highest bit no longer being required as a sign
  • Thus three types are available: char, signed char and unsigned char
  • By eliminating the sign of the character and integer types, you can change their range to start at 0 and include an only positive number
  • This allows them to represent numbers twice as big as the signed type. The unsigned types are used when the quantities represented are always positive such as representing a count of something or when the positive range of the signed types is not quite large enough
  • To change an integer type top an unsigned type, preceding the data type keyword with the keyword unsigned
  • For an example of an unsigned variable of type, char would be defined as
unsigned char ucharvar;

Unsigned vs Signed integer in C++

  • signed integers will allow you to represent numbers both in the positive and negative ranges
  • unsigned integers will only allow you to represent numbers in the positive or zero it cannot be negative
  • Both the unsigned and signed variables have the same range
  • The unsigned variable can represent a larger magnitude number than signed variable
  • An unsigned byte can represent values from 0 to 255, while signed byte can represent -128 to 127.

declaration of both signed and unsigned integers are:

signed int negative=-44;  unsigned int number=44;

Character type:

  • Universally, a char has 8 bits so that it can hold one of 256 different values
  • The 256 values represented by an 8-bit byte can be interpreted as the values 0 to 225 or as the values -127 to 127
  • signed char which can hold at least the values -127 to 127, and unsigned char, which can hold at least the values 0 to 225

wchar_t:

  • A type char_t is provided to hold characters of a larger character set such as Unicode
  • The size of wchar_t is implementation-defined and large enough to hold the largest character set
  • wchar_t is a typedef (type define) rather than a built-in type. The suffix _ t was added to distinguish standard typedef
  • A variable of wchar_t holds wide-character or multibyte character

What is a floating-point data type?

  • Floating-point variables represent numbers with a decimal place like 3.1415927,0.054452 and -10.2
  • They have both an integer part, to the left of the decimal point, and a fractional part, to the right
  • Floating-point variables represent what mathematics call real numbers, which are used for measurable quantities such as distance, area, and temperature. They typically have a fractional part
  • There are three kinds of floating-point variables in C++: type float, type double and type long double

Type float:

  • Type float stores numbers in the range of 3.4X10^-38 to 3.4X10^38, with a precision of seven-digit
  • It occupies 4 bytes(32 bits) in memory
  • Program for type float is given below:
#include<iostream>
using namespace std;
int main()
{
float rad;
const float PI=3.14159F;     //type const float
cout<< "Enter radius of circle:";  //prompt
cin>>rad;                         //get radius
float area=PI*rad*rad;      //find area
cout<< "Area is"  << area << endl;  //display result
return 0;
}

OUTPUT:

Enter the radius of the circle: 0.5
Area is 0.785398

Type double and long double:

  • The larger floating-point types, double and long double, are similar to float except that they require more memory space and provide a wider range of values and more precision
  • Type double requires 8 bytes of storage and handles numbers in the range from 1.7X10^-308 to 1.7X10^308 with a precision of 15 digits
  • Type long double is compiler-dependent but is often the same as double

Floating-Point Constants:

  • The number 3.14159F in the circle area is an example of a floating-point constant.
  • The decimal point signals that it is floating-point constant, and not an integer, and the F specifies that it’s type float, rather than double or long double
  • You can also write floating-point constants using exponential notation. Exponential notation is a way of writing large numbers without having to write out a lot of zero. (the exponent can be positive or negative)

String type:

A string is a series of characters treated as a single unit. A string may include letters, digits and various special characters such as +,-,*,/ and $. String literals, or string constants, in C++ are written in double quotes like this:

"Hello friends"
"How are You"

A string in C++ is an array of characters ending in the null character(‘/0’), which specifies where the string terminates in memory

Translate »