The first lesson is just to get you to write, compile, and execute a very simple program. It is a very basic example commonly used to introduce C.
- Open an editor (Notepad under Windows, Xemacs under Linux, Sublime, etc.) and enter the following text:
-
The preprocessor instructions refer to the instructions to the
preprocessor of the compiler. Such instructions always start with # and
are executed before the actual compilation starts.
#include <filename>
is simply replaced by the content of filename, which therefore includes a bunch of useful function definitions for usage in the present program. -
main() is the header of the program, every
program starts with this header.
int is the value returned to the caller of the program. - The body of the program is enclosed by the braces {}
- The program contains two statements (terminated with ;). A statement may be a simple statement or a compound statement.
-
printf("Hello World!");
prints "Hello World!" to the console. -
return 0;
is the last statement in the program and returns 0 to the caller. - Comments are added to explain to the reader what the program is doing, but these lines are ignored by the compiler.
- Multi-‐line comments: enclosed by /* … */
- Single-‐line comments: starting with // …
- Save the file in some directory (e.g. …/CS100/summer_homeworks/prog01.c)
- Compile your program
- Open a terminal
- Go to the directory of your summer_homeworks
- Compile the program, e.g.:
-
gcc prog01.c
- "gcc" here represents a call to the GNU-‐C-‐Compiler
-
If you want to give a custom name to your executable, run
prog01.c –o prog
-
Run the program:
./prog

The examples and exercises in this booklet all involve simple single-‐file code examples, which can be compiled using (almost) any regular C compiler. For further details on how to open the terminal and on how to install and use a basic compiler on your system, please refer to the document CS100_build_environment_installation.pdf from the course webpage (http://mpl.sist.shanghaitech.edu.cn/CS100)
我发现上不去……反正这是文档里的。我猜是不是这个(http://mpl.sist.shanghaitech.edu.cn/Teaching_CS100.html)?