D語(yǔ)言的支持還有其他一些重要的運(yùn)算符,包括的sizeof和? :。
| 運(yùn)算符 | 描述 | 示例 |
|---|---|---|
| sizeof | 返回一個(gè)變量的大小。 | a.sizeof, 其中a是整數(shù),將返回4。 |
| & | 返回一個(gè)變量的地址。 | &a; 將得到的變量actaul地址。 |
| * | 指向變量的指針。 | *a; 指針變量。 |
| ? : | 條件表達(dá)式 | If Condition is true ? Then value X : Otherwise value Y |
試試下面的例子就明白了所有的D編程語(yǔ)言雜運(yùn)算符:
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; }
當(dāng)編譯并執(zhí)行上面的程序它會(huì)產(chǎn)生以下結(jié)果:
value of a is 4 *ptr is 4. Value of b is 30 Value of b is 20