Cの変数の取り扱いの概要と基本的な型
Cは静的に型付けされた言語です。
これは、変数にはタイプが関連付けられており、このタイプはコンパイル時に認識されることを意味します。
これは、Python、JavaScript、PHP、およびその他のインタープリター言語で変数を操作する方法とは大きく異なります。
Cで変数を作成するときは、宣言で変数の型を指定する必要があります。
この例では、変数を初期化しますage
タイプ付きint
:
int age;
変数名には、大文字または小文字を含めることができ、数字と下線文字を含めることができますが、数字で始めることはできません。AGE
そしてAge10
有効な変数名です。1age
ではありません。
初期値を指定して、宣言時に変数を初期化することもできます。
int age = 37;
変数を宣言すると、プログラムコードでその変数を使用できるようになり、変数を使用していつでもその値を変更できます。=
たとえば、演算子age = 100;
、新しい値が同じタイプである場合。
この場合:
#include <stdio.h>
int main(void) {
int age = 0;
age = 37.2;
printf("%u", age);
}
コンパイラはコンパイル時に警告を発し、10進数を整数値に変換します。
ザ・C組み込みのデータ型はint
、char
、short
、long
、float
、double
、long double
。それらについてもっと知りましょう。
整数
Cは、整数値を定義するために次のタイプを提供します。
char
int
short
long
ほとんどの場合、int
整数を格納します。ただし、場合によっては、他の3つのオプションのいずれかを選択することをお勧めします。
ザ・char
typeは、ASCIIチャートの文字を格納するために一般的に使用されますが、からの小さな整数を保持するために使用できます。-128
に127
。少なくとも1バイトかかります。
int
少なくとも2バイトかかります。short
少なくとも2バイトかかります。long
少なくとも4バイトかかります。
ご覧のとおり、異なる環境で同じ値が保証されるわけではありません。表示のみです。問題は、各データ型に格納できる正確な数が実装とアーキテクチャに依存することです。
私たちはそれを保証しますshort
より長くないint
。そして、私たちは保証されていますlong
より短くないint
。
ANSI C仕様標準は、各タイプの最小値を決定します。そのおかげで、少なくとも、自由に使用できると期待できる最小値を知ることができます。
ArduinoでCをプログラミングしている場合、ボードが異なれば制限も異なります。
Arduino Unoボードでは、int
次の範囲の2バイトの値を格納します-32,768
に32,767
. On a Arduino MKR 1010, int
次の範囲の4バイトの値を格納します-2,147,483,648
に2,147,483,647
。かなり大きな違い。
すべてのArduinoボードで、short
次の範囲の2バイトの値を格納します-32,768
に32,767
。long
から4バイトを格納します-2,147,483,648
に2,147,483,647
。
符号なし整数
上記のすべてのデータ型について、先頭に追加できますunsigned
負の数ではなく、0から範囲を開始します。多くの場合、これは理にかなっています。
unsigned char
からの範囲になります0
少なくとも255
unsigned int
からの範囲になります0
少なくとも65,535
unsigned short
からの範囲になります0
少なくとも65,535
unsigned long
からの範囲になります0
少なくとも4,294,967,295
オーバーフローの問題
これらすべての制限を考えると、疑問が生じる可能性があります。数が制限を超えないようにするにはどうすればよいでしょうか。そして、制限を超えるとどうなりますか?
あなたが持っている場合unsigned int
番号を255に設定し、それをインクリメントすると、代わりに256が返されます。予想通り。あなたが持っている場合unsigned char
数値を255に設定し、それをインクリメントすると、代わりに0が返されます。可能な初期値からリセットします。
あなたが持っている場合unsigned char
255の数で、それに10を加えると、その数が得られます。9
:
#include <stdio.h>
int main(void) {
unsigned char j = 255;
j = j + 10;
printf("%u", j); /* 9 */
}
符号付きの値がある場合、動作は定義されていません。それは基本的にあなたにこの場合のように変化することができる膨大な数を与えるでしょう:
#include <stdio.h>
int main(void) {
char j = 127;
j = j + 10;
printf("%u", j); /* 4294967177 */
}
言い換えると、Cは、型の制限を超えることからユーザーを保護しません。あなたはこれを自分で世話する必要があります。
間違ったタイプを宣言するときの警告
変数を宣言して間違った値で初期化すると、gcc
コンパイラ(おそらく使用しているもの)は次のように警告する必要があります。
#include <stdio.h>
int main(void) {
char j = 1000;
}
hello.c:4:11: warning: implicit conversion from 'int' to
'char' changes value from 1000 to -24
[-Wconstant-conversion]
char j = 1000;
~ ^~~~
1 warning generated.And it also warns you in direct assignments:
#include <stdio.h>
int main(void) {
char j;
j = 1000;
}
But not if you increase the number using for example +=
:
#include <stdio.h>
int main(void) {
char j = 0;
j += 1000;
}
Floating point numbers
Floating point types can represent a much larger set of values than integers can, and can also represent fractions, something that integers can’t do.
Using floating point numbers, we represent numbers as decimal numbers times powers of 10.
You might see floating point numbers written as
1.29e-3
-2.3e+5
and in other seemingly weird ways.
The following types:
float
double
long double
are used to represent numbers with decimal points (floating point types). All can represent both positive and negative numbers.
The minimum requirements for any C implementation is that float
can represent a range between 10^-37 and 10^+37, and is typically implemented using 32 bits.
double
can represent a bigger set of numbers.
long double
can hold even more numbers.
The exact figures, as with integer values, depend on the implementation.
On a modern Mac, a float
is represented in 32 bits, and has a precision of 24 significant bits, 8 bits are used to encode the exponent.
A double
number is represented in 64 bits, with a precision of 53 significant bits, 11 bits are used to encode the exponent.
The type long double
is represented in 80 bits, has a precision of 64 significant bits, 15 bits are used to encode the exponent.
On your specific computer, how can you determine the specific size of the types? You can write a program to do that:
#include <stdio.h>
int main(void) {
printf("char size: %lu bytes\n", sizeof(char));
printf("int size: %lu bytes\n", sizeof(int));
printf("short size: %lu bytes\n", sizeof(short));
printf("long size: %lu bytes\n", sizeof(long));
printf("float size: %lu bytes\n", sizeof(float));
printf("double size: %lu bytes\n", sizeof(double));
printf("long double size: %lu bytes\n", sizeof(long double));
}
In my system, a modern Mac, it prints:
char size: 1 bytes
int size: 4 bytes
short size: 2 bytes
long size: 8 bytes
float size: 4 bytes
double size: 8 bytes
long double size: 16 bytes
Download my free C Handbook
More clang tutorials:
- Introduction to the C Programming Language
- C Variables and types
- C Constants
- C Operators
- C Conditionals
- How to work with loops in C
- Introduction to C Arrays
- How to determine the length of an array in C
- Introduction to C Strings
- How to find the length of a string in C
- Introduction to C Pointers
- Looping through an array with C
- Booleans in C
- Introduction to C Functions
- How to use NULL in C
- Basic I/O concepts in C
- Double quotes vs single quotes in C
- How to return a string from a C function
- How to solve the implicitly declaring library function warning in C
- How to check a character value in C
- How to print the percentage character using `printf()` in C
- C conversion specifiers and modifiers
- How to access the command line parameters in C
- Scope of variables in C
- Can you nest functions in C?
- Static variables in C
- C Global Variables
- The typedef keyword in C
- C Enumerated Types
- C Structures
- C Header Files
- The C Preprocessor