第13話 演算子
and01.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
*/
#include <stdio.h>
int main( void )
{
int i;
int *pi;
i = 1;
pi = &i; /* この&は単項演算子であるアドレス演算子 */
pi = &*pi; /* この&は単項演算子であるアドレス演算子 */
i = i & *pi; /* この&は二項演算子であるビット毎のAND演算子 */
i &= 0xFF; /* この&は二項演算子であるビット毎のAND代入演算子*/
printf("i=%d\n", i);
if(&i && &*pi) /* この&&は二項演算子であるAND演算子 */
printf("&ばかりで訳がわからない\n");
return 0 ;
}
or01.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
*/
#include <stdio.h>
int main( void )
{
int i;
int j;
int result;
i = 1;
j = 2;
result = i | j; /* この|はビット毎のOR演算子 */
printf("result=%d\n", result);
result |= 8; /* この|=はビット毎のOR代入演算子 */
printf("result=%d\n", result);
result = i || j; /* この||はOR演算子 */
printf("result=%d\n", result);
return 0 ;
}
relational01.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
*/
#include <stdio.h>
int main( void )
{
int i;
int j;
int result;
i = 1;
j = 2;
result = i < j; /* この<はless than演算子 */
printf("result=%d\n", result);
result = i <= j; /* この<=はless than or equal to演算子 */
printf("result=%d\n", result);
result = i << j; /* この<<はビット毎の左シフト演算子 */
printf("result=%d\n", result);
result <<= j; /* この<<=はビット毎の左シフト代入演算子 */
printf("result=%d\n", result);
return 0 ;
}
shift01.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
*/
#include <stdio.h>
int main( void )
{
int shift;
int i;
unsigned int u;
i = 0x80000000;
u = 0x80000000;
for(shift=0;shift<=32;shift++)
{
printf("i=%d (0x%08x), u=%u (0x%08x)\n", i, i, u, u);
i >>= 1;
u >>= 1;
}
i = 0x00000001;
u = 0x00000001;
for(shift=0;shift<=32;shift++)
{
printf("i=%d (0x%08x), u=%u (0x%08x)\n", i, i, u, u);
i <<= 1;
u <<= 1;
}
return 0 ;
}
multiDev01.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
*/
#include <stdio.h>
int main( void )
{
int i;
int j;
int result;
int *pi;
int *pj;
i = 100;
j = 10;
pi = &i;
pj = &j;
result = i / j; /* この/は除算演算子 */
printf("result=%d\n", result);
result = i * j; /* この*は乗法演算子 */
printf("result=%d\n", result);
result /= j; /* この/は除算代入演算子 */
printf("result=%d\n", result);
result *= j; /* この*は乗法代入演算子 */
printf("result=%d\n", result);
result *= *pj; /* この*は乗法代入演算子と間接参照演算子 */
printf("result=%d\n", result);
result = *pi * *pj; /* この*は間接参照演算子と乗法演算子 */
printf("result=%d\n", result);
/*result = i * pj;*/ /* この*はどっち? *//* コンパイルエラー */
return 0 ;
}
multiDev02.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
*/
#include <stdio.h>
int main( void )
{
int *pi;
int result, j =100, i =5;
pi = &i;
result = j/*pi;
printf("result=%d\n", result);
return 0 ;
}
multiDev03.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
*/
#include <stdio.h>
int main( void )
{
int *pi;
int result, j =100, i =5;
pi = &i;
result = j/ *pi;
printf("result=%d\n", result);
return 0 ;
}
sizeof01.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
*/
#include <stdio.h>
int main( void )
{
char c;
char a[10];
short s;
int i;
size_t size0;
size_t size1;
size_t size2;
struct t_st
{
char sc;
short ss;
int si;
} st;
size0 = sizeof c;
size1 = sizeof(char);
printf("size of c=%d, size of char=%d\n", size0, size1);
size0 = sizeof a;
size1 = sizeof a[0];
size2 = sizeof *a;
printf("size of a=%d, size of a[0]=%d, size of *a=%d\n",
size0, size1, size2);
size0 = sizeof s;
size1 = sizeof(short);
printf("size of s=%d, size of short=%d\n", size0, size1);
size0 = sizeof i;
size1 = sizeof(int);
printf("size of c=%d, size of char=%d\n", size0, size1);
size0 = sizeof st;
size1 = sizeof(struct t_st);
printf("size of st=%d, size of struct t_st=%d\n", size0, size1);
i = 100;
size0 = sizeof s + i;
size1 = sizeof(s + i);
printf("size of s + i=%d, size of (s + i)=%d\n", size0, size1);
return 0 ;
}
offsetof01.c
/*
* (c)Copyright Spacesoft corp., 2006 All rights reserved.
*/
#include <stdio.h>
#include <stddef.h>
/*
* Visual C++のstddef.hに定義されているoffsetofマクロ
* #define offsetof(s,m) (size_t)&(((s *)0)->m)
*/
int main( void )
{
size_t offset;
struct t_st
{
char c;
short s;
int i;
};
offset = offsetof(struct t_st, c);
printf("offset of c=%d\n", offset);
offset = offsetof(struct t_st, s);
printf("offset of s=%d\n", offset);
offset = offsetof(struct t_st, i);
printf("offset of i=%d\n", offset);
return 0 ;
}