Monday, December 8, 2008

มาเรียนภาษา ซี กันเถอะ The stuff you need to know before you read all the other stuff in this book...

ขอได้รับคำขอบคุณจาก ซีฟอร์ดัมมี่ เซคเคินอิดิชั่น แอนด์ บิททอแรนท์...

The stuff you need to know before you read all the other stuff in this book...

you need two things to program in C on your computer:
- A compiler
- A place to put your programs

For Mac OS X:
GCC compiler is installed and ready to used.
To confirm this, open a terminal window and type the following line at the command prompt
gcc -v

Making programs, you need two tools:
- an editor
- a compiler

For the various UNIX operating systems, you have multiple editor choices.
The simplest text editor is Easy Editor, activated with the ee command.
The author' favorite editor for working with C is vim, a variant on the infamous vi editor in UNIX. Unlike vi, vim uses colors to code text.

Compiling and linking:
This step transforms the meek and mild text file into a robust and useable program on your computer.
The main thrust of programming the individual examples in this book involves the following three steps.
1. Ensure that you're in the work folder

2. Use your text editor to create your source code file

3. Compile and link the source code:
Compiling and linking are both handled by the GCC command. As an example, here's what you need to type to compile and link the GOODBYE.C source code created in Step 1:
gcc goodbye.c -o goodbye
- gcc, the command to compile and link the source code
- goodbye.c, the name of the source code file
- -o, the output switch
- goodbye, the name of the final program
If you leave off the -o switch and its option, GCC creates the program file named a.out. The author doesn't recommend this. Instead, remember the -o option and specify a nmae for the output program. The name can be the same as the source code file, but without the .c extension.

4. Run the program
UNIX runs only programs found on the path, and the author doesn't recommend to put your learn directory on the path.
To get the operating system to notice your program, you have to be specific about where the program lives (in the current folder, for example). you do that by prefixing ./ to the program's name. To run the goodbye program, type the following at the prompt:
./goodbye
and the program runs.

Tip: For each program you create, there are two files on disk: the source code file (a text file) and the program file. That's generally how things go in this book.
Tip: The GCC compiler automatically sets the permission bits on the resulting program file, allowing it to be run. Usually, it's -rwxr-xr-x
If you create your own programs that you want to run, copy them to a bin directory beneath your home directory, and put that directory on the path.


No comments: