Built-in Types in C++
Built-in types in C++ refer to the basic data types the language provides. These types serve as the essential building blocks for declaring variables, manipulating data, and performing various operations in a C++ application. Built-in types are also occasionally referred to as "primitive types".
In this tutorial, we'll look at the fundamental built-in data types in C++. These types act as the fundamental data storage and manipulation units in C++ programs.
Example:
#include <iostream>
using namespace std;
int main() {
// Declare and initialize an integer variable
int policenum = 100;
// Declare and initialize a double variable
double var = 8.34670;
// Declare and initialize a character variable
char hello = 'S';
// Declare and initialize a boolean variable
bool tutorial = true;
// Output the value of the integer variable
cout << "Police Number: " << policenum << endl;
// Output the value of the double variable
cout << "Value: " << var << endl;
// Output the value of the character variable
cout << "Hello: " << hello << endl;
// Output the value of the boolean variable
cout << "Is tutorial: " << (tutorial ? "Yes": "No") << endl;
return 0; // Indicate successful completion of the program
}
Output:
![Built-in Types in C++](https://static.tutorialandexample.com/cpp/built-in-types-in-cpp1.png)
List of Built-in Types in C++
- Integer Types
- Floating-Point TypesCharacter Types
- Boolean Type
- Void Type
- Enumeration Types
- Pointer Types
- Array Types
- Reference Types
- User-Defined Types
- Derived Types
Integer Types
- Int: A regular integer type that can store positive and negative values in a system-specific amount of memory.
Syntax:
int variablename;
- Short: a lower-range, memory-efficient integer type that works well when space is an issue.
Syntax:
short variableName;
- Long: An integer type with a greater value range than int is good for working with bigger numbers.
Syntax:
long variableName;
- Long long: A type that takes up 8 bytes of memory and was created to support extremely big integers and exact calculations.
Syntax:
long long variableName;
- Unsigned int: The range of positive numbers is essentially doubled by the unsigned int integer type, which is reserved for non-negative values.
Syntax:
Unsigned int variableName;
- Unsigned short: An unsigned integer type with a constrained positive range that is memory efficient.
Syntax:
Unsigned short variableName;
- Unsigned long: An unsigned integer type that, in contrast to unsigned int, has a wider positive range, is known as unsigned long.
Syntax:
Unsigned long variableName;
- Unsigned long long: An unsigned type appropriate for handling extremely big non-negative numbers and complex calculations.
Syntax:
Unsigned long, long variableName;
Example for integer types:
#include <iostream>
using namespace std;
int main() {
int age = 42;
short ambulancenum = 108;
long num1 = 1234567890L;
long long sample = 987654321012345LL;
unsigned int var1 = 500;
unsigned short var2 = 300;
unsigned long samplelong = 1234567890UL;
unsigned long, long example= 987654321012345ULL;
cout << "The given int value : " << age << endl;
cout << "The given short value: " << ambulancenum << endl;
cout << "The given long value: " << num1 << endl;
cout << "The given long long value: " << sample << endl;
cout << "The given unsigned int value: " << var1 << endl;
cout << "The given unsigned short value: " << var2 << endl;
cout << "The given unsigned long value: " << samplelong << endl;
cout << "The given unsigned long long value: " << example << endl;
return 0;
}
Output:
![Built-in Types in C++](https://static.tutorialandexample.com/cpp/built-in-types-in-cpp2.png)
Floating-Point Types
- Float: Single-precision floating-point numbers are represented by the float data type. It offers a balance between precision and range. Most applications that call for decimal numbers can use the float type.
Syntax:
float variableName;
- Double: Double-precision floating-point numbers are represented by the double data type. Compared to float, it offers greater range and more precision. It is frequently utilized when greater accuracy is required, such as in financial applications or scientific computations.
Syntax:
double variableName;
- Long double: Extended-precision floating-point numbers are represented by the long double data type. Even more, precision is provided than the double type, but different systems and compilers may have varying availability and behavior.
Syntax:
Long double variableName;
Example for floating point data types:
#include <iostream> // Include input/output library
using namespace std; // Use standard namespace
int main() { // Main function
// Declare and initialize floating-point variables
float samplefloat = 8.657f; // A floating-point number (single precision)
double doublevar = 6.7890654915697; // A double-precision floating-point number
long double longdoublevar = 4.12873094528956427456; // A long double-precision floating-point number
// Output values
cout << "Float value: " << samplefloat << endl; // Output the float value
cout << "Double value: " << doublevar << endl; // Output the double value
cout << "Long Double value: " << longdoublevar << endl; // Output the long double value
return 0;
}
Output:
![Built-in Types in C++](https://static.tutorialandexample.com/cpp/built-in-types-in-cpp3.png)
Character Types
- Char: A single character is represented by the char data type. It can store any character from the character set, including a letter, digit, or symbol. Small integer numbers and individual characters are frequently stored in it.
Syntax:
char variableName;
- Signed char: Like a char representing a character, it further specifies that it can carry values between -128 and 127.
Syntax:
Signed char variableName;
- Unsigned char: Unsigned char may only store positive values and represent characters in the same way as char. It has a range of 0 to 255.
Syntax:
Unsigned char variableName;
Example:
#include <iostream>
using namespace std;
int main() {
char var1 = 'S';
signed char samplesignedchar = -245;
unsigned char sampleunsignedchar = 789;
cout << "The given Char value is: " << var1 << " (ASCII Value: " << static_cast<int>(var1) << ")" << endl;
cout << "The given Signed Char value is : " << samplesignedchar << " (Value: " << static_cast<int>(samplesignedchar) << ")" << endl;
cout << "The given Unsigned Char value is : " << sampleunsignedchar << " (Value: " << static_cast<int>(sampleunsignedchar) << ")" << endl;
return 0;
}
Output:
![Built-in Types in C++](https://static.tutorialandexample.com/cpp/built-in-types-in-cpp4.png)
Boolean Types:
A basic built-in type in C++ called bool represents boolean values, which can be true or false. Programmers frequently employ boolean values to direct logic flow and create condition-based choices.
- Bool: Boolean values, which can be true or false, are represented by the bool data type. It is frequently employed for decision-making in control structures such as if, while, and for loops. A boolean typically takes up one byte in memory but is typically treated as having a value of 1 for true and 0 for false.
Example:
#include <iostream>
using namespace std;
int main() {
// Declare boolean variables
bool res; // declare a boolean type variable res
bool ans = false; // declare boolean type variable ans and initialize it as false
// Assign boolean values
res = true;
ans = true;
// Use boolean values in conditional statements
if (res) {
cout << "The given type is true" << endl;
} else {
cout << "The declared type is false" << endl;
}
// Logical NOT operator
if (!ans) {
cout << "This will not be printed" << endl;
} else {
cout << "This will be printed" << endl;
}
return 0;
}
Output:
![Built-in Types in C++](https://static.tutorialandexample.com/cpp/built-in-types-in-cpp5.png)
Void Type
A basic idea in C++ and other computer languages is the void type. It stands for the absence of a value, indicating that neither a function nor a pointer points to any particular kind of data. The void type is very helpful in function declarations, pointers, and some specialized situations where a return value is not required.
- Void Type: In the C++ programming language, void is a particular data type that denotes an empty or null value. It typically occurs in the following circumstances:
- Function return type
- Pointer type
Example:
#include <iostream>
using namespace std;
//Function with void return type
void action() {
cout << "Welcome to tutorials and examples" << endl;
}
int main() {
void* pointer; // Declare a void pointer
action(); // Call the void Function to display a message
int samplevar = 567; // Declare and initialize an integer variable
pointer = &samplevar; // Assign memory address to the void pointer
cout << "Value at address: " << *static_cast<int*>(pointer) << endl; // Output value using void pointer
return 0;
}
Output:
![Built-in Types in C++](https://static.tutorialandexample.com/cpp/built-in-types-in-cpp6.png)
Enumeration Types
Enumeration (enum): Enums allow you to create new data types that can accept a predetermined range of values. The compiler automatically assigns incrementing integer values starting at zero to each constant and associates each value in the enum with an integer. Enums allow you to use meaningful names rather than constant numbers, which can help your code be more expressive.
Example:
#include <iostream>
using namespace std;
enum sample {
PINK,
YELLOW,
PURPLE
};
int main() {
// Declare a variable of the 'sample' enum
sample select = PINK;
// Use the enum values in conditional statements
if (select == PINK) {
cout << "The selected color is pink." << endl;
} else if (select == YELLOW) {
cout << "The selected color is yellow." << endl;
} else {
cout << "The selected color is purple." << endl;
}
return 0;
}
Output:
![Built-in Types in C++](https://static.tutorialandexample.com/cpp/built-in-types-in-cpp7.png)
Pointer Types
A pointer is a type of data that stores the location in memory of another type of data. It enables you to manipulate and indirectly access the data in that memory location.
syntax:
Type* pointerName;
Array Types
An array is a group of identically typed elements kept near one another in memory. Multiple values can be kept in an array under a single variable name.
Syntax:
Type arrayName[size];
Reference Types
A reference is an alternate name or alias for an existing variable. It allows you to access the same memory address using a different identifier.
Syntax:
Type& referenceName = existingVariable;
User-defined Types
Customized data types constructed using struct or class keywords are user-defined types. They allow you to combine various data members into a single entity.
Syntax:
struct TypeName {
// Data members and functions
};
Similar syntax is used when defining a user-defined type with a class.
Derived Types
By including qualifiers in fundamental data types, derived types are produced. These qualifiers alter the behavior or qualities of the original data type.
- Const: Denotes that the data is unchangeable and constant.
- Volatile: Denotes that the data may change without warning (for instance, because of hardware or outside influences).
- Mutable: Only applies to class members and permits changing a member of a const object.
Example:
#include <iostream>
using namespace std;
int main() {
int var1 = 672; // Declare and initialize an integer variable
int* pointer = &var1; // Declare a pointer to an int and assign the address of 'var1.'
int samplearray[7] = {70, 137, 123, 690, 1, 134, 500}; // Declare and initialize an array of integers
int& refer = var1; // Declare a reference to an int and associate it with 'var1'
struct sample { // Declare a user-defined struct type named 'sample'
string hello;
int num;
};
const double r = 6.1590345; // Declare and initialize a constant double
volatile int tutor = 0; // Declare and initialize a volatile integer
// Output the values using appropriate descriptions
cout << "The given Pointer value is: " << *pointer << endl;
cout << "The sample array 3rd element is: " << samplearray[2] << endl; // Arrays are 0-indexed
cout << "The given reference value is: " << refer << endl;
cout << "The given User-Defined Type is: sample" << endl;
cout << "The given Constant: " << r << endl;
cout << "The given Volatile: " << tutor << endl;
return 0;
}
Output:
![Built-in Types in C++](https://static.tutorialandexample.com/cpp/built-in-types-in-cpp8.png)
Advantages of built-in Types in C++
- Efficiency: Built-in types have direct hardware support and are heavily optimized by compilers, which results in efficient memory use and execution speed. They are made to operate in perfect harmony with the underlying hardware.
- Compatibility: For communicating with libraries, APIs, and system calls, anticipating, and returning common data types, built-in types are crucial.
- Standardization: To ensure uniform behavior across various implementations, C++ defines a set of rules and behaviors for built-in types.
- Compiler optimization: Compilers are well-optimized to handle built-in types well, allowing them to carry out numerous optimizations and produce the best machine code.
- Utilization: Built-in types don't require further setup or configuration because they are a fundamental language component. They are available for use in your programs right away.
- Simple Syntax: Declaring and working with variables of these types is simple when using built-in types, necessitating minimal syntax. Across various C++ compilers and platforms, it is possible to build and run your code across many platforms without having to make significant changes.
The fundamental components of C++'s built-in types are what make efficient and standardized data representation possible. Thanks to their optimized memory utilization, simplified syntax, and platform compatibility, they are the foundation of dependable and high-performance programming. These types enable various applications, from system-level functions to performance-critical jobs, by supporting effective memory management and portability.
They represent C++'s dedication to effective computation and interoperability as fundamental building blocks, making them crucial for developing reliable algorithms, data structures, and abstractions. Built-in types, which offer a dependable, effective, and adaptable framework for data manipulation and program creation, effectively summarize C++'s strengths.