Cprogramming-c-sizeof-operator

提供:Dev Guides
移動先:案内検索

Cのsizeofおよび三項演算子

上記の演算子に加えて、 sizeof および*?を含む他の重要な演算子はほとんどありません。 :* C言語でサポートされています。

Operator Description Example
sizeof() Returns the size of a variable. sizeof(a), where a is integer, will return 4.
& Returns the address of a variable. &a; returns the actual address of the variable.
* Pointer to a variable. *a;
? : Conditional Expression. If Condition is true ? then value X : otherwise value Y

次の例を試して、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