- 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)
Wednesday, March 9, 2011
2.7 Type Conversions
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:
Comments (Atom)


