[unsigned] char [unsigned] int [unsigned] short [unsigned] long
[例] 整数型のメモリ幅(サイズ)を調べるプログラム
#include <stdio.h> int main(void) { printf("char %d[byte]\n", sizeof(char)); printf("int %d[byte]\n", sizeof(int)); printf("short %d[byte]\n", sizeof(short)); printf("long %d[byte]\n", sizeof(long)); }
char 1[byte] int 4[byte] short 2[byte] long 4[byte]
#include <stdio.h> int main(void) { int i; printf("int\tchar\tunsigned char\n"); for(i=0; i<256; i++) { printf("%d\t%d\t%u\n", i, (char)i, (unsigned char)i); } }
変数iの値を文字型データに変換した値 | |
変数iの値を符号無しの文字型に変換した値 |
[実行結果]
int char unsigned char 0 0 0 1 1 1 2 2 2 3 3 3 4 4 4 ... 124 124 124 125 125 125 126 126 126 127 127 127 128 -128 128 129 -127 129 130 -126 130 131 -125 131 132 -124 132 ... 252 -4 252 253 -3 253 254 -2 254 255 -1 255
0 | 偽(NO, FALSE) |
---|---|
1 | 真(YES, TRUE) |
0以外 | 真(YES, TRUE) |
int main(void) { int i; printf("TRUE: %d\n", 4==4); printf("FALSE: %d\n", 4==5); for(i=-5; i<5; i++) { if(i) printf("%d\tTrue\n", i); else printf("%d\tFalse\n", i); } }
TRUE: 1 FALSE: 0 -5 True -4 True -3 True -2 True -1 True 0 False 1 True 2 True 3 True 4 True
演算子 | 意味 | 例 |
---|---|---|
~ | 反転(not) | 0xf8→ 0x07 |
& | 論理積(and) | 0x0f & 0x11 → 0x01 |
| | 論理和(or) | 0x0f | 0x11 → 0x1f |
^ | 排他的論理和(Ex-or) | 0x0f ^ 0x11 → 0x1e |
<< | 左へビットシフト | 0x05 << 1 → 0x0a |
>> | 右へビットシフト | 0x05 >> 1 → 0x02 |
a | 0010110101101010 |
---|---|
b | 0101101110110100 |
~ a | 1101001010010101 |
a & b | 0000100100100000 |
a | b | 0111111111111110 |
a ^ b | 0111011011011110 |
a << 4 | 1101011010100000 |
a >> 4 | 0000001011010110 |
0x0f ... 15, 0x10 ... 16
整数 / 整数整数型データの割算は余りを切捨て,0に近い整数値に丸める.
整数 % 整数剰余演算子(%)は余りを計算する.
#include <stdio.h> int main(void) { char c; for(c='a'; c<='z'; c++) putchar(c); putchar('\n'); }
関数 | 意味 |
---|---|
getchar() | 標準入力より読み込んだ文字を返す(1文字) |
putchar(c) | 標準出力へ1文字(c)を書き出す |
int main(void) { int n=0; char c; while((c=getchar()) != EOF) { n++; putchar(c); } printf("読んだ文字数=%d\n", n); }
[ヒント]
[実行結果の例]
0 1 1 2 2 4 3 8 ... 30 1073741824 31 2147483648