zoukankan      html  css  js  c++  java
  • ReLearn C(The C Programming Language.2Nd)

    Chapter 1 - A Tutorial Introduction
     
    printf is not part of the C language; there is no input or output defined in C itself. printf is just a useful function from the standard library of functions that are normally accessible to C programs. The behaviour of printf is defined in the ANSI standard, however, so its properties should be the same with any compiler and library that conforms to the standard.
     
     
    The solution is that getchar returns a distinctive value when there is no more input, a value that cannot be confused with any real character. This value is called EOF, for ``end of file''. EOF is an integer defined in <stdio.h>, but the specific numeric value doesn't matter as long as it is not the same as any charvalue.
     
     
    The grammatical rules of C require that a forstatement have a body. The isolated semicolon, called a null statement, is there to satisfy that requirement. We put it on a separate line to make it visible.
     
     
    In C, all function arguments are passed ``by value.'' This means that the called function is given the values of its arguments in temporary variables rather than the originals. This leads to some different properties than are seen with ``call by reference'' languages like Fortran or with varparameters in Pascal, in which the called routine has access to the original argument, not a local copy.
    When necessary, it is possible to arrange for a function to modify a variable in a calling routine. The caller must provide the address of the variable to be set (technically a pointer to the variable), and the called function must declare the parameter to be a pointer and access the variable indirectly through it.
    The story is different for arrays. When the name of an array is used as an argument, the value passed to the function is the location or address of the beginning of the array - there is no copying of array elements.
     
     
    Each local variable in a function comes into existence only when the function is called, and disappears when the function is exited. This is why such variables are usually known as automatic variables, following terminology in other languages.
     
    As an alternative to automatic variables, it is possible to define variables that are external to all functions, that is, variables that can be accessed by name by any function.The variable must also be declaredin each function that wants to access it;
     
     
    ``Definition'' refers to the place where the variable is created or assigned storage; ``declaration'' refers to places where the nature of the variable is stated but no storage is allocated.
     
     
    Chapter 2 - Types, Operators and Expressions
     
    There are only a few basic data types in C:
    char a single byte, capable of holding one character in the local character set
    int an integer, typically reflecting the natural size of integers on the host machine
    float single-precision floating point
    double double-precision floating point
     
     
    Technically, a string constant is an array of characters. The internal representation of a string has a null character '\0'at the end, so the physical storage required is one more than the number of characters written between the quotes.
    Be careful to distinguish between a character constant and a string that contains a single character: 'x'is not the same as "x". The former is an integer, used to produce the numeric value of the letter xin the machine's character set. The latter is an array of characters that contains one character (the letter x) and a '\0'.
     
     
     
    External and static variables are initialized to zero by default. Automatic variables for which is no explicit initializer have undefined (i.e., garbage) values.
     
     
    When an operator has operands of different types, they are converted to a common type according to a small number of rules. In general, the only automatic conversions are those that convert a ``narrower'' operand into a ``wider'' one without losing information, such as converting an integer into floating point in an expression like f + i.
     
     
    The language does not specify whether variables of type char are signed or unsigned quantities. When a charis converted to an int, can it ever produce a negative integer? The answer varies from machine to machine, reflecting differences in architecture.
     
     
    If there are no unsigned operands, however, the following informal set of rules will suffice:
    • If either operand is long double, convert the other to long double.
    • Otherwise, if either operand is double, convert the other to double.
    • Otherwise, if either operand is float, convert the other to float.
    • Otherwise, convert char and short to int.
    • Then, if either operand is long, convert the other to long.
    Notice that floats in an expression are not automatically converted to double; this is a change from the original definition.
     
     
     
    Chapter 4 - Functions and Program Structure
     
    External variables are defined outside of any function, and are thus potentionally available to many functions. Functions themselves are always external, because C does not allow functions to be defined inside other functions.
    The scope of an external variable or a function lasts from the point at which it is declared to the end of the file being compiled.
    If an external variable is to be referred to before it is defined, or if it is defined in a different source file from the one where it is being used, then an externdeclaration is mandatory.
     
    The static declaration, applied to an external variable or function, limits the scope of that object to the rest of the source file being compiled.
    Internal static variables are local to a particular function just as automatic variables are, but unlike automatics, they remain in existence rather than coming and going each time the function is activated. This means that internal static variables provide private, permanent storage within a single function.
     
     
    A register declaration advises the compiler that the variable in question will be heavily used. The idea is that registervariables are to be placed inmachine registers, which may result in smaller and faster programs. But compilers are free to ignore the advice.
    The register declaration can only be applied to automatic variables and to the formal parameters of a function.
     
     
    The preprocessor operator ## provides a way to concatenate actual arguments during macro expansion.
     
    Chapter 5 - Pointers and Arrays
     
     
    The & operator only applies to objects in memory: variables and array elements. It cannot be applied to expressions, constants, or register variables.
     
    The unary operator * is the indirection or dereferencing operator; when applied to a pointer, it accesses the object the pointer points to.
     
     
    When main is called, it is called with two arguments. The first (conventionally called argc, for argument count) is the number of command-line arguments the program was invoked with; the second (argv, for argument vector) is a pointer to an array of character strings that contain the arguments, one per string.By convention, argv[0]is the nameby which the program was invoked, so argc is at least 1.
     
     
     
    Chapter 6 - Structures
     
    The two components can be placed in a structure declared like this:
    struct point {
    int x;
    int y;
    };
    The keyword struct introduces a structure declaration, which is a list of declarations enclosed in braces. An optional name called a structure tag may follow the word struct(as with point here). The tag names this kind of structure, and can be used subsequently as a shorthand for the part of the declaration in braces.
     
    A struct declaration defines a type. The right brace that terminates the list of members may be followed by a list of variables, just as for any basic type. That is,
    struct { ... } x, y, z;
    is syntactically analogous to
    int x, y, z;
     
     
    Ifthe declaration is tagged, however, the tag can be used later in definitions of instances of the structure. For example, given the declaration of point above,
    struct point pt;
     
     
    The parentheses are necessary in (*pp).x because the precedence of the structure member operator . is higher then * . The expression *pp.x means *(pp.x), which is illegal here because x is not a pointer.
     
    C provides a compile-time unary operator called sizeof that can be used to compute the size of any object. The expressions
    sizeof object
    and
    sizeof (type name)
    yield an integer equal to the size of the specified object or type in bytes. (Strictly, sizeofproduces an unsigned integer value whose type, size_t, is defined in the header <stddef.h>.)
     
    A sizeof can not be used in a #if line, because the preprocessor does not parse type names. But the expression in the #define is not evaluated by the preprocessor, so the code here is legal.
     
    Don't assume, however, that the size of a structure is the sum of the sizes of its members. Because of alignment requirements for different objects, there may be unnamed ``holes'' in a structure.
     
     
    It is illegal for a structure to contain an instance of itself, but
    struct tnode *left;
    declares left to be a pointer to a tnode, not a tnode itself.
     
    Alignment requirements can generally be satisfiedeasily, at the cost of some wasted space, by ensuring that the allocator always returns a pointer that meets all alignment restrictions.
     
     
    A union is a variable that may hold (at different times) objects of different types and sizes, with the compiler keeping track of size and alignment requirements.Unions provide a way to manipulate different kinds of data in a single area of storage, without embedding any
    machine-dependent information in the program.
     
    union u_tag {
    int ival;
    float fval;
    char *sval;
    } u;
    The variable u will be large enough to hold the largest ofthe three types; the specific size is implementation-dependent.
     
     
    In effect, a union is a structure in which all members have offset zero from the base, the structure is big enough to hold the ``widest'' member, and the alignment is appropriate for all of the types in the union.
     
     
    Chapter 7 - Input and Output
     
     
     
    This pointer, called the file pointer, points to a structure that contains information about the file, such as the location of a buffer, the current character position in the buffer, whether the file is being read or written, and whether errorsor end of file have occurred.
     
    FILE *fp;
    FILE *fopen(char *name, char *mode);
    This says that fpis a pointer to a FILE, and fopen returns a pointer to a FILE. Notice that FILE is a type name, like int, not a structure tag; it is defined with a typedef.
     
    If a file that does not exist is opened for writing or appending, it is created if possible.
     
    When a C program is started, the operating system environment is responsible for opening three files and providing pointers for them. These files are the standard input, the standard output, and the standard error; the corresponding file pointers are called stdin, stdout, and stderr, and are declared in <stdio.h>. Normally stdinis connected to the keyboard and stdoutand stderrare connected to the screen, but stdinand stdout may be redirected to files or pipes as described in Section 7.1.
     
     
    Chapter 8 - The UNIX System Interface
     
    In the UNIX operating system, all input and output is done by reading or writing files, because all peripheral devices, even keyboard and screen, are files in the file system.
     
     
    When the command interpreter (the ``shell'') runs a program, three files are open, with file descriptors 0, 1,and 2, called the standard input, the standard output, and the standard error.
     
     
    The user of a program can redirect I/O to and from files with < and >:
    prog <infile >outfile
     
     
     
    Input and output uses the read and write system calls, which are accessed from C programs through two functions called readand write.
    int n_read = read(int fd, char *buf, int n);
    int n_written = write(int fd, char *buf, int n);
    Each call returns a count of the number of bytes transferred. On reading, the number of bytes returned may be less than the number requested. A return value of zero bytes implies end of file, and -1 indicates an error of some sort. For writing, the return value is the number of bytes written; an error has occurred if this isn't equal to the number requested.
     
     
    int creat(char *name, int perms);
    fd = creat(name, perms);
    returns a file descriptor if it was able to create the file, and -1if not. If the file already exists, creat will truncate it to zero length, thereby discarding its previous contents; it is not an error to creata file that already exists. If the file does not already exist, creatcreates it with the permissions specified by the perms argument.
  • 相关阅读:
    Springboot使用PlatformTransactionManager接口的事务处理
    js 正则替换html标签
    【转】mysql查询时,查询结果按where in数组排序
    js输出字幕数字a-zA-Z0-9
    tcpdump使用教程
    rsync安装使用教程
    vim配置修改教程
    XD刷机报错bad CRC
    使用docker搭建seafile服务器
    案例:使用sqlplus登录报ORA-12547错误
  • 原文地址:https://www.cnblogs.com/chiefhsing/p/3091156.html
Copyright © 2011-2022 走看看