D Programming - sizeof operator



There are few other important operators including sizeof and ? : supported by D Language.

Operator Description Example
sizeof() Returns the size of an variable. sizeof(a), where a is integer, returns 4.
& Returns the address of a variable. &a; gives actual address of the variable.
* Pointer to a variable. *a; gives pointer to a variable.
? : Conditional Expression If condition is true then value X: Otherwise value Y.

Example

Try following example to understand all the miscellaneous operators available in D programming language −

import std.stdio;

int main(string[] args) { 
   int a = 4; 
   short b; 
   double c; 
   int* ptr;

   /* example of sizeof operator */ 
   writefln("Line 1 - Size of variable a = %d\n", a.sizeof ); 
   writefln("Line 2 - Size of variable b = %d\n", b.sizeof ); 
   writefln("Line 3 - Size of variable c= %d\n", c.sizeof );  
  
   /* example of & and * operators */ 
   ptr = &a; /* 'ptr' now contains the address of 'a'*/ 
   writefln("value of a is  %d\n", a); 
   writefln("*ptr is %d.\n", *ptr);  
   
   /* example of ternary operator */ 
   a = 10; 
   b = (a == 1) ? 20: 30; 
   writefln( "Value of b is %d\n", b ); 
   
   b = (a == 10) ? 20: 30; 
   writefln( "Value of b is %d\n", b ); 
   return 0; 
} 

When you compile and execute the above program it produces the following result −

value of a is  4 

*ptr is 4. 

Value of b is 30 

Value of b is 20
d_programming_operators.htm
Advertisements