- D Programming - Home
- D Programming - Overview
- D Programming - Environment
- D Programming - Basic Syntax
- D Programming - Variables
- D Programming - Data Types
- D Programming - Enums
- D Programming - Literals
- D Programming - Operators
- D Programming - Loops
- D Programming - Decisions
- D Programming - Functions
- D Programming - Characters
- D Programming - Strings
- D Programming - Arrays
- D Programming - Associative Arrays
- D Programming - Pointers
- D Programming - Tuples
- D Programming - Structs
- D Programming - Unions
- D Programming - Ranges
- D Programming - Aliases
- D Programming - Mixins
- D Programming - Modules
- D Programming - Templates
- D Programming - Immutables
- D Programming - File I/O
- D Programming - Concurrency
- D Programming - Exception Handling
- D Programming - Contract
- D - Conditional Compilation
- D Programming - Classes & Objects
- D Programming - Inheritance
- D Programming - Overloading
- D Programming - Encapsulation
- D Programming - Interfaces
- D Programming - Abstract Classes
- D Programming - Useful Resources
- D Programming - Quick Guide
- D Programming - Useful Resources
- D Programming - Discussion
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