D 编程 - sizeof 运算符



D 语言支持一些其他的重要运算符,包括sizeof? :

运算符 描述 示例
sizeof() 返回变量的大小。 sizeof(a),其中 a 为整数,返回 4。
& 返回变量的地址。 &a; 给出变量的实际地址。
* 指向变量的指针。 *a; 给出指向变量的指针。
? : 条件表达式 如果条件为真,则返回值 X;否则返回值 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\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
d_programming_operators.htm
广告

© . All rights reserved.