发布于 2015-08-16 14:50:13 | 65 次阅读 | 评论: 0 | 来源: 网络整理

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 = %dn", a.sizeof );
   writefln("Line 2 - Size of variable b = %dn", b.sizeof );
   writefln("Line 3 - Size of variable c= %dn", c.sizeof );

   /* example of & and * operators */
   ptr = &a;	/* 'ptr' now contains the address of 'a'*/
   writefln("value of a is  %dn", a);
   writefln("*ptr is %d.n", *ptr);

   /* example of ternary operator */
   a = 10;
   b = (a == 1) ? 20: 30;
   writefln( "Value of b is %dn", b );

   b = (a == 10) ? 20: 30;
   writefln( "Value of b is %dn", b );
   return 0;
}

当编译并执行上面的程序它会产生以下结果:


value of a is  4
*ptr is 4.
Value of b is 30
Value of b is 20
最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务