Show 例子 6
优质
小牛编辑
143浏览
2023-12-01
除了上面讨论的运算符之外,很少有其他重要的运算符,包括sizeof和? : ? : C语言支持。
操作者 | 描述 | 例 |
---|---|---|
sizeof() | 返回变量的大小。 | sizeof(a),其中a是整数,将返回4。 |
& | 返回变量的地址。 | &一个; 返回变量的实际地址。 |
* | Pointer to a variable. | *a; |
? : | 条件表达式。 | 如果条件为真? 然后值X:否则值Y. |
例子 (Example)
尝试以下示例来了解C中可用的所有其他运算符 -
#include <stdio.h>
main() {
int a = 4;
short b;
double c;
int* ptr;
/* example of sizeof operator */
printf("Line 1 - Size of variable a = %d\n", sizeof(a) );
printf("Line 2 - Size of variable b = %d\n", sizeof(b) );
printf("Line 3 - Size of variable c= %d\n", sizeof(c) );
/* example of & and * operators */
ptr = &a; /* 'ptr' now contains the address of 'a'*/
printf("value of a is %d\n", a);
printf("*ptr is %d.\n", *ptr);
/* example of ternary operator */
a = 10;
b = (a == 1) ? 20: 30;
printf( "Value of b is %d\n", b );
b = (a == 10) ? 20: 30;
printf( "Value of b is %d\n", b );
}
编译并执行上述程序时,会产生以下结果 -
Line 1 - Size of variable a = 4
Line 2 - Size of variable b = 2
Line 3 - Size of variable c= 8
value of a is 4
*ptr is 4.
Value of b is 30
Value of b is 20