Which type of cpp?
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:
1. Primitive Data Types: These data types are built-in or predefined data types and can be used directly by the user to declare variables. example: int, char, float, bool, etc. Primitive data types available in C++ are:
2. Derived Data Types: Derived data types that are derived from the primitive or built-in datatypes are referred to as Derived Data Types. These can be of four types namely:
3. Abstract or User-Defined Data Types: Abstract or User-Defined data types are defined by the user itself. Like, defining a class in C++ or a structure. C++ provides the following user-defined datatypes:
Example:
The size of variables might be different from those shown in the above table, depending on the compiler and the computer you are using.
Time Complexity: O(1)
Space Complexity: O(1)
As the name suggests, datatype modifiers are used with built-in data types to modify the length of data that a particular data type can hold.
Data type modifiers available in C++ are:
The below table summarizes the modified size and range of built-in datatypes when combined with the type modifiers:
Now to get the range of data types refer to the following chart
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:
Time Complexity: O(1)
Space Complexity: O(1)
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.
This article is contributed by Harsh Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.
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.
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 −
Several of the basic types can be modified using one or more of these type modifiers −
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.
The size of variables might be different from those shown in the above table, depending on the compiler and the computer you are using.
Following is the example, which will produce correct size of various data types on your computer.
This example uses endl, which inserts a new-line character after every line and << operator is being used to pass multiple values out to the screen. We are also using sizeof() operator to get size of various data types.
When the above code is compiled and executed, it produces the following result which can vary from machine to machine −
Following is another example:
You can create a new name for an existing type using typedef. Following is the simple syntax to define a new type using typedef −
For example, the following tells the compiler that feet is another name for int −
Now, the following declaration is perfectly legal and creates an integer variable called distance −
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 −
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".
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.
Here, blue will have a value of 6 because each name will be one greater than the one that precedes it.
Unlike some languages, C++ has no universal base type from which all other types are derived. The language includes many fundamental types, also known as built-in types. These types include numeric types such as int , double , long , bool , plus the char and wchar_t types for ASCII and UNICODE characters, respectively.
Users can use the primitive data types to declare variables, and these are built-in data types in C++, for instance, float, bool, etc. Primitive data types present in C++ are defined below:
The keyword int can represent integer data types. The range of integers is -2147483648 to 2147483647, and they take up 4 bytes of memory.
For example,
data here is an integer data type variable. The variable data requires 2 or 4 bytes of memory space.
The keyword char represent characters. It is 1 byte in size. Single quotes ' ' are used to enclose characters in C++.
For example,
ch here is a character datatype variable. This means that the variable requires 1 byte of memory space.
The boolean data type's keyword is bool. True or false are the two possible values for the boolean data type. Boolean values are generally used in conditional statements and loops.
For example,
is_true here is a boolean data type variable. This means that the variable requires 1 byte of memory space.
float is the keyword used to hold floating-point numbers (decimals and exponentials). The float variable has a size of 4 bytes.
For example,
val here is a floating-point datatype variable. This means that the variable requires 4 bytes of memory space.
double is the keyword used to hold floating-point numbers (decimals and exponentials) with double precision. The double variable has a size of 8 bytes.
For example,
val here is a double floating-point datatype variable. This means that the variable requires 8 bytes of memory space.
The term void refers to something that has no worth. The void data type represents a valueless entity. Variables of the void type cannot be declared. It is only used for functions, not returning any data.
The wide-character wchar_t data type is similar to the char data type, but its size is 2 or 4 bytes rather than 1 byte. It's used to represent characters that take up more memory than a single char to represent.
To get the datatype of variable, use typeid(x).name() of typeinfo library. It returns the type name of the variable as a string.
The syntax to get the type name of a variable x using typeid() is
In the following example, we take a variable x of type double, and print the type name of this variable programmatically using typeid() function.
C++ Program
Output
d stands for double.
Now, let us take a variable of user-defined class type, say Abc, and print the type of this variable programmatically.
C++ Program
Output
More Questions
- how to help myanmar?
- What does iea stand for?
- which aura is better?
- How to pain in hand?
- How to identify microsoft office version?
- What is the cost of metlife dental insurance?
- What is the treatment for high alt sgpt?
- How do restaurants make their food better?
- Which is the best product to lose weight?
- How to start your own affiliate marketing business?