C&C++学习笔记2

来源:互联网 发布:列宁格勒 知乎 编辑:程序博客网 时间:2024/05/17 22:02

《C Primer Plus 5th Edition》摘录

Object Code Files,Executable Files,and Libraries

basic strategy:  use programs that convert your source code fileto an executable file ,which is a file containing ready-to-run machine language code.C implementations do this in two steps:compiling and linking.The compiler converts your source code to an intermediate code, and the linker combines this with other code to produce the executable file. C uses this two-part approach to facilitate the modularization of programs. You can compileindividual modules separately and then use the linker to combine the compiled modules later. That way ,if you need to change one module , you don't have to recompile the other ones. Also,the linker combines your program with precompiled library code.

Compiler and linker 

A Simple Example of C

#include <stdio.h>int main(void) /* a simple program */{int num; /* define a variable called num */num = 1; /* assign a value to num */printf("I am a simple "); /* use the printf() function */printf("computer.\n");printf("My favorite number is %d because it is first.\n",num);return 0;}


The main() function

int main(void)

main is a rather plain name,but it is the only choice available. A C program (With some exceptions we won't worry about ) always begins execution with the function calledmain().

you are free to choose names for other functions you use, but main() must be there to start things. What about the parentheses? They identify main() as a function. 

The int is the main() function's return type.That means that the kind of value main() can return is an integer.

/* a simple program */
The parts of the program enclosed in the /* */ symbols are comments.Using comments makes it easier for someone (including yourself) to understand your program.One nice feature of C comments is that they can be placed anywhere even on the same line as the material they explain. A longer comment can be placed on its own line or even spread over more than one line. Everything between the opening /* and */ is ignored by the compiler.

C99 adds a second style of comments, //

//Here is a comment confined to one line

Braces,Bodies,and Blocks

{

....

}

braces delimited the main() function

Declarations

int num; /* define a variable called num */

This line from the program is termed a declaration statement.The declaration statement is of C's most important features.This particular example declares  two things.

First, somewhere in the function, you have a variable called num.

Second, the int proclaims num as an integer---that is, a number without a decimal point or fractional part 

int is an example data type and the word int is a C keyword identifying one of the basic C data types.Keywords are the words used to express a language and you can't usurp them for other purposes. 

In C, all variables must be declared before they are used. This means that you have to provide lists of all the variables you use in a program and that you have to show which data type each variable is. Declaring variables is considered a good programming technique, and, in C, it is mandatory.


Data Types

C deals with several kinds of data: integers, characters, and floating point, for example. Declaring a variable to be an integer or a character type makes it possible for the computer to store,fetch,and interpret the data properly. 


Name Choice 

You should use meaningful names for variables.

Valid Names Invalid Names

wiggles $Z]**
cat2 2cat
Hot_Tub Hot-Tub
taxRate tax rate
_kcab don't

Four Good Reasons to Declare Variables

1 Putting all the variables in one placemakes it easier for a reader to grasp what the program is about. This is particularly true if you give your variables meaningful names (such as taxrate instead of r). If the name doesn't suffice, use comments to explain what the variables represent. Documenting a program in this manner is one of the basic techniques of good programming.
2 Thinking about which variables to declare encourages you to do some planning before plunging into writing a program. What information does the program need to get started? What exactly do I want the program to produce as output? What is the best way to represent the data?
3 Declaring variables helps prevent one of programming's more subtle and hard-to-find bugs—that of the misspelled variable name. 

4 Your C program will not compile if you don't declare your variables. If the preceding reasons fail to move you, you should give this one serious thought.


1 0