Variables, types, and arithmetic
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · このページには最新のブラウザ(SharedArrayBuffer対応)が必要です。Chrome、Edge、Firefox、Safariを最新バージョンに更新してください。
English
Every value has a type
- In C, every variable has a type that says what kind of value it holds.
- The three you use most:
int(a whole number),double(a number with a decimal point), andchar(a single character). - You must give the type when you create the variable:
int n = 5;ordouble pi = 3.14;.
日本語
全ての値には型がある
- C では、すべての変数にはその値の种类を示す型があります。
- 最もよく使う3つ:
int(整数)、double(小数点を含む数)、char(単一文字)。 - 変数を作成する際に型を指定する必要があります:
int n = 5;またはdouble pi = 3.14;。
English
Doing arithmetic
- C does math with
+,-,*, and/, just like a calculator. *and/happen before+and-. Use brackets( )to change the order.- The
%operator gives the remainder of a division:17 % 5is2.
日本語
計算を行う
- C は電卓と同じように
+、-、*、/で数学計算を行います。 *と/は+と-よりも先に実行されます。順序を変更するには括弧( )を使用します。%演算子は除算の余りを返します:17 % 5は2です。
English
Integers divide differently
- When you divide two
ints, C throws away the fraction:7 / 2is3, not3.5. - To keep the fraction, at least one side must be a
double.7 / 2.0is3.5. - You can cast an
intto adoublewith(double):(double)sum / count.
日本語
整数の割り算は異なる
- 2つの
intを割ると、C は小数点を切り捨てます:7 / 2は3であり、3.5ではありません。 - 小数点部分を維持するには、片方が
doubleである必要があります。7 / 2.0は3.5です。 intをdoubleに型変換できます:(double):(double)sum / count。
English
Reading input
scanfreads typed input into a variable.scanf("%d", &n);reads oneintinton.- Note the
&beforen:scanfneeds the address of the variable so it can fill it in. - In this playground, Run first asks you for the input, one value per line; Check answer feeds the example input for you.
日本語
入力の読み込み
scanfはキーボード入力を変数に読み込みます。scanf("%d", &n);は1つのintをnに読み込みます。&がnの前にある点にご注意ください:scanfは変数を埋め込むために、そのアドレスが必要です。- このプレイグラウンドでは、実行 が initially 入力を要求し(各行1値)、答えを確認 が例示の入力を自動で提供します。
#include <stdio.h>
int main(void) {
int a = 7, b = 2;
printf("%d\n", a / b); // 3 (integer division)
printf("%.1f\n", a / 2.0); // 3.5 (one side is a double)
printf("%d\n", a % b); // 1 (remainder)
return 0;
}
English
Common mistakes
- Declare a variable with its type:
int n;. - Integer division truncates:
7 / 2is3, not3.5.
日本語
よくあるミス
- 型付きで変数を宣言します:
int n;。 - 整数除算は切り捨てられます:
7 / 2は3であり、3.5ではありません。
English
Now you try
- Use
/and%for whole-number math, and adouble(or a cast) when you need a fraction. - For the function tasks, do not write a
main— the checker provides one. - Press Run to compile and run, then Check answer.
日本語
あなたも試してみよう
- 整数計算には
/と%を使い、小数点が必要な場合はdouble(または型変換)を使用します。 - 関数タスクについては、
mainを書かないでください;チェックツールが用意します。 - 実行 を押してコンパイル・実行し、その後 回答を確認 を押してください。
Explore · 探索
Variables hold a value
A variable is a named box of a fixed type; assigning replaces it.
Read one integer n with scanf. A box holds 6 items. Print two lines exactly: Full boxes: then the value of n / 6, and Left over: then the value of n % 6.
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。
Complete double average3(int a, int b, int c) so it returns the average of the three as a double. Watch out for integer division — divide by 3.0, not 3. Do not write a main.
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。
Write double hyp(double a, double b) that returns the hypotenuse length: the square root of a*a + b*b. Use sqrt from <math.h>. Do not write a main.
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。