<The C Programming Language> study note

Introduction

A C program, whatever its size, consists of functions and variables.

A C program begins its execution at the main function. This means that every C program must have a main somewhere.
main will usually call other functions to help perform its job.

Variables

In C, all variables must be declared before they are used, usually at the beginning of the function before any executable statements.
A declaration consists of a type name and a list of variables, such as

1
2
int fahr, celsius;
int lower, upper, step;

The type int means that the variables listed are integers.

for vs. while

The for is usually appropriate for loops in which the initialization and increment are single statements and logically related, since it is more compact than while and it keeps the loop control statements together in one place.

Character Input and Output

The model of input and output supported by the standard library is very simple. Text input or output, regardless of where it originates or where it goes to, is dealt with as streams of characters. A text stream is a sequence of characters divided into lines; each line consists of zero or more characters followed by a newline character.

A character written between single quotes represents an interger value equal to the numerical value of the character in the machine’s character set.
So chars, which are just small integers, may be freely used in arithmetic expressions.

Arrays

The declaration

1
int ndigit[10];

declares ndigit to be an array of 10 integers.

Functions

A function provides a convenient way to emcapsulate some computations, which can then be treated as black-box abstraction and used without worrying about its implementation.

We will generally use parameter for a variable named in the parenthesized list in a function definition, and argument for the value used in a call of the function.

Function Arguments

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. The called function cannot directly alter a variable in the calling function; it can only alter its private, temporary copy. One good thing about this mechanism is that parameters can be treated as conveniently initialized local variables in the called routine.

However, 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 address of the beginning of the array. By subscripting this value, the function can access and alter any element of the array.

External Variables

An external variable must be defined, exactly once, outside of any function; this sets aside storage for it. The variable must also be declared in each function that wants to access it.

Definitionrefers to the place where the variable is created or assigned storage;declarationrefers to places where the nature of the variable is stated but no storage is allocated.

Data Types and Sizes

The qualifier signed or unsigned may be applied to char or any integer. unsigned numbers are always non-negative, and obey the laws of arithmetic modulo 2n, where n is the number of bits in the type. So, for instance, if chars are 8 bits, unsigned char variables have values between 0 and 255, while signed chars have values between -128 and 127 (in a two’s complement machine).

Printable characters are always positive.

Constants

An integer constant like 1234 is an int. A long constant is written with a terminal l or L, as in 123456789L; an integer too bit to fit into an int will also be taken as a long. Unsigned constants are written with a terminal u or U, and the suffix ul or UL indicates unsigned long.

Floating-point constants contain a decimal point (123.4) or an exponent (1e-2) or both; their type is double, unless suffixed. The suffixes f or F indicate a float constant; l or L indicate a long double.

The character constant ‘\0’ represents the character with numerical value zero, the null character.

A string constant, or string literal, is a sequence of zero or more characters surrounded by double quotes, as in

1
2
3
	"I am a string"
or
"" /* the empty string */

String constants can be concatenated at compiled time:

1
"hello," " world"

Is equivalent to

1
"hello, world"

Technically, a string constant is an array of characters.

An enumeration is a list of constant integer values. Enumerations provide a convenient way to associate constant values with names (like dict in Python), an alternative to #define with the advantage that the values can be generated for you.

Declarations

External and static variables are initialized to zero by default.
Automatic variables for which there is no explicit initializer have undefined (i.e., garbage) values.

The qualifier const can be applied to the declaration of any variable to specify that its value will not be changed. For an array, the const qualifier says that the elements will not be altered.

The const declaration can also be used with array arguments, to indicate that the function does not change that array:

1
int strlen(const char[]);