SP_02_Lec_engl_01_noeff.ppt
- Количество слайдов: 31
The data type conception. Variables and constants. Assignment operation l Any program, intended for the realization on the COMPUTER, represents the formalized description of any task solution algorithm. The actions which are carried out by the program according to this algorithm are directed on the transformation of some objects which determine the current condition of the task solution process. Such internal objects of the program are called the data. For the storage of any data element the observant system allocates necessary space in the operative computer memory, the size of which we’ll call the length of this element. The main characteristic of any data element is its type.
The concept of the type includes the following information about the data element: l an allowable set of values which the object of this type can accept during the work of the program (the set of all specified values we’ll call the range of the type definition); l structure of operations which are allowed to be executed on the objects of the given type; l a way to represent the data element of the considered type in the machine memory; l the rules of any operation performance from the set of allowable for this type operations.
Language С provides an opportunity of representation and data processing of the following basic types: l integers of various length with the sign and without it; l real numbers of various length; l symbols, representable in a standard format ASCII.
Those elements of the data that keep the constant values during all operating time of the program are called constants. Other objects that are changing during the algorithm performance are called variables. From the point of view of the language С, any variable is identified with its name, or the identifier. From the position of the COMPUTER it is considered to be the changing in the time contents of some operative memory’s area.
The instruction of the programming language that allows to assign and to change the values of variables is called the operation of assignment. For its designation in С the symbol "=" is used. For example: a = 2; b = 3 * (a+4); l After performance of the specified actions the variable a receives the value equal to 2, and variable b - 18.
Types of the data in language C. The description of the data in the program As it was already spoken in lecture 1, the descriptions in the language С are the lines of the program defining the names and the characteristics of the data elements which participate in the work of the algorithm. l In the simplest case the instruction of the data description in the С-program has the following format: l <sc-specifier> type-specifier identifier <, identifier. . . >;
The description of the data in the program <sc-specifier> type-specifier identifier <, identifier. . . >; where sc-specifier is the descriptor of the memory class; l type-specifier – the type descriptor; identifier – the name (identifier) of the variable. l
Types of the data in language C l For the majority of compilers from the language С the descriptors of the type specified in the following table are allowable. Here in parentheses the length of the data element of each type is specified in bytes (1 byte = 8 bits) and the area of allowable values for personal computer IBM PC AT.
l The remark. Key words “unsigned”, “short” and “long” can be used as modifiers of the data basic types at the construction of the derivatives. For example, the following descriptions are considered by the compiler as equivalent: short int. . . . short long int. . . . . long unsigned int. . . . unsigned short int. . unsigned short unsigned long int. . unsigned long signed char. . . . char signed int. . . . int signed short. . . . short signed long. . . . long
Let's consider some examples of the data description in the program. int a, b, c; /* Variables a, b, c are declared */ /* to have the int type */ float alpha, beta; /* Variables alpha and beta are declared */ /* to have the float type*/
As the internal machine data presentations of the same type for various COMPUTERS may be different than the certain difficulties at providing the appropriate software mobility arise. l To overcome the arising difficulties it is possible to use, where it is necessary, the operation of the definition of the memory quantity required for some variable or type representation. Generally this operation looks as follows: l sizeof (name) where “name” is either the identifier of a variable, or the data type name.
Elements of language C Operators l The elementary operator of language С is any expression, coming to an end a semicolon. In particular, separately worth semicolon is interpreted by the compiler as the zero - operator. The most significant group of operators is formed with instructions (operators) of management of process of performance of the program. To them concern:
Arithmetic operations and arithmetic expressions l In the first paragraph it has been already considered one of the major operations of the language С - operation of the assignment, which allows to assign and change values of variables. Here we’ll consider a group of arithmetic operations and we’ll define the concept of arithmetic expression. The traditional way of setting the arithmetic operations is the use of two-place arithmetic operators of multiplicate and additive groups. Operands of any two-place operation can be both constants, and variables, and names of the latter should be predefined in one of the instructions of the type description.
Arithmetic operations and arithmetic expressions l Operators of multiplicate group serve for representation of operations of multiplication (*), divisions (/) operands and reception of the remainder (%) from the division of the first operand by the second. In the latter case both operands should be the whole values. Operation of multiplication is possible on the operands of any type (may be various!). The operation of division applied to two integer operands, can lead to the loss of the result’s fractional part. The attempt of division by zero gives the mistake at the stage of the compilation or program execution. The order of the multiplicate group operation performance is from left to right. The group of additive operators includes two operators: additions (+) and subtraction (-). Both these operations are feasible on the operands of any type (may be various!). The order of the additive group operations performance is from left to right. Their priority is lower than the multiplicate operations priority, but higher, than the assignment operation priority.
Let’s show some simple examples of arithmetic expressions: l l l a = 4. 3 + 2. 7 In this example both operands are to the right of the assignment operator and are numerical constants. Arithmetic expressions of a similar kind are called the constant expressions. 2. b = (c + d) % 4 Here the traditional order of performance of arithmetic operations is broken by use of the parentheses allocating subexpression in structure of more complex arithmetic expression. According to definition of operation of % operands c and d should be variables of the whole type. 3. e = ++ f / (g + h) In the given example the form of the operator ++ is used prefix and consequently operation of division will be executed after actual increase in value of a variable f on unit.
In language С operation of giving is a full part of any arithmetic expression and generally has a format expression 1 = expression 2 However, not pressing while in semantic details of such record, we shall use its simplified form identifier = expression where identifier there is a predefined name of a variable, and expression - any arithmetic expression. But even in such simplified circuit are stacked difficult enough in the semantic relation of expression of a kind p = 2 * (q + s) / (t = u * ++ v)
Operations of the relation, logic operations and logic expressions The group of two-place operations of the relation) carries out comparison of the first operand with the second, checking, whether that parity between operands which is determined by a symbol of operation is true. The full set of operations of the relation is set by the following group of operators: <-it is less <= - it is less than or equal == - it is equal > - it is more >= -more or equally ! = - it is not equal
l l Result of any operation of the relation is numerical value such as int, equal to unit if comparison is true, and to zero otherwise. Thus, operations of the relation in internal machine representation result in arithmetic result. Strictly speaking, logic value "true" should answer any numerical value which is distinct from zero. Further we shall see, what exactly such agreement is accepted in language С. It enables to unit concepts of arithmetic, conditional and logic expressions in uniform concept "expression" that is very important from the point of view of flexibility and "symmetry" of language. The expressions designed by means of operations of the relation, it is accepted to name conditional expressions.
During calculation of value of any conditional expression, operation of the relation are processed from left to right. The established order can be changed by the conclusion of a part of expression in parentheses. For example, record x <y == z it is completely equivalent to record (x <y) == z but expression of a kind x <(y == z) differs from previous the order of performance of operations <and ==.
Logic operations By development of real programs frequently it appears necessary to unit two or more conditional expressions. It can be made, using a set of two-place logic operations: l && - logic And l || - logic OR l ! - logic NOT (denying)
Let's assume, that exp 1 and exp 2 - two simple conditional expressions. Then: 1. value expressions exp 1 and exp 2 are true; 2. Value exp 1 || exp 2 is true, if even one of expressions - operands matters "true"; 3. value! exp 1 it is true, if expression exp 1 is false, and vice versa. The expressions constructed with use of logic operations, we shall name logic expressions. We shall notice, that logic expressions are direct generalization of simple conditional expressions.
l The standard order of their processing - from left to right. The priority of logic operations && and || is lower than a priority of any operation of the relation and consequently logic expressions: a <b && b <c and (a <b) && (b <c) are completely equivalent, though second of them is more preferable because of presentation inherent in it. However operation of logic denying (!) possesses very high priority (it is the same as priority of arithmetic operations) and only parentheses have a higher priority.
Generally operands of logic operations can be not only conditional expressions, but also any arithmetic expressions. It is easy for understanding, if to zero value of arithmetic expression to put in conformity logic value “false" and, on the contrary, any numerical value distinct from zero to identify with logic value "true". The elementary instruction of language С using logic expressions, the conditional operator is: expression 1? expression 2: expression 3 where expression 1 there is a logic expression, and expression 2 and expression 3 essence any arithmetic expressions. If expression 1 accepts value "true" result of conditional operation will be value expression 2, otherwise it is equal to value expression 3.
For example, the instruction: abs_a = (a> 0)? a: -a gives a variable abs_a absolute value of a variable a.
Automatic transformation of the types and operation of reduction If the structure of arithmetic or conditional expression includes operands of various types the compiler automatically carries out their reduction the common type. In spite of the fact that in some cases character of transformation depends on a kind of concrete operation and such as operands, there is a common set of standard rules of transformation:
l 1. If operation is carried out above the data of two various types, both sizes are resulted in the "maximum" type; l 2. In the operator of giving the end result of calculation of expression in the right part is resulted in type of a variable by which value should be given (left type). The sequence of names of the types ordered from the "maximum" type to "lowest", looks as follows: double, float, long, int, short and char. Application of a key word unsigned raises a rank of corresponding type of the data with is familiar. l
This opportunity is realized in operation of reduction of types as follows: Before the given size in parentheses the name of required type enters the name. Let, for example, the variable res has type int. Then value of arithmetic expression res = 2. 7 + 1. 5 according to the general rules of transformation of types, it is equal 4.
l However having applied obvious image operation of reduction of type to both operands in the right part res = (int) 2. 7 + (int) 1. 5 let's receive the result equal 3. This example speaks about necessity with extra care to concern to every possible transformations of types of operands.
The elementary operators of language C. The compound operator The arithmetic above arithmetic, conditional and logic expressions in aggregate form objects of the program which can be named simply expressions. This terminology emphasizes equality of operations of various groups from the point of view of their role in formation of expressions. Moreover, to any expression in С there meets some numerical value (behind exception, certainly, those cases when the result of operation is considered uncertain) which can be used further. However in themselves expressions of any kind yet are not the finished instructions of language С.
The elementary operators of language C. The compound operator For a designation of the completed actions the concept of the operator is used. The elementary operator of language is any expression, coming to an end a semicolon (; ). In particular, separately worth semicolon is interpreted by the compiler as the zero operator. For example, instructions of a kind a = c * (d + e)/4; f = alpha ++; being operators - expressions, play a role of the finished instructions for observant systems.
The elementary operators of language C. The compound operator The group of one or greater number of the operators made in braces, forms the compound operator. We shall notice, that by rules of language the semicolon after a closing brace is not put. For example, group of three operators {x = 7; y = x + 10; z = (x + y)/13; } it is considered by the compiler as one compound operator. In the program in language С compound operators can be used everywhere level with simple operators.
SP_02_Lec_engl_01_noeff.ppt