note <practical c programming> chapter 5

Practical C Programming                Chapter 5: Arrays, Qualitifiers, and Reading Numbers


1. Arrays:

    type array_name[length]; /* comment */
    to reference an element of an arrya, you use a number named index


2. Strings:

    Strings are sequences of characters
    the '\0'(NUL) is used to indicate the end of a string
    char string_name[length]; /* comment */
    C does not allow one array to be assigned to another, instead use the standard library function strcpy
        Function                            Description
        strcpy(string1, string2)    copy string2 into string1
        strcat(string1, string2)     concatenate string2 onto the end of string1
        length = strlen(string)      get the length of a string
        strcmp(string1, string2)    0 if string1 equals string2, otherwise nonzero


3. Reading Strings

    fgets(name, sizeof(name), stdin);
        name: the name of a character array. The line (including the end-of-line charavter) is read into this array;
        sizeof(name): indicates the maximum number of characters to read (plus one of the end-of-string character);
        stdin: the file to read


4. Multidimensional Arrays:

    type variable[size1][size2]; /* comment */
    C allows the programmer to use as many as dimensions as needed.


5. Reading numbers

    scanf provides a simple and easy way of reading numbers that almost never works.
    the scanf is notorious for its poor end-of-line handling, which makes scanf useless for all but an expert.
    Instead use fgets to read a line of input and sscanf to convert the text into numbrs.
        char line[256];
        fgets(line, sizeof(line), stdin);
        sscanf(line, format, &variable1, &variable2, ... );


6. Initializing variables

    C allows variable to be initialized in the declaration statement.


7. Types of Integers:

    long int
    short int
    signed int
    Integer printf/sscanf conversions
        %conversion    uses
        %hd                   (signed) short int
        %d                     (signed) int
        %ld                    (signed) long int
        %hu                   unsigned short int
        %u                     unsigned int
        %lu                    unsigned long int
    long int declarations allow the program to explicitly specify extra precision where it is needed (at the expense of memory).
    short int numbers save space but have a more limited range
    the most compact integers have type char, they also have the most limited range
    unsigned numbers provide a way of doubling the positive range at expense of elminating negative numbers.


8. Types of floats:

    float
    double
    long double
    float printf/sscanf conversions
        %conversion    uses               notes
        %f                       float                 printf only
        %lf                     double             scanf only
        %Lf                    long double    Not available on all compilers
    on some machines, single-precision, floating-point instructions execute faster (but less accurately) than double-precision instructions. Double-precision intructions gain accuracy at the expense of time and storage.
    In most cases, float is adequate; however, if accuracy is a problem, switch to double


9. Constant Declarations

    the keyword const indicates a variable that never changes
    const type VARIABLE = value; /* comment */
    constants must be initialized at declaration time and can never be changed.


10. Hexadecimal and Octal Constants

    leading zeros are used to signal an octal constant;
    staring a number with "0x" indicates a hexadecimal constant


11. operators for performing shortcuts

    ++ : used for incrementing
    -- : used for decrementing
    operator    shorthand    equivalent statement
    +=                x += 2;         x = x + 2;
    -=                 x -= 2;          x = x - 2;
    *=                 x *= 2;         x = x * 2;
    /=                 x /= 2;          x = x / 2;
    %=              x %= 2;        x = x % 2;


12. Side effects:

    a side effect is an operation that is performed in addition to the main operation executed by the statement.
    Never use ++ or -- as part of any other statement, and always put them on lines by themselves

13. ++x or x++

14. more side-effect problems

        value = 1;
        result = (value++ * 5) + (value++ * 3);
    in order to avoid trouble and keep the program simple, always put ++ and -- on a line by themselves


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
This book is devoted to practical C programming. C is currently the premier language for software developers. That's because it's widely distributed and standard. Newer languages are available, such as C++, but these are still evolving. C is still the language of choice for robust, portable programming. This book emphasizes the skills you will need to do real-world programming. It teaches you not only the mechanics of the C language, but the entire life cycle of a C program as well (including the program's conception, design, code, methods, debugging, release, documentation, maintenance, and revision). Good style is emphasized. To create a good program you must do more than just type in code. It is an art in which writing and programming skills blend themselves together to form a masterpiece. True art can be created. A well-written program not only functions correctly, but is simple and easy to understand. Comments allow the programmer to include descriptive text inside the program. When clearly written, a commented program is highly prized. A program should be as simple as possible. A programmer should avoid clever tricks. This book stresses simple, practical rules. For example, there are 15 operator precedence rules in C. These can be simplified into two rules: 1. Multiply and divide come before add and subtract. 2. Put parentheses around everything else. Consider two programs. One was written by a clever programmer using all the tricks. The program contains no comments, but it works. The other program is well commented and nicely structured, but it doesn't work. Which program is more useful? In the long run, the broken one. It can be fixed. Although the clever program works now, sooner or later all programs have to be modified. The worst thing that you will ever have to do is to modify a cleverly written program. This handbook is written for people with no previous programming experience or programmers who already know C and want to improve their style and reliability. You should have access to a computer and TEAM FLY PRESENTS 9 know how to use the basic functions such as a text editor and the filesystem. Specific instructions are given for producing and running programs using the UNIX operating system with a generic cc compiler or the Free Software Foundation's gcc compiler. For MS-DOS/Windows users, instructions are included for Borland C++, Turbo C++, and Microsoft Visual C++. (These compilers compile both C and C++ code.) The book also gives examples of using the programming utility make for automated program production. How This Book is Organized You must crawl before you walk. In Part I we teach you how to crawl. These chapters enable you to write very simple programs. We start with the mechanics of programming and programming style. Next, you learn how to use variables and very simple decision and control statements. In Chapter 7, we take you on a complete tour of the software life cycle to show you how real programs are created. Part II describes all of the other simple statements and operators that are used in programming. You'll also learn how to organize these statements into simple functions. In Part III we take our basic declarations and statements and learn how they can be used in the construction of advanced types such as structures, unions, and classes. We'll also introduce the concept of pointers. Finally, a number of miscellaneous features are described Part IV. Chapter

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值