D語言sizeof運算符

D語言的支持還有其他一些重要的運算符,包括的sizeof和? :。

運算符

描述

示例

sizeof

返回一個變量的大小。

a.sizeof, 其中a是整數,將返回4。

&

返回一個變量的地址。

&a; 將得到的變量actaul地址。

*

指向變量的指針。

*a; 指針變量。

? :

條件表達式

If Condition is true ? Then value X : Otherwise value Y

示例

試試下面的例子就明白了所有的D編程語言雜運算符:

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

當編譯並執行上面的程序它會產生以下結果:

value of a is 4
*ptr is 4.
Value of b is 30
Value of b is 20