C basics: main, printf, and functions
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
C is a compiled language
- C is one of the oldest and most important languages. Linux, databases, and even Python are built with C.
- Unlike Python, C is compiled: the computer first turns your code into a program, then runs it.
- C is strict: every statement ends with a semicolon
;, and every variable needs a type.
日本語
Cはコンパイル言語です
- Cは最古かつ最も重要なプログラミング言語の一つです。Linux、データベース、さらにはPythonもCで構築されています。
- Pythonとは異なり、Cはコンパイルされます:コンピュータはまずあなたのコードをプログラムに変換し、その後実行します。
- Cは厳密です:すべての文はセミコロン
;で終わり、すべての変数は型が必要です。
English
The C skeleton
- Every C program starts in a function called main. Your code goes inside main.
#include <stdio.h>gives youprintf, which prints text.return 0;at the end ofmainmeans "the program finished without an error".
日本語
Cの骨子
- すべてのCプログラムはmainと呼ばれる関数から始まります。コードはmainの内部に入れます。
#include <stdio.h>はprintfを提供し、テキストを出力します。return 0;がmainの最後にある場合、「プログラムがエラーなく終了した」ことを意味します。
#include <stdio.h>
int main(void) {
printf("C runs from inside main.\n");
int year = 11;
printf("Grade %d\n", year);
return 0;
}
English
printf and types
printfprints text. The\ninside the text means "start a new line".- To print a value, use a placeholder:
%dfor anint,%ffor adouble,%cfor one character. printf("Grade %d\n", year);puts the value ofyearwhere the%dis.- Every variable needs a type first:
int year = 11;ordouble pi = 3.14;.
日本語
printfと型
printfはテキストを出力します。テキスト内の\nは「改行する」を意味します。- 値を出力するには、プレースホルダーを使用します:
%dはint用、%fはdouble用、%cは1文字用です。 printf("Grade %d\n", year);はyearの値を%dの位置に格納します。- すべての変数にはまず型が必要です:
int year = 11;またはdouble pi = 3.14;。
English
Common mistakes
- Every statement ends with
;, andmainreturnsint(return 0;). printfneeds the right format (%d,%f) and\nfor a new line.
日本語
よくあるミス
- 各文は
;で終わり、mainはint(return 0;) を返します。 printfは正しい形式 (%d,%f) と新しい行のための\nを必要とします。
English
Now you try
- Each task pre-fills the code you need. Write inside
main, or complete the function shown. - Some tasks ask for a function only — do not write your own
main; the checker adds one. - Press Run to compile and run, then Check answer. Your code compiles and runs on the server.
日本語
あなたも試してみよう
- 各タスクは必要なコードを事前に埋め込んでいます。
main内に記述するか、表示された関数を完成させてください。 - 一部のタスクでは関数のみを要求しています。ご自身で
mainを書かないでください;チェックツールが追加します。 - 実行 を押してコンパイル・実行後、答えを確認 を押してください。コードはサーバー上でコンパイル・実行されます。
Explore · 探索
Compile then run
C source is compiled to a program, which you then run.
Inside main, use printf(...) to print exactly Hello, C! on its own line.
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。
Complete the function int square(int n) so it returns n * n. The checker calls it with several values. Do not write a main — the checker provides one.
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。
Inside main, declare int grade = 11;. Then use printf to print two lines: first Hello!, then Grade 11 — print the number with a %d placeholder, not by typing 11.
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。