Show 例子 6
优质
小牛编辑
130浏览
2023-12-01
还有其他一些重要的运算符,包括sizeof和? : ? : D语言支持。
操作者 | 描述 | 例 |
---|---|---|
sizeof() | 返回变量的大小。 | sizeof(a),其中a是整数,返回4。 |
& | 返回变量的地址。 | &一个; 给出变量的实际地址。 |
* | Pointer to a variable. | *一个; 给出一个变量的指针。 |
? : | 条件表达式 | 如果condition为true则为值X:否则为Y. |
例子 (Example)
请尝试以下示例来了解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\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;
}
编译并执行上述程序时,会产生以下结果 -
value of a is 4
*ptr is 4.
Value of b is 30
Value of b is 20