このページには最新のブラウザ(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;.
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.
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.

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. - For a complete program that reads input, Run first asks for one value per line. Check answer supplies the test inputs for you.
English
#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.
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. - For a complete program, Run compiles and runs it. For a function task, Check answer compiles your code with the supplied tests and shows the results.
探索
Variables hold a value
A variable is a named box of a fixed type; assigning replaces it.
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
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.
実行ボタンをクリックして出力を確認してください。
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
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 ~ではない write a main.
実行ボタンをクリックして出力を確認してください。
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
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 ~ではない write a main.
実行ボタンをクリックして出力を確認してください。