Fundamental statements about C

notes from "The C Programming Language" book by Brian Kernighan and Dennis Ritchie

  1. 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.

  2. An identifier is a sequence of letters and digits. The first character must be a letter; the underscore _ counts as a letter. Upper and lower case letters are different.

  3. Usual practice is to collect extern declarations of variables and functions in a separate file, historically called a header, that is included by #include at the front of each source file.

  4. The suffix .h is conventional for header names. The functions of the standard library, for example, are declared in headers like .

  5. variables and constants are the basic data objects manipulated in a program.

  6. declarations list the variables to be used, and state what type they have and perhaps what their initial values are.

  7. variables must be declared before use, although certain declarations can be made implicitly by context.

  8. expressions combine variables and constants to produce new values.

  9. type of an object determines the set of values it can have and what operations can be performed on it.

  10. conditional C practice is to use lower case for variable names, and all upper case for symbolic constants.

  11. technically, a string constant is an array of characters.

  12. 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.

  13. external and static variables are initialized to zero by default

  14. binary arithmetic operators are +, -, *, /, and the modulus operator %. Integer division truncates any fractional part.

  15. when a char is converted to an int, it can produce a negative integer. Result varies from machine to machine, reflecting the architecture.

  16. though we are not dogmatic about the matter, it does seem that goto statements should be used rarely, if at all.

  17. if the function takes arguments, declare them; if it takes no arguments, use void.

  18. it is not a block-structured language in the sense of Pascal or similar languages, because functions may not be defined within other functions.

  19. in absence of explicit initialization, external and static variables are guaranteed to be initialized to zero; automatic and register variables have undefined (i.e., garbage) initial values.

  20. for external and static variables, the initializer must be a constant expression; the initialization is done once, conceptually before the program begins execution.

  21. for automatic and register variables, it is done each time the function or block is entered.

  22. provides certain language facilities by means of a preprocessor, which is conceptually a separate first step in compilation.

  23. the two most frequently used features are #include, to include the contents of a file during compilation, and #define, to replace a token by an arbitrary sequence of characters.

  24. other features described in this section include conditional compilation and macros with arguments.

  25. #include is the preferred way to tie the declarations together for a large program. It guarantees that all the source files will be supplied with the same definitions and variable declarations, and thus eliminates a particularly nasty kind of bug.

  26. naturally, when an included file is changed, all files that depend on it must be recompiled.

  27. is also possible to define macros with arguments, so the replacement text can be different for different calls of the macro.

  28. pointer is a variable that contains the address of a variable.

  29. pointers are much used in C, partly, because they are sometimes the only way to express a computation, and partly because they usually lead to more compact and efficient code than can be obtained in other ways.

  30. the unary operator * is the indirection or dereferencing operator; when applied to a pointer, it accesses the object the pointer points to.

  31. the unary operator & gives the address of an object.

  32. when an array name is passed to a function, what is passed is the location of the initial element.

  33. within the called function, this argument is a local variable, and so an array name parameter is a pointer, that is, a variable containing an address.

  34. a structure is a collection of one or more variables, possibly of different types, grouped together under a single name for convenient handling.

  35. C provides a compile-time unary operator called sizeof that can be used to compute the size of any object.

  36. when a C program is started, the operating system environment is responsible for opening three files and providing file pointers for them.

  37. 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 .

  38. normally stdin is connected to the keyboard and stdout and stderr are connected to the screen, but stdin and stdout may be redirected to files or pipes.
  39. pointers stdin and stdout are objects of type FILE*. They are constants, however, not variables, so it is not possible to assign to them.

  40. an object is a named region of storage; an lvalue is an expression referring to an object. An obvious example of an lvalue expression is an identifier with suitable type and storage class.