Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

data type
d
communitycreator

How to check the data type of a variable in D

NDUKWE CHIDERA K.

Overview

Extracting detailed data can be useful when we’re writing conditionals and other circumstances. In this shot, we’ll look at a particular method that allows us to check data types in D.

The typeof() method

The typeof() method checks a variable’s data type provided as a parameter. It returns the data type of that variable.

Syntax

typeof(variable)

Parameters

  • variable : This is the variable whose data type we want to determine.

Return value

The data type of the variable is returned. We can only print the data type to the screen as a string using the stringof method.

import std.stdio;

void main(){
int myint; 
string mystring;
enum Shoes { Men } 
ulong school;
char type;

writeln(typeof(myint).stringof);
writeln(typeof(mystring).stringof);
writeln(typeof(Shoes.Men).stringof);
writeln(typeof(school).stringof);
writeln(typeof(type).stringof);

 }

Explanation

In the code snippet above, the data type of a few variables declared was returned.

Note: The enum statement lets developers create custom data types.

  • Line 1: We import relevant packages.
  • Line 3–8: We declare variables whose types must be checked.
  • Line 10–14: We display the outcome of checking the data type of each variable by using the .stringof property that allows data types to be printed as strings in D.

RELATED TAGS

data type
d
communitycreator
RELATED COURSES

View all Courses

Keep Exploring