Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What are type specifiers in C++?
In a statically typed language such as C++, type specifiers are keywords that are used to define the type of data that given variables will hold. There are two types of type specifiers: built-in and user-defined type specifiers.
Built-in Type Specifiers
The built-in type specifiers are the basic and predefined data types provided by C++, such as int, float, char, signed, unsigned, short, long, etc.
int myNumber = 42;
In this given statement, the "int" is a type specifier, which states that the variable "myNumber" can only store integer values or numeric data types.
There exist a lot of built-in type specifiers in C++ like int, float, double, char, string, bool, void, etc.
Example
Here is the following example of various built-in Type specifiers in C++.
#include <iostream>
using namespace std;
int main() {
int myNum = 10; // Integer
float b = 3.14f; // Float
char myChar = 'Z'; // Character
bool isDone = true; // Boolean
string myName = "Aman"; // String
cout << "Integer Value: " << myNum << endl;
cout << "Float Value: " << b << endl;
cout << "Character Value: " << myChar << endl;
cout << "Boolean Value: " << isDone << endl;
cout << "String Value: " << myName << endl;
return 0;
}
Output
The output of the above code is:
Integer Value: 10 Float Value: 3.14 Character Value: Z Boolean Value: 1 String Value: Aman
User-defined Type specifier
The user-defined type specifiers are used for custom data types, which are created by combining existing built-in data types, such as struct, class, union, and enum.
Example
Here is the following example code struct user-defined type specifier in C++.
#include <iostream>
using namespace std;
// define a struct named 'sports'
struct mySport {
string name;
int players;
};
int main() {
mySport s1;
s1.name = "Cricket"; // assigned name
s1.players = 11; // assigned number of players
cout << "Name of Sport: " << s1.name << endl;
cout << "Number of players: " << s1.players << endl;
return 0;
}
Output
The output of the above code is:
Name of Sport: Cricket Number of players: 11
Dynamically Typed Language
A dynamically typed language is a programming language where you don't need to declare the type of a variable. Here, it automatically determined the data type of a variable at runtime.
For example, in Ruby or JavaScript, you can simply declare the variable without explicitly specifying the data type of the variable.
Example
var number = 42; // Integer var name = "Alice"; // String var isLoggedIn = true; // Boolean var items = [1, 2, 3, 4]; // Array