Квалификатор const

Изменения в новых версиях / C89

// *** было: ***
//В K&R C не было const: константа была макросом без типа,
//и ничто не мешало функции писать в строку, которую она только читает
#define TAX_RATE 0.2

int countSpacesOld(text)
char *text;
{
    int n = 0;
    for (; *text != '\0'; text++)
        if (*text == ' ')          //промах *text = ' ' компилировался молча
            n++;
    return n;
}

char nameOld[] = "Ann Lee";
int spacesOld = countSpacesOld(nameOld);
double taxOld = 100 * TAX_RATE;
//spacesOld is 1
//taxOld is 20

printf("spacesOld is %d\n", spacesOld);
printf("taxOld is %g\n", taxOld);

// *** в версии C89: ***
//const - обещание, и компилятор требует его выполнять
int countSpaces(const char *text) {
    int n = 0;
    for (; *text != '\0'; text++)
        if (*text == ' ')
            n++;
    //*text = ' ';    <- error: assignment of read-only location
    return n;
}

const double taxRate = 0.2;       //константа с типом
//taxRate = 0.3;    <- error: assignment of read-only variable 'taxRate'

const char *name = "Ann Lee";
int spaces = countSpaces(name);
double tax = 100 * taxRate;
//spaces is 1
//tax is 20

printf("spaces is %d\n", spaces);
printf("tax is %g\n", tax);