<<

ProgrammingProgramming inin ++C++ 5.5. IntegralIntegral datadata typestypes

! Introduction ! Type int ! Integer & ! Increment & decrement operators ! Associativity & precedence of operators ! Some common operators ! Long & short integers ! Unsigned integers ! Character types ! Summary

1 Introduction to programming in C++ for engineers: 5. Integral data types IntroductionIntroduction

C++ is a typed language - the variables and the constants that occur in a program each have a type which controls how they may be used and what storage is required. The language-defined types which are defined to represent integers, characters and floating point numbers are known as fundamental types. It is also possible to define what are known as derived types in order to manipulate objects such as matrices, complex numbers etc. The motivation for using a typed language is that the compiler can catch many programming errors by performing type-checking. For example if an object is declared to be constant (by using const keyword) than an attempt to change its value is a compile-time error. The integral data types consist of two groups: the integer and character types.

2 Introduction to programming in C++ for engineers: 5. Integral data types TypeType intint The int is the most important of the integral data types and can hold both positive and negative integers. If we wish to use variables k, l, m they can be defined by: int k, l, m; Defining an object not only specifies the type but also reserves the appropriate amount of memory. We can also make an declaration e.g. extern int k; without reserving any memory. In a complete program there must be one and only one definition of an object even though there can be many declarations. An object must be defined or declared before it can be used in a program, otherwise a compile-time error occurs. The operator = is used for assignment. This is rather assignment than equality operator!

3 Introduction to programming in C++ for engineers: 5. Integral data types TypeType intint k=m+n;//sumofmandnisassignedtok k + 3 = 7; // WRONG!!! even looks mathematically OK k = k + l; // in mathematics OK only if l=0 k := 10; // := does not exist in C++ Since the int type occupies a fixed amount of memory only a limited amount of integers can be stored. Some operations can give results above the maximum amount or below the minimum value. Such invalid operations are called integer overflow or integer underflow respectively. INT_MAX and INT_MIN are the maximum and minimum values that can be stored by the int type. They are defined in the header file . These values depend on the particular compiler.

4 Introduction to programming in C++ for engineers: 5. Integral data types IntegerInteger multiplicationmultiplication && divisiondivision Integer multiplication is denoted by the token *.

Integer division is denoted by forward slash /. Integer division with both numbers positive always truncates! The same is true when both numbers are negative. If only one number is negative the result depends on compiler.

Integer modulus or remainder is denoted by % token. If k and l are both positive then k%lgives the remainder obtained on dividing k by l. If both k and l are negative the result is negative. If only one value is negative the result is dependent on the C++ compiler. k=(k/l)*l+k%l

5 Introduction to programming in C++ for engineers: 5. Integral data types IncrementIncrement && decrementdecrement operatorsoperators The value of a variable can be changed without using = operator. k=k+1; ++k; // prefix increment operator k++; // postfix increment operator The prefix operator increments k by one before it is used. The postfix operator increments after the value of k has been used. In many cases the result depends on pre or postfix character of ++. int i, j, k, l, m, n; i =2; j=2; k=3; m=i++/k; //assigns0tom n=++j/k; //assigns1ton There are also prefix and postfix decrement operators. 10++; // WRONG!!! constant cannot be incremented --3.14 // WRONG!!! and incremented too

6 Introduction to programming in C++ for engineers: 5. Integral data types AssociativityAssociativity && precedenceprecedence ofof operatorsoperators We have learnt how to use quite a few operators: =+-*/%++-- The =, *, /, % tokens are binary operators. It means that they are defined for an operand on each side of the operator. The ++, -- are both unary operators. They require a single operand. The - operator can be both a binary or an unary operator: k=i-j; k=-j; Operator precedence controls which operator in an expression is applied first. The associativity determines the order in which operators with the same precedence are applied. The compiler first uses operator precedence to determine the order of evaluation in an expression and any remaining ambiguities are removed by using operator associativity.

7 Introduction to programming in C++ for engineers: 5. Integral data types SomeSome commoncommon operators Operator Name Associativity ++ increment right to left -- decrement right to left - unary minus right to left ______* multiplication left to right / division left to right % modulus left to right ______+ left to right - left to right ______= assignment right to left += -= *= /= %=

8 Introduction to programming in C++ for engineers: 5. Integral data types LongLong && shortshort integersintegers Long integers have the same properties as integers - typically four bytes (the minimum acceptable for the ANSI and ISO C standard). It is rarely common for a compiler to define both int and long data types to have the same four byte representation. long int m; long n; The suffix L or l is used to distinguish a long constant. long k = 7L; Short integer type has the same properties as int except that a particular C++ compiler should not use for it more bytes than are used for int. This type can be useful for saving memory, but if the representation is less than the natural size suggested by the hardware there may be a performance penalty. Keyword short is used to define short integers. short int m; short n;

9 Introduction to programming in C++ for engineers: 5. Integral data types UnsignedUnsigned integersintegers The short, int and long types all have corresponding unsigned types. They occupy the same amount of memory as the signed type and they obey the laws of arithmetic modulo 2n. unsigned addition does not overflow and subtraction does not underflow, they simply wrap around. unsigned int k; // suppose represented by 2 bytes k = 65535; // max. unsigned number for 2 bytes k=k+1; //theresultis0!!! k=1-2; //theresultis65535 It is not usually worth using an unsigned rather than signed integral type just to gain a higher upper limit on the positive integers that can be stored. A U or u is used in any combination with L or l to denote an unsigned constant. unsigned int k = 1U; unsigned long m = 2UL;

10 Introduction to programming in C++ for engineers: 5. Integral data types CharacterCharacter typestypes The type char is represented by a sufficient number of bytes to hold any character belonging to the character set for the particular compiler. This representation is usually one byte (the minimum allowed by ANSI C standard). C++ distinguishes three character types: char unsigned char signed char The signed char and unsigned char data types obey the same rules of arithmetic as their integer counterparts. If the type is simply specified as char then it is compiler dependent as to whether or not the higher bit is treated as a sign bit. The char types are usually used to hold characters. char c1, c2; c1 = ‘A’; c2 = 44;

11 Introduction to programming in C++ for engineers: 5. Integral data types SummarySummary ! The integral types are short, int, long ! The character types are char, signed char, unsigned char ! Do not forget about basic integral operators together with their associativity and precedence.

12 Introduction to programming in C++ for engineers: 5. Integral data types