- C provides certain language facilities by means of a preprocessor, which is conceptually a separate first step in compilation
- Two most frequently used features are #include and #define
- #include includes the contents of a file during compilation
- #define replace a token by an arbitrary sequence of characters.
Thursday, March 24, 2011
4.11 The C Preprocessor
Wednesday, March 23, 2011
4.10 Recursion
- A function may call itself either directly or indirectly
- Example of recursion function: printd, quicksort
4.9 Initialization
- In the absence of explicit initialization, external and static variables are initialized to zero; automatic and register variables have undefined/garbage initial values
- For external and static variable, initializer must be a constant expression; initialization is done once, before the program begins execution
- For automatic and register variable, initializer is not restricted to be a constant expression, it can be previously defined values or function calls; initialization is done each time the function or block is entered
- An array may be initialized by following its declaration with a list of initializers enclosed in braces and separated by commas. ie:
- When the size of array is omitted, compiler will compute the length by counting the initializers
- For character arrays initialization, a string may be used instead of the braces and commas notation:
Tuesday, March 22, 2011
4.7 Register Variables
- A register declaration advises the compiler that the variable in question will be heavily used
- Register variables are to be placed in machine registers which may result in smaller and faster programs. ( but compilers are free to ignore the advice)
- Register declaration can only be applied to automatic variables and to the formal parameters of a function
4.6 Static Variables
- The static declaration, applied to an external variable or function, limits the scope of that object to the rest of the source file being compiled
- If a variable or function is declared static, its name is invisible outside the file in which it is declared
- Internal static variables provide private, permanent storage within a single function
4.4 Scope Rules
- The scope of a name is the part of the program within which the name can be used
- For an automatic variable declared at the beginning of a function, the scope is the function in which the name is declared. Local variables of the same name in different functions are unrelated
- The scope of an external variable or a function lasts from the point at which it is declared to the end of the file being compiled
- It is important to distinguish between the declaration of an external variable and its definition
- A declaration announces the properties of an external variable (primarily its type) (ie. extern int a;)
- A definition also cause storage to be set aside (ie. int a;)
- Array size must be specified with the definition, but are optional with an extern declaration
- Initialization of an external variable goes only with the definition
- Example:
Monday, March 21, 2011
4.3 External Variables
- External variables and functions have the property that all references to them by the same name are references to the same thing. (external linkage)
Friday, March 18, 2011
4.2 Functions Returning Non-integers
- For functions returning non-integers
- Function must declare the type of value it returns (ie. double function ( ) )
- Calling routine must know that the function returns a non-int value
- Declarations must match definitions
- If functions take argument, declare them; if functions take no argument, use void
Thursday, March 17, 2011
Wednesday, March 16, 2011
Chapter 4 Functions and Program Structure
- Functions break large computing tasks into smaller ones, and enable people to build on existing program without starting from scratch
Tuesday, March 15, 2011
3.8 Goto and Labels
- Goto statement allows breaking out of two or more loops at once:
- A label has the same form as a variable name, and is followed by a colon
3.7 Break and Continue
- Break statement provides early exit from a loop (for, while, do and switch)
- Continue statement causes the next iteration of the enclosing for, while or do loop to begin
3.6 Loops - Do While
- Do syntax:
- Statement is executed, then the expression is evaluated. If expression is true, the cycle repeats
3.5 Loops - While and For
- While syntax:
- Expression is evaluated. If expression is true, statement is executed and expression is re-evaluated. Cycle continue until expression is zero
- for syntax:
Monday, March 14, 2011
3.4 Switch
- Switch statement is a multi-way decision that tests whether an expression matches one of a number of constant integer values, and branches accordingly
- Syntax:
- case labeled default is executed if none of the cases are satisfied
- default is optional
- Example:
- Break statement causes an immediate exit from the switch
3.1 Statements and Blocks
Chapter 3 Control Flow
- Semicolon ; is a statement terminator ( ie. x = 0; )
- Braces { } are used to group declarations and statements into a compound statement or block
Thursday, March 10, 2011
2.12 Precedence and Order of Evaluation
- C does not specified the order of operands of an operator are evaluated. (exception ?: etc)
- C does not specified the order of function arguments are evaluated
- Function calls, nested assignment statements, and increment and decrement operators cause " side effects" - some variables are changed as a by-product of the evaluation of an expression
- ie. a[i] = i++; ( subscript is the old or new value of i? Different compliers, generate different answers, depending on machine architecture)
2.11 Conditional Expressions
- Conditional expression written with ternary operator " ?:" ( expr1 ? expr2 : expr3 )
- expr1 is evaluated, if expr1 is true then expr2 is evaluated, otherwise expr3 is evaluated (ie. z = (a > b) ? a : b; /* z = max (a,b) */ )
2.10 Assignment Operators and Expressions
- Assignment Operators: += (ie. i = i + 2 can be written as i += 2)
- Most binary operators have a corresponding assignment operator op = (op: + - * / % << >> & ^ | )
- expr1 op = expr2 equal to expr1 = (expr1) op (expr2)
Wednesday, March 9, 2011
2.9 Bitwise Operators
- Six operators for bit manipulation:
- The operators can only be applied to integral operands - int, char, short and long (signed or unsigned)
- bitwise AND operator is often used to mask off some set of bits
- bitwise inclusive OR operator is used to turn bits on
- bitwise exclusive OR operator set a one in each bit position where its operands have different bits, and zero where they are the same
- Shift operator shift the bit position of operand and fill vacated bits with zero
- Unary operator yields the one's complement of an integer (convert each 1-bit into 0-bit and vice versa)
2.8 Increment and Decrement Operators
- Increment operators ++ add 1 to the operand while decrement operator -- subtract 1 from the operand
- ++ and -- can be used as prefix or postfix operators, effect might be different depending on use cases
- The expression ++n increment n before its value is used, while the expression n++ increment after its value is used
- ++ and -- can only be applied to variables
2.7 Type Conversions
- When an operator has operands of different types, they are converted to a common type according to a small number of rules
- In general, the only automatic conversions are those that convert a "narrower" operand to a "wider" operand, without loss of information ( ie. integer to floating point)
2.6 Relational and Logical Operators
- Relational operators (same precedence): >, >=, <, <=
- Equality operators (lower precedence than relational): = =, ! =
- Relational operators have lower precedence than arithmetic
- Logical operators (evaluated left to right): && , | |
- Precedence of && is higher than ||
- Logical operators' precedence is lower than equality and relational
- Unary negation operator ! convert a non-zero operand into 0 and a zero operand into 1 (ie. !valid)
Monday, March 7, 2011
2.5 Arithmetic Operators
- Binary arithmetic operators are: +, -, *, /
- Integer division truncates any fractional part
- Modulus operator: %
- x % y produce the remainder when x is divided by y (zero when y divides x exactly)
- The % modulus operator cannot be applied to float or double
- The direction of truncation for / and the sign of the result for % are machine-dependent for negative operands
- Precedence (lowest first) : + -, * / %, unary + -
- Arithmetic operators associate left to right
Friday, March 4, 2011
2.4 Declarations
- All variables must be declared before use. (certain declarations can be made implicitly by context)
- A declaration specifies a type and a list of one or more variables associated with the type
- A variable may also be initialized in its declaration
- An explicitly initialized automatic variable is initialized each time its function/block is entered
- External and static variables are initialized to zero by default
- Automatic variables with no explicit initializer has undefined (ie. garbage) value
- The qualifier const can be applied to the declaration of any variable to specify that its value will not be changed (ie. const double e = 2.718)
Thursday, March 3, 2011
2.3 Constants
- A long constant is written with a terminal l or L (ie. 123456789L)
- An unsigned constant is written with a terminal u or U
- The suffix ul or UL indicates unsigned long
- Floating-point constants contain a decimal point (123.4) or an exponent (1e-2) (double type unless suffixed)
- The suffix f or F indicates floating constant
- A leading 0 on an integer constant means octal
- A leading 0x on an integer constant means hexadecimal
- A character constant is an integer written as one character within single quotes (ie. 'x')
- The value of character constant is the numeric value of the characters in the machine's character set (ie '0' has the value 48 in ASCII character set
- Certain characters can be represented in character and string constants by escape sequences like \n newline
- Complete set of escape sequence:
- Character constant ' \0 ' represents the character with value zero - the null character
- A string constant/literal is a sequence of zero or more characters surrounded by double quotes. ( ie. " Hello World" )
- The internal representation of a string has a null character ' \0 ' at the end, so the physical storage required is one more than the written characters length
- An enumeration is a list of constant integer values
- The first name in an enum has value 0, the next 1, and so on, unless explicit values are specified
- If not all values are specified, unspecified value continue progression from the last value
- Enumeration associate constant value with name, an alternative to #define with auto-value generation advantage
Tuesday, March 1, 2011
2.2 Data Types and Sizes
- Basic data types:
- short and long (qualifiers) apply to integer data type
- int will normally be the natural size of a particular machine
- short is often 16 bit; long is 32 bit, integer can be 16 or 32 bits
- signed and unsigned (qualifiers) apply to char and int
- Unsigned numbers are always positive or zero and obey the laws of arithmetic modulo 2^n, where n is the number of bits in the type
- long double specifies extended-precision floating points
- The standard headers <limits.h> and <float.h> contain symbolic constants for all of these sizes
Chp 2 Types, Operators, and Expressions
- Variables and constants are basic data objects manipulated in a program.
- Declarations list the variables to be used, and state what types and initial values if any
- Operators specify what is to be done to the data objects
- The type of an object determine its set of values and what operations to be performed
- 2.1 Variable Names
- Names are made up of letters and digits; first character must be a letter
- Underscore "_" counts as a letter
- Upper and lower case letters are distinct
- Traditional C practice is to use lower case for variable name and all upper case for symbolic constants
Subscribe to:
Posts (Atom)