D Programming language Cheatsheet

1020




Sample program

import std.stdio;

void main()
{
		writeln("Hello, World!");
}
  • import : import is used to load system libraries into your program
  • std.stdio : std.stdio is a library module which handles data input and output.
  • main() : starting of the program which determines the order of execution
  • writeln : to display data to console
  • /* Multi line comments */
  • // : Single line comment

Data types

Data typeDescriptionRangeSize
intused to store whole numbers-2,147,483,648 to 2,147,483,6474 bytes
shortused to store whole numbers-32,768 to 32,7672 bytes
longused to store whole numbers-9,223,372,036,854,775,808 to 9,223,372,036,854,775,8078 bytes
byteused to store whole numbers-128 to 1271 byte
floatused to store fractional numbers1.17549e-38 to 3.40282e+384 bytes
doubleused to store fractional numbers2.22507e-308 to 1.79769e+3088 bytes
ifloatused to store imaginary value type of float1.17549e-38i to 3.40282e+38i4 bytes
idoubleused to store imaginary value type of double2.22507e-308i to 1.79769e+308i8 bytes
cfloatused to store complex number type made of two floats1.17549e-38+1.17549e-38i to 3.40282e+38+3.40282e+38i8 bytes
cdoubleused to store complex number type made of two doubles2.22507e-308+2.22507e-308i to 1.79769e+308+1.79769e+308i16 bytes
boolcan either store true or falseeither true or false1 byte
charused to store a single characterone character1 byte

Variables

Syntax

datatype variable-names;

Strings

char[15]  msg1 = "Hello World!"; 

/* or */
string msg2 = "Happy learning";

string msg3 = msg1~" "~msg2; //string concatenation

int length = msg3.length // gives length of the string

Operators

Operator typeDescription
Arithmetic Operator+ , - , * , / , %, ++, --
comparision Operator< , > , <= , >=, != , ==
Bitwise Operator& , ^ , |, ~, <<,>>
Logical Operator&& , ||, !
Assignment Operator= , += , -= , *= , /= , %=, <<=, >>=, &=, ^=, |=
Misc Operatorsizeof(), &, *, ? :

Conditional Statements

1. If

if(conditional-expression)
{
    //code
}

2. If-else

if(conditional-expression)
{
    //code
} else {
    //code
}

3. If-else-if ladder

if(conditional-expression-1)
{
    //code
} else if(conditional-expression-2) {
    //code
} else if(conditional-expression-3) {
    //code
}
....
else {
    //code
}

4. Nested-If

if(conditional-expression-1) {    
     //code    
          if(conditional-expression-2) {  
             //code
             if(conditional-expression-3) {
                 //code
             }  
    }    
}

5. Switch

switch(conditional-expression){    
case value1:    
 //code    
 break;  //optional  
case value2:    
 //code    
 break;  //optional  
...    
    
default:     
 //code to be executed when all the above cases are not matched;    
} 

Loops

1. For

for(Initialization; Condition; Increment/decrement){  
//code  
} 

2. While

while(condition){  
//code 
}  

3. Do-While

do{  
//code 
}while(condition); 

Arrays

data-type array-name [ array-size ];

Functions

//defining a function
return_type function_name(parameters) {  
  //code
}

//calling a function
function_name (parameters)

Pointers

Pointer is a variable which holds the memory information(address) of another variable of same data type.

datatype *pointername;

Example

int x = 10, *ptr;

/*ptr = x; // Error because ptr is adress and x is value
*ptr = &x;  // Error because x is adress and ptr is value */

ptr = &x; // valid because &x and ptr are addresses
*ptr = x; // valid because both x and *ptr values 

Structures

Structure is a user-defined data type where it allows you to combine data of different data types.

struct structure_name {

   member definition;
   member definition;
   ...
   member definition;
} [one or more structure variables]; 

struct structure_name variable name; //declaring structure variables

Enum

Enumeration data type is a user-defined data type and enum keyword is used to declare a new enumeration types.

enum name{constant1, constant2, constant3, ....... };

Example

enum month{January, February, March, April, May, June, July, August, September, October, November, December} name;