Macro Constants
Name |
Expresses |
---|---|
CHAR_MIN |
The minimum value for an object of type char |
CHAR_MAX |
Maximum value for an object of type char |
SCHAR_MIN |
The minimum value for an object of type Signed char |
SCHAR_MAX |
Maximum value for an object of type Signed char |
UCHAR_MAX |
Maximum value for an object of type Unsigned char |
CHAR_BIT |
Number of bits in a char object |
MB_LEN_MAX |
Maximum number of bytes in a multi-byte character |
SHRT_MIN |
The minimum value for an object of type short int |
SHRT_MAX |
Maximum value for an object of type short int |
USHRT_MAX |
Maximum value for an object of type Unsigned short int |
INT_MIN |
The minimum value for an object of type int |
INT_MAX |
Maximum value for an object of type int |
UINT_MAX |
Maximum value for an object of type Unsigned int |
LONG_MIN |
The minimum value for an object of type long int |
LONG_MAX |
Maximum value for an object of type long int |
ULONG_MAX |
Maximum value for an object of type Unsigned long int |
LLONG_MIN |
The minimum value for an object of type long long int |
LLONG_MAX |
Maximum value for an object of type long long int |
ULLONG_MAX |
Maximum value for an object of type Unsigned long long int |
The actual value depends on the particular system and library implementation but shall reflect the limits of these types in the target platform. LLONG_MIN, LLONG_MAX, and ULLONG_MAX are defined for libraries complying with the C standard of 1999 or later (which only includes the C++ standard since 2011: C++11).
C++ Program to Find the Range of Data Types using Macro Constants
Example:
C++
// C++ program to Demonstrate the sizes of data types #include <iostream> #include <limits.h> using namespace std; int main() { cout << "Size of char : " << sizeof ( char ) << " byte" << endl; cout << "char minimum value: " << CHAR_MIN << endl; cout << "char maximum value: " << CHAR_MAX << endl; cout << "Size of int : " << sizeof ( int ) << " bytes" << endl; cout << "Size of short int : " << sizeof ( short int ) << " bytes" << endl; cout << "Size of long int : " << sizeof ( long int ) << " bytes" << endl; cout << "Size of signed long int : " << sizeof ( signed long int ) << " bytes" << endl; cout << "Size of unsigned long int : " << sizeof (unsigned long int ) << " bytes" << endl; cout << "Size of float : " << sizeof ( float ) << " bytes" << endl; cout << "Size of double : " << sizeof ( double ) << " bytes" << endl; cout << "Size of wchar_t : " << sizeof ( wchar_t ) << " bytes" << endl; return 0; } |
Size of char : 1 byte char minimum value: -128 char maximum value: 127 Size of int : 4 bytes Size of short int : 2 bytes Size of long int : 8 bytes Size of signed long int : 8 bytes Size of unsigned long int : 8 bytes Size of float : 4 bytes Size of double : 8 bytes Size of wchar_t : 4 bytes
Time Complexity: O(1)
Space Complexity: O(1)
C++
#include <iostream> #include <string> using namespace std; int main() { // Integer data types int a = 10; short b = 20; long c = 30; long long d = 40; cout << "Integer data types: " << endl; cout << "int: " << a << endl; cout << "short: " << b << endl; cout << "long: " << c << endl; cout << "long long: " << d << endl; // Floating-point data types float e = 3.14f; double f = 3.141592; long double g = 3.14159265358979L; cout << "Floating-point data types: " << endl; cout << "float: " << e << endl; cout << "double: " << f << endl; cout << "long double: " << g << endl; // Character data types char h = 'a' ; wchar_t i = L 'b' ; char16_t j = u 'c' ; char32_t k = U 'd' ; cout << "Character data types: " << endl; cout << "char: " << h << endl; wcout << "wchar_t: " << i << endl; cout << "char16_t: " << j << endl; cout << "char32_t: " << k << endl; // Boolean data type bool l = true ; bool m = false ; cout << "Boolean data type: " << endl; cout << "true: " << l << endl; cout << "false: " << m << endl; // String data type string n = "Hello, world!" ; cout << "String data type: " << endl; cout << n << endl; return 0; } |
Integer data types: int: 10 short: 20 long: 30 long long: 40 Floating-point data types: float: 3.14 double: 3.14159 long double: 3.14159 Character data types: char: a wchar_t: b char16_t: 99 char32_t: 100 Boolean data type: true: 1 false: 0 String data type: Hello, world!
This program declares variables of various data types, assigns values to them, and then prints out their values.
The integer data types include int, short, long, and long long. These data types represent whole numbers of varying sizes.
The floating-point data types include float, double, and long double. These data types represent real numbers with varying levels of precision.
The character data types include char, wchar_t, char16_t, and char32_t. These data types represent individual characters of varying sizes.
The boolean data type is a simple data type that can only have one of two values: true or false.
The string data type is a sequence of characters. In this program, we use the string class to declare a string variable and assign it a value.
Advantages:
Data types provide a way to categorize and organize data in a program, making it easier to understand and manage.
Each data type has a specific range of values it can hold, allowing for more precise control over the type of data being stored.
Data types help prevent errors and bugs in a program by enforcing strict rules about how data can be used and manipulated.
C++ provides a wide range of data types, allowing developers to choose the best type for a specific task.
Disadvantages:
Using the wrong data type can result in unexpected behavior and errors in a program.
Some data types, such as long doubles or char arrays, can take up a large amount of memory and impact performance if used excessively.
C++’s complex type system can make it difficult for beginners to learn and use the language effectively.
The use of data types can add additional complexity and verbosity to a program, making it harder to read and understand.
C++ Data Types
All variables use data type during declaration to restrict the type of data to be stored. Therefore, we can say that data types are used to tell the variables the type of data they can store. Whenever a variable is defined in C++, the compiler allocates some memory for that variable based on the data type with which it is declared. Every data type requires a different amount of memory.
C++ supports a wide variety of data types and the programmer can select the data type appropriate to the needs of the application. Data types specify the size and types of values to be stored. However, storage representation and machine instructions to manipulate each data type differ from machine to machine, although C++ instructions are identical on all machines.
C++ supports the following data types:
- Primary or Built-in or Fundamental data type
- Derived data types
- User-defined data types