- 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
Monday, March 7, 2011
2.5 Arithmetic Operators
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
Monday, January 31, 2011
1.10 External Variables and Scope
- Variables (automatic) in a function are local or private to the particular function
- Each local variable in a function comes into existence only when the function is called, and disappeared when the function exited
- Automatic variables do not retain their values from one call to the next, and must be explicitly set upon each entry. If they are not set, they will contain garbage
- External variables are globally accessible, exist permanently and retain values after function returned, oppose to automatic variables
- An external variable must be defined, exactly once, outside of any function; to set aside storage for it
- The external variable must also be declared in each function that want to access it; this states the type of the variable. The declaration may be an explicit "extern" statement or may be implicit from context
- If the definition of an external variable occurs in the source file before its use in a particular function, then there is no need for an extern declaration in the function
- Common practice is to place definition of all external variables at the beginning of source files and then omit all extern declarations
- If a program is in several source files, and a variable is defined in file1 and used in file2 and file3, then the extern declarations are needed in file2 and file3 to connect the occurrences of the variable
- The usual practice is to collect extern declaration of variables and functions in a separate file (header)
Thursday, January 27, 2011
Subscribe to:
Posts (Atom)




