CSc 352: Lecture-4
Fundamental program structures, data types, operators in C
Note: All the programs mentioned in this lecture are in:
/home/cs352/SUMMER02/lecture4progs/
DECLARATIONS:
- tell the compiler to reserve enough memory
- enable the compiler to perform specified
operations correctly(e.g. + has different implementations at
the machine level for integers and floats.)
EXPRESSIONS:
- any meaningful combination of variables, constants,
operators and function calls.
- most have a value, and a type
- Ex: a+b, i=7(assignment expr.)
expression1<expression2 (OR >, >=, <=,...)
expression1 == expression2 (OR !=)
- Expression with logical operations
expression1 && expression2 (OR ||)
- With the relational expressions the value is 1 or 0.
- 1<i<5 is an expression, its value=? when i=7, i.e.;
- NOTE: The equality expr. and assignment expr. similar
will have different effects, second if will always
evaluate to true.
- Lazy evaluation with the expressions with logical
operations &&, ||
Evaluation stops as soon as the outcome true or
false is known, i.e. in expr1 && expr2, first expr1 is evaluated,
if it's false expr2 is not evaluated at all.
- Assignment with unary operators ++ and --:
i=++j; and i=j++; are different
The first one : first increment j then assign the
new value to i.
The second one :first assign the value of j to i,
then increment j.
OPERATOR PRECEDENCE RULES:
- 2- unary operators(unary -, unary +, --, ++, negation !)
- 5- relational (<, >, <=, >=)
- 9- assignment statements(=, +=, -+, *=, /=, ...)
The order indicates the precedence order, i.e., number 1 has highest
precedence and number 9 has the lowest. Except for 2 and 9 associativity is from left to right
FUNDAMENTAL DATA TYPES:
- char to define a character
- Character constants: 'a'(e.g.correspons to 97)
'b'(corr. 98), ... 'A'(corr. 65), 'B'(corr. 66)...
'0'(corr. 48), '1'(corr. 49) ...
'\t'(tab, corr. 9), '\0'(null, corr. 0),
'\n'(newline, corr.10), '\\'(backslash), '\"'(double quote)
- also signed char(-128 to 127) and unsigned char(0 to 256)
- Reading/Writing characters
macros getchar() and putchar() (defined in stdio.h)
- can think of chars as small integers, and integers
as large chars.
- typically 2 or 4 bytes(see integer.c)
- Also short, long and unsigned to define
- float, double or long double to define
- scientific notation: 3.42e-3
- usually float is 4, double is 8 bytes.(see sizes.c)
- float precision almost 6 decimal digits
- double precision almost 15 decimal digits.
- sizeof operator(a macro defined in stdio.h):
- sizeof(object) gives the size of the object(can be a
type or expression...)
- sqrt(), pow(), exp(), sin(), ...
- include the library math.h, and compile with -lm option
- Check Appendix-B of the textbookon info about specific .h files provided by the standard library.
- Conversion and type-casting
- Expr. x+y has not only a value but also a type
- If both x and y the same type then it also has that type
- Automatic Conversion rules:
1- Any char or short is converted to int
2- Then automatic conversion follows the rule:
int<long<unsigned<unsigned long<float<double<long double
- Ex: c+s/i is integer, 10+s+c+l is long, ...
- Explicit Conversion(type-casting)
(double)expression makes the type of expression double
- Ex: (double) x casts the value to be of type double
- NOTE: Automatic or explicit conversions the actual value of the expression(or its representation in memory) is unchanged, only a copy that has the new type is created. Ex: If i is an integer (double)i has type double but i still has type int.
- No boolean type. We can imitate using define:
- (NOTE: In addition to fundamental types, we'll see other very useful types: array, struct, pointer, etc...For now we just need to know a couple of things about arrays only. We define an array called myarray that will hold 10 integers in it, as follows:
Note that 'int' can be replaced with any of the fundamental types to get arrays with elemnts of that type. In C, there's no string type. A string is an array of
characters. The folowing is string 'mystr' is a string of length 10: