Chapter 2 - Types, Operators, and Expressions

Section 2.1 Variable Names

重要细节

Section 2.2 Data Types and Sizes

重要细节

char 字符型(一字节)
int 整型
float 单精度浮点型
double 双精度浮点型
long 限定intdouble
short 限定int
signed 限定charint
unsigned 限定charint

Excercise 2-1. Write a program to determine the ranges of char , short , int , and long variables, both signed and unsigned , by printing appropriate values from standard headers and by direct computation. Harder if you compute them: determine the ranges of the various floating-point types.

手动计算需要一些数字电路方面的概念。floatdouble这一块,参考 Explain this code in K&R 2-11111e28这个数没什么非常特别的地方,是一个属于可用区间的 cute number

delta needs to be small enough so if fla is the 2nd largest number, adding delta, would not sum right up to float.infinity and skip FLT_MAX

#include <stdio.h>
#include <limits.h>
#include <float.h>

int main() {
    unsigned char uc;
    signed char sc;
    unsigned short us;
    signed short ss;
    unsigned int ui;
    signed int si;
    unsigned long ul;
    signed long sl;
    unsigned long long ull;
    signed long long sll;
    float fl, fla, fll;
    double db, dba, dbl;
    long double ldb, ldba, ldbl;

    printf("Let's compute the minimums and maximums of each type!\n");
    uc = sc = us = ss = ui = si = ul = sl = sll = ull = 0;
    fl = fla = fll = db = dba = dbl = ldb = ldba = ldbl = 0.0;

    /* signed char (minimum, maximum), unsigned char(0, maximum) */

    sc++;
    signed char tmp_sc;
    while ((tmp_sc = sc * 2) > sc)
        sc *= 2;

    printf("`signed char` maximum: %d\n", sc = sc * 2 - 1);
    printf("`signed char` minimum: %d\n", ++sc);
    printf("`unsigned char` maximum: %u\n", --uc);

    /* short int, unsigned short */

    ss++;
    signed short tmp_ss;
    while ((tmp_ss = ss * 2) > ss)
        ss *= 2;

    printf("`signed short` maximum: %d\n", ss = ss * 2 - 1);
    printf("`signed short` minimum: %d\n", ++ss);
    printf("`unsigned short` maximum: %u\n", --us);

    /* signed int, unsigned int */

    si++;
    signed int tmp_si;
    while ((tmp_si = si * 2) > si)
        si *= 2;

    printf("`signed int` maximum: %d\n", si = si * 2 - 1);
    printf("`signed int` minimum: %d\n", ++si);
    printf("`unsigned int` maximum: %u\n", --ui);

    /* signed long, unsigned long */

    sl++;
    signed long tmp_sl;
    while ((tmp_sl = sl * 2) > sl)
        sl *= 2;

    printf("`signed long` maximum: %li\n", sl = sl * 2 - 1);
    printf("`signed long` minimum: %li\n", ++sl);
    printf("`unsigned long` maximum: %lu\n", --ul);

    /* signed long long, unsigned long long */

    sll++;
    signed long long tmp_sll;
    while ((tmp_sll = sll * 2) > sll)
        sll *= 2;

    printf("`signed long long` maximum: %lli\n", sll = sll * 2 - 1);
    printf("`signed long long` minimum: %lli\n", ++sll);
    printf("`unsigned long long` maximum: %llu\n", --ull);

    /* float */
    while (fl == 0.0) {
        fll = fla;
        fla = fla + 1111e28;
        fl = (fl + fla) - fla;
    }

    printf("`float` maximum: %e\n", fll);
    printf("`float` minimum: %e\n", 0 - fll);

    /* double */
    while (db == 0.0) {
        dbl = dba;
        dba = dba + 1111e297;
        db = (db + dba) - dba;
    }

    printf("`double` maximum: %e\n", dbl);
    printf("`double` minimum: %e\n", 0 - dbl);

    printf("\nNow, let's cheat and use the helpful headers!\n");
    printf("`char`s go from %d to %d (unsigned, up to %u)\n", SCHAR_MIN, SCHAR_MAX, UCHAR_MAX);
    printf("`short`s go from %d to %d (unsigned, up to %u)\n", SHRT_MIN, SHRT_MAX, USHRT_MAX);
    printf("`int`s go from %d to %d (unsigned, up to %u)\n", INT_MIN, INT_MAX, UINT_MAX);
    printf("`long`s go from %ld to %ld (unsigned, up to %lu)\n", LONG_MIN, LONG_MAX, ULONG_MAX);
    printf("`float`s range from %e to %e\n", 0.0 - FLT_MAX, FLT_MAX);
    printf("`double`s range from %e to %e\n", 0.0 - DBL_MAX, DBL_MAX);
    return 0;
}

Section 2.3 Constants

重要细节

Section 2.4 Declarations

重要细节

Section 2.5 Arithmetic Operators

重要细节

Section 2.6 Relational and Logical Operators

重要细节

Section 2.7 Type Conversions

重要细节

Section 2.8 Increment and Decrement Operators

重要细节

Section 2.9 Bitwise Operators

重要细节

Section 2.10 Assignment Operators and Expressions

重要细节

Section 2.11 Conditional Expressions

重要细节

Section 2.12 Precedence and Order of Evaluation

重要细节