/****************************************************
* Author: omohe
*Date: 2007-9/10
*Some rights reserved.
**************************************************** /
1. Advices about learning C or C++:. 1
4. What compilers are available?. 3
5. A Book Sequence: From C++ Beginner To C++ Expert. 4
8. What does it take to learn to be a programmer? (10-9). 5
10. How to lay out your program structure in your mind before you begin to write code 6
11. The Hows and Whys of Commenting.. 7
12. Good Programming Style:. 8
14. Writing for Readability. 9
15. Why do I want to learn C.. 10
1. Advices about learning C or C++:
What is C++?
What is Object-Oriented Programming?
Object oriented programming is essentially building a program around self-contained collections of data and code to modify that data; this programming model is in contrast to a model that uses function that act on data scattered throughout a program. Object-oriented programming (or coding, as programming is commonly referred to) is an organizational style, but it helps programmers create reusable code because the code to do a specific thing is entirely contained within a single section of code, and to use the code to perform tasks - for instance, creating a menu - involves using only a small number of functions to access the internals of the class. Think of it as a black box that can be easily carried from place to place, and that performs complex actions simply at the press of a button: for instance, a microwave lets you heat food for a specified time limit - say, two minutes - by typing in the time and pressing the heat button. You do not need to know how the microwave operates or why the physics works. In the same way that self-contained appliances simplify life for the consumer, object-oriented programming simplifies the transfer of source code from one program to another program by encapsulating it - putting it all in one place.
2. Compilers
Numerous compilers are available for C and C++.
A compiler is necessary to make your source code (..c, .cpp, or .cc files) into a running program.
Understanding the Compilation Process
Compilation refers to the processing of source code files (.c, .cc, or .cpp) and the creation of an 'object' file. This step doesn't create anything the user can actually run. Instead, the compiler merely produces the machine language instructions that correspond to the source code file that was compiled. For instance, if you compile (but don't link) three separate files, you will have three object files created as output, each with the name <filename>.o or <filename>.obj (the extension will depend on your compiler). Each of these files contains a translation of your source code file into a machine language file -- but you can't run them yet! You need to turn them into executables your operating system can use. That's where the linker comes in.
Linking refers to the creation of a single executable file from multiple object files.
Link如果找不到函数或者变量的定义,会到其他文件中寻找。
为什么要分开compilation和linking steps呢:
明显的原因是当编写较大的程序的时候,不需要每次都编译所有的文件。比如可以使用”conditional compilation”,只编译那些已经修改的source files;
另一个原因是implement libraries of pre-compiled code更加方便,只需要create object files and like them just like any other object file.(如各种动态链接库的使用)。
条件编译需要知道那些文件已经修改since you last compiled, 但是如果使用IDE的话,通常IDE已经take care of this for you.
了解compilation phase和link phase的不同可以easier to hunt for bugs. Compiler errors经常是一些语法错误;Linking errors经常是缺少定义等。
Dealing with Compiler Errors - Surviving the Compilation Process
Types of Compilation Errors
most compilers will give three types of compile-time alerts: compiler warnings, compiler errors, and linker errors.
1. compiler warnings: Usually, compiler warnings are an indication that something might go wrong at runtime. Generally, you can set the warning level of your compiler--I like to keep it at its highest level so that my compiler warnings don't turn in to bugs in the running program ('runtime bugs'). Nevertheless, compiler warnings aren't going to stop you from getting your program working (unless you tell your compiler to treat warnings as errors)
2. Errors are conditions that prevent the compiler from completing the compilation of your files.
Compiler errors are restricted to single source code files and are the result of 'syntax errors'.
What this really means is that you've done something that the compiler cannot understand.
3. Linker errors, unlike compiler errors, have nothing to do with incorrect syntax. Instead, linker errors are usually problems with finding the definitions for functions, structs, classes, or global variables that were declared, but never actually defined.
Compiler Errors - Where do you start?
This brings up a guiding principle of hunting down compiler errors: when in doubt, look earlier in the program.(遇到Compiler Errors, 从前面开始找错误)
vi, vim(Vi IMproved) , Emacs…
http://fmwww.bc.edu/repec/bocode/t/textEditors.html
4. What compilers are available?
Windows/DOS
- Code::Blocks and MINGW Our recommended free compiler setup!
- Borland Find out how to download and set up Borland's free command-line compiler
- DJGPP Read about DJGPP, a DOS-based compiler
- Dev-C++ and Digital Mars Read about Dev-C++, a good windows based compiler, and Digital Mars
Windows Only
- Microsoft Visual C++ Read about Visual C++
*nix
- g++ is a C++ compiler that comes with most *nix distributions.
- gcc is a C compiler that comes with most *nix distributions.
GDB (Gnu Debugger) is a powerful debugging tool for both C and C++.
各种IDE的内核compiler:
Mingw <place w:st="on"><placetype w:st="on">port</placetype> of <placename w:st="on">GCC</placename></place> (GNU Compiler Collection)
Cygwin or any other GCC based compiler等等
Free Compilers and Articles on Writing a Compiler:
http://www.bloodshed.net/compilers/index.html
说明:
*nix上一般使用gcc和g++;
Windows上最好的为Microsoft Visual C++ (6.0, 7.0/2003, 8.0/2005)等;但是含有复杂的SDK以及库MFC或者.NET库,因此如果想学好可以使用含有库很少的IDE:Code::Blocks(Open Source, 且支持插件扩展)
Setting Up Code::Blocks and the MINGW Compiler on Windows
http://www.cprogramming.com/code_blocks/
5. A Book Sequence: From C++ Beginner To C++ Expert
http://www.cprogramming.com/books.html
<chsdate year="2007" month="9" day="29" islunardate="False" isrocdate="False" w:st="on" tabindex="0" style="background-position: left bottom; background-image: url(res://ietag.dll/#34/#1001); background-repeat: repeat-x;"><span lang="EN-US" style="font-family: Verdana;">2007-9-29</span></chsdate>
http://www.cprogramming.com/begin.html
6. How do you learn C++?
1. studying by reading tutorial or books.
2. often helpful to type the code into the compiler and run it (not copy and paste even if you can!), because by typing you can:
a. get used to the typical typing errors that cause problems
b. it will force you to pay attention to the details of programming syntax
c. Typing your program will also familiarize you with the general structure of programs and with the use of common commands.
d. play with the program and test your own ideas, and can experiment with it by myself
e. By seeing which modifications cause problems and which sections of the code are most important to the function of the program, you should learn quite a bit about programming.
Programming is just plain fun. And you can read opinion on the matter here:
7. Why learn to program?
http://www.cprogramming.com/whyprogram.html
(so proud of Knowing the Answer to the "Why" Questions)
It may be a challenging road, but it is doable.
As you learn, you'll want to pay special attention to anything you can about designing programs. When you start to design your first real game, you'll need to think both about plot and code, and the more ideas you've already picked up about how to design programs, the easier you will find it to plan out your game. In fact, as you start to write more interesting and difficult practice programs, you would probably benefit from taking the time to plan out the high-level structure of the program in advance. Design will be all the more important if you decide you want to work with a friend or as part of a team--without having a design up-front, integrating code written by several people can be extremely hard.
If you work hard at these things--learning your language of choice well and focusing on program design concepts--you will be well-positioned to write a good game and succeed in doing so. I've seen people go from no programming experience to writing fun graphics-intense games in about six months working at 10 to 20 hours per week. If you bear down and learn to program one step at a time, eventually you'll reach the point where you can start writing the sort of programs that you really want to write. As a side benefit, you will have a good set of programming skills in case you decide that you're more interested in other aspects of computer science than writing games!
8. What does it take to learn to be a programmer? (10-9)
1.Some helpful traits you should have and develop:
1) Programming is more of a designer’s task: to be a good programmer, having an eye for style and good design
What I really mean by style is that you have to have a good sense for discriminating between "good" and "bad" approaches to attacking problems.
If you're disciplined about it, you'll take some time to plan things out on paper and figure out more or less what you'd like your program to do. That's great, but it won't substitute for having actually used the program and noticed that, yes, it would be fantastic to add this one little feature here.
The more willing you are to put in the up-front thinking before designing your program, the easier you will find the actual writing of code. This is not to say that when you're first learning you shouldn't just write some simple programs without worrying too much about these issues. But you should be prepared to pay attention to these things and what problems your first programs did have.
<真正地从设计者的角度去思考自己的program,在coding之前就要想得足够全面,设计得足够合理>
2)The second trait that you need is patience.
the better you get as you practice, the more you find that your bugs are interesting
Life is not bleak: not all of your time will be spent finding bugs!
3) think in a precise, rigorous way
You have to be willing and able to specify all of the details in a process and understand exactly what goes into it. This can lead to some amazing realizations.
You need to have the ability to in some way come to understand the entirety of a process at the level of detail required for a computer to be able to mechanically reproduce it.
4) capable of framing problems the right way and be or become a good problem solver
Finally: If you are persistent, willing to pay attention to issues of design and focus on both problem solving and precise solutions to problems, you will go far as a programmer. If not, then you may find a programming career to be frustrating and tedious.
http://cprogramming.com/whatdoesittake.html
It is a common problem for your program's output window to close before you can see the result of the program. The simplest solution is to require the program to wait for the user to input a final keypress before closing. The simplest solution is to add the following code at the end of your program, just before the return 0;:
cin.get();
This code will cause your compiler to wait until you press a key before closing the program's output window.
10. How to lay out your program structure in your mind before you begin to write code
1. start writing down a layout for the program
2. There are two standard methods of program design:
the top-down approach: Top-down programming involves writing code that calls functions you haven't defined and working through the general algorithm before writing the functions that do the processing. Top-down programming is, to a good degree, a very abstract way of writing code because it starts out by using functions you haven't designed, and that you perhaps do not know how to design.
the bottom-up approach: the programmer writes the basic functions she realizes will be necessary at some point in the programming and then work up to the more complex parts of the program.
3. Many times, the best way to write a program is to figure out the variables you need to work with and then progress with a top-down approach to the program that manipulates those variables
By defining variables first and then working with functions that work on them, you will always maintain a basic foundation of what your program should be doing. Once you have an idea of what variables you will be using, then you can write functions to perform the operations you need to perform on the variables while maintaining site of the goal. Finally you can write the code for each individual function.
11. The Hows and Whys of Commenting
1. A good program is beautiful in its concept -- the algorithm used, the design, the flow of control -- but also in its readability. Readable code, while a nice ideal, often requires some help from the English (or from some other) language.
Comments should reveal the underlying structure of the code, not the surface details. A more meaningful comment would be "int callCount = 0; //holds number of calls to [function]."
Comments should not be overly long. Comments should not give details for the sake of details; only when a fact is necessary or interesting should it be brought to the attention of the program's reader.
2. For the sake of future readers, you should generally include some header information at the top of your program.
This information may include your name and contact information, the date the code was last modified, the purpose of the program, and if necessary, a brief exposition of the algorithm used or the design decisions made. You may also want to include a list of known bugs, inefficiencies, or suggestions for improvement. It is convenient to demarcate this section of the program file in a large comment block of the form.
/********
* *
* *
********/
3. Whenever you create a new class or a new function definition, you should add a comment explaining what the class or function does.
For a class: you should explain the purpose of the class, what publicly accessible functions and variables are available, any limitations of the class, and information that the programmer may need if he or she wanted to inherit from the class.
When defining a function: you should describe what the function does and whether or not it has side effects such as changing global variables, interacting with the user, or so forth. It is also convenient to describe what sort of arguments the function takes and what, if any, value it returns: e.g.
4. When adding comments directly to your code, be spare. The less you say, the better; if you force yourself to be precise, you will make the comments more helpful
5. Good commenting can improve your programming. You can use them as an organizational device by including comments prior to filling in code -- for instance, when you have a long block of conditional statements, you may wish to comment what should happen when each conditional is executed before you flesh out the code. In doing so, you save yourself the burden of remembering the details of the entire program, allowing you to concentrate on the implementation of one aspect at a time. Additionally, when you force yourself to comment during the programming process, you cannot get away with writing code that "you hope will work" if you make yourself explain why it works. (Keep in mind that if the code is so complex that you don't know how it works, then you probably should be commenting it for the sake of both yourself and others.)
6. Keep in mind that what seems obvious now may not seem obvious later. While you shouldn't excessively comment, do make sure to comment things that are nonstandard algorithms. You do not need to comment a programming idiom, but you do want to comment an algorithm you designed for the program, no matter how simple it may seem to you. No doubt it will seem foreign three weeks after you write the code, and if you plan (and eve n if you do not plan) to come back to the code, it will be immeasurably helpful.
12. Good Programming Style:
1. Consistency:
the fewer surprises, the better
2. Clarity:
making your program clear and understandable as well as easily modifiable.
When in doubt, choose the most easily understood technique for a problem. Remember that whatever you write, you will likely have to read at some point after you remember exactly what you were thinking. Do your future self a favor and be clear the first time.
3. White space and Formatting: Indentation; Brace Styles; Indentation Depth; Tabs vs. Spaces
4. Mis-Use of Whitespace
http://www.cprogramming.com/tutorial/style.html
13. Naming Conventions: <<chsdate year="2007" month="10" day="10" islunardate="False" isrocdate="False" w:st="on" tabindex="0" style="background-position: left bottom; background-image: url(res://ietag.dll/#34/#1001); background-repeat: repeat-x;">07-10-10</chsdate>>
1. General Naming Conventions:
1) It's usually best to choose a consistent set of naming conventions for use throughout your code.
2) Naming conventions usually govern things:
a. how you capitalize your variables, classes, and functions
b. whether you include a prefix for pointers, static data, or global data
c. how you indicate that something is a private field of a class.
3) common naming conventions for variables, classes, functions and objects:
a. C-style naming separates words in a name using underscores: this_is_an_identifer
b. camelCase: one that begins with a lowercase letter and then capitalizes the first letter of every ensuing word
c. CamelCase: one that capitalizes the first letter of every single word
One popular convention is that leading capital letter CamelCase is used for the names of structs and classes, while normal camelCase is used for the names of functions and variables (although sometimes variables are written in c-style to make the visual separation between functions and variables more clear).
4) use prefixes for certain types of data to remind you what they are
a. prefixing it with "p_" tells you that it's a pointer
b. use a prefix for global or static variables
5) a common convention is to prefix the private fields and methods of a class with an underscore
2. Hungarian Notation:
associated with prefixing variables with information about their type
3. Abbreviations:
Abbreviations are dangerous;
When possible, be consistent about using particular abbreviations, and restrict yourself to using only a small number of them.
14. Writing for Readability
1. Using Functions:
1) it's perfectly fine to use the same construction over and over again; turn a repeated chunk of code into a function: this is even more readable because it gives the block of code a descriptive name.
2) increasing readability by using standard functions and data structures (such as the STL)
Doing so avoids the confusion of someone who might ask, "why did you create a new function when you had a perfectly good one already available?"
by using standard functions you help your reader understand the names of the arguments to the function.
There's much less need to look at the function prototype to see what the arguments mean, or their order, or whether some arguments have default values.
2. Use Appropriate Language Features:
1) don't use a loop as though it were an if statement;
2) Choose the right data type for your data;
3) If you mean for a value to be unsigned, used an unsigned numbers;
4) When you want to indicate that a value should never change, use const to make it so.
5) avoid uncommon constructions unless you have good reason to use them
6) don't use a feature just because the feature exists
3. Unpack Complex Expressions:
4. Avoid Magic Numbers:
1) Magic numbers are numbers that appear directly in the code without an obvious reason
2) For instance, what does the number <chmetcnv unitname="in" sourcevalue="80" hasspace="True" negative="False" numbertype="1" tcsc="0" w:st="on" tabindex="0" style="background-position: left bottom; background-image: url(res://ietag.dll/#34/#1001); background-repeat: repeat-x;">80 in</chmetcnv> the following expression mean?
for( int i = 0; i < 80; ++i )
{
printf( "-" );
}
It might be the width of the screen, but it might also be the width of a map whose wall is being drawn. You just don't know
3) The best solution is to use macros, in C, or constants in C++.
A. This gives you the chance to descriptively name your numbers;
B. makes it easier to spot the use of a particular number and differentiate between numbers with the same value that mean different things
C. if you decide you need to change a value, you have a single point where you can make the change, rather than having to sift through your code.
http://www.cprogramming.com/tutorial/programming-style-readability.html
15. Why do I want to learn C (10-11)
http://www.cprogramming.com/whyc.html
1.lot of programming languages available right now
1) the extremely high level (such as Visual Basic)
2) the low level power of assembly
3) a good variety of specialized options in between (Perl, Ruby, and Python are good choices for many tasks).
4) Java has also become quite the hot programming language for some tasks, in part because of its large API and in part because the virtual machine provides some elements of security. (Garbage collection is another nice feature and can make programmers much more efficient.)
2. good reasons to learn to program in C
1) C has been around for 30 years, and there is a ton of source code available. This means there's a lot to learn from, and a lot to use.
2) C is a great language for expressing common ideas in programming in a way that most people are comfortable with.
A lot of the principles used in C -- for instance, argc and argv for command line parameters, as well as loop constructs and variable types -- will show up in a lot of other languages you learn so you'll be able to talk to people even if they don't know C in a way that's common to both of you.
3) C is reasonably close to the machine. When you're working with pointers, bytes, and individual bits, things like optimization techniques start to make a lot more sense.
a. helps a great deal when something you're trying to do in a higher level language seems way slower than expected, or just doesn't work at all
b. A higher level language will make it a little bit simpler, but it'll be harder to understand what's going on, and when things stop working, it's much better to know exactly what's going on so you can fix it.
c. If you like computer science as a discipline, or just like knowing how things work learning the details of the system is great fun.