You are on page 1of 5

Dr.S.J.M.

Yasin/CE206_lecture_03_basics/ 28 March 2009/P-1

Dr.S.J.M.Yasin/CE206_lecture_03_basics/ 28 March 2009/P-2

Size of different data types

The number of bites used for some data types vary from compiler to compiler. However we can get this information by using the sizeof() operator. #include<iostream> using namespace std; void main() { // Program - P03 cout<<"Size (number of bytes) of different data types\n\n"; cout<<"Size of \'int \'= "<<sizeof(int)<<endl; cout<<"Size of \'double =\'= "<<sizeof(double)<<endl; cout<<"Size of \'short \' = "<<sizeof(short)<<endl; cout<<"Size of \'long \' = "<<sizeof(long)<<endl; cout<<"Size of \'float \' = "<<sizeof(float)<<endl; cout<<"Size of \'char \' = "<<sizeof(char)<<endl; cout<<"Size of \'long double \' = "<<sizeof(long double)<<endl; float weight=25.0; int serial_no=5; cout<<"Size of \'weight \' = "<<sizeof weight<<endl; cout<<"Size of \'serial_no \' = "<<sizeof(serial_no)<<endl; }

Note sizeof( ) is a unary operator so it takes a single operand it returns an integer value that represents the number of bytes occupied by a type or by a variable to find the size of a type you have to put the type name inside the parenthesis to find the size of a variable you may put the variable name without parenthesis you can apply sizeof operator even to an expression and the expression does not necessarily have to be within parenthesis Finding the limits We can find out the maximum value that can be stored in a variable of a given type using the expression numeric_limits<type_name>::max() To find the minimum value numeric_limits<type_name>::min() The standard header file limits is required for the above. Escape sequence We can enter control characters and certain other characters by means of escape sequence. ............................................................................. #include<iostream> using namespace std; void main() { // Test program cout<<Trial Program; } ........................................................................... When we run the above program the following appear in the output window on the monitor Trial ProgramPress any key to continue

Dr.S.J.M.Yasin/CE206_lecture_03_basics/ 28 March 2009/P-3

Dr.S.J.M.Yasin/CE206_lecture_03_basics/ 28 March 2009/P-4

Notice the effect of escape sequences on output of the following program Now if we modify the program as follows then ........................................................ #include<iostream> using namespace std; void main() { // Test program cout<<Trial Program\n; //this line is changed } .............................................................. Trial Program Press any key to continue //Program P05 #include<iostream> using namespace std; void main() { // Test program cout<<"Trial Program\nI wrote it\n\tIt is a simple program"; cout<<"\n\"Program showing the effect of escape sequence\"\n"; } Output of the above program is

As you can see due to the placement of \n the message from operating system is now printed on the next line. Thus we can control the appearance of string output or message by using escape sequence.

Note /n has the same effect as endl (we used it in our first program) m number of /n produce m-1 blank lines Arithmetic operators Operator Operation + Addition Subtraction * Multiplication / Division % Modulus (the remainder after division) Note: 5/2 -> 2, 5.0/2 -> 2.5, 5./2 -> 2.5, 5/2.0 -> 2.5 5%2 -> 1, 10%4 -> 2, 15%3 -> 0 For division if the two operands are integer (number or variable) then the expression returns only the integer part of the actual result. The operands of % must be integer

Escape Sequence \n \t \v \b \r \f

Control Character newline horizontal tab vertical tab backspace carriage return form feed

Escape Sequence \a \\ \ \ \?

Control Character alert-bell backslash single quote double quote question mark

Dr.S.J.M.Yasin/CE206_lecture_03_basics/ 28 March 2009/P-5

Dr.S.J.M.Yasin/CE206_lecture_03_basics/ 28 March 2009/P-6

precedence and associativity of arithmetic operators Suppose we have written an expression 4 * 5 / 3 % 4 +7 / 3 Which operator will be executed first? In an expression involving several different operators, the order in which the operators are executed is determined by their precedence rule. * / % are of higher precedence than + and * / % are of equal precedence + and are of equal precedence However we can overrule the above precedence by use of parenthesis. When there are consecutive operators of equal precedence (such as in the expression 4 * 10 / 6 % 4 ) then the sequence in which these will be executed is determined by their associativity. * / % are left associative and these will be executed from left to right. + and are also left associative An example to show the importance of associativity 20/4/2 ==> 5 /2 =2 However if 4/2 is evaluated first then the result would have been 20/2 =10 // Program - P07 #include<iostream> using namespace std; void main( ) { cout<<endl<<endl<<" 20/4/2 = "<<20/4/2; cout<<endl<<endl<<" (20/4)/2 = "<<(20/4)/2; cout<<endl<<endl<<" 20/(4/2) = "<<20/(4/2) <<endl; } ==> ==> ==> ==>

The expression 4 * 10 / 6 % 4 will be evaluated as 4 * 10 / 6 % 4 ((4 * 10) / 6) % 4 (40 / 6) % 4 6%4 2

<- notice integer division here

If the above expression were right associative then it would have been evaluated as 4 * 10 / 6 % 4 4 * (10 / (6 % 4)) 4 * (10 / 2) 4*5 20

==> ==> ==> ==>

Additional examples 10 + 20 / 10 5 ==> 10 + 2 5 ==> 12 5 ==> 7 (10 +20) / 10 5 ==> 30 / 10 5 ==> 3-5 ==> -2

However, in an expression such as 4 * 5 / 3 % 4 + 7 / 3 we cannot say whether the sub-expression 4 * 5 / 3 % 4 will be evaluated first or 7 / 3 will be evaluated first. It depends on the compiler and we dont need to know it because the result is same for both the cases.

Dr.S.J.M.Yasin/CE206_lecture_03_basics/ 28 March 2009/P-7

Dr.S.J.M.Yasin/CE206_lecture_03_basics/ 28 March 2009/P-8

Assignment operators ( = += -= *= /= ) // Program - P06 #include<iostream> using namespace std; void main() { cout<<endl<<" 4*10/6%4 ="<<4*10/6%4; cout<<endl<<" 4 * 10 = "<<4*10; cout<<endl<<" 40 / 6 = "<<40/6; cout<<endl<<" 6 % 4 = "<<6%4; cout<<endl<<" ((4*10)/6)%4 ="<<((4*10)/6)%4; cout<<endl<<" If * / % were right associative then" <<endl<<" the following would have occurred"; cout<<endl<<" 6 % 4 ="<<6%4; cout<<endl<<" 10 / 2 ="<<10/2; cout<<endl<<" 4 * 5 ="<<4*5; cout<<endl<<" 4*(10/(6%4)) ="<<4*(10/(6%4)) <<endl; } int age; ............ age = 25; 25 is stored in the integer memory location named age.

We cannot write statements like 25 = age; The left side of the = operator must be a single variable If we write a = 5.2; b = a + 4; //the right side is first evaluated and then the result is stored in the variable to the left Suppose you wrote int weight; ....... weight = 25.5; cout<<weight; The output will be 25 not 25.5 because weight is declared as int type

We can write statements as below : length = width = 5; apples = oranges = 50; a = b = c / 2.2; Remember the = operator is right associative. Therefore in the first one of the above examples 5 is first stored in width and then the value in width is stored in length. So it is effectively length = (width = 5); It implies that the expression width = 5 has value namely the value stored in oranges. In fact we can write statements like fruits = ( oranges = 10) + ( apples = 11) ; This will store 10 in oranges, 11 in apples, then add the two together and store the result in fruits.

Dr.S.J.M.Yasin/CE206_lecture_03_basics/ 28 March 2009/P-9

Dr.S.J.M.Yasin/CE206_lecture_03_basics/ 28 March 2009/P-10

Suppose part of a program code is as follows int a; ................... a = 2; ............. memory is allocated for integer variable named a 2 is stored in the memory location named a Some commonly used library functions (header file : math.h / cmath.h) Function Type Purpose abs(i) int Return the absolute value of i acos(d) double Return the arc cosine of d asin(d) double Return the arc sine of d atan(d) double Return the arc tangent of d cos(d) double Return the cosine of d sin(d) double Return the sine of d cosh(d) double Return the hyperbolic cosine of d sinh(d) double Return the hyperbolic sine of d tanh(d) double Return the hyperbolic tangent of d exp(d) log(d) log10(d) double double double Raise e to the power d (e=2.781818.. Naperian base) Return the natural logarithm (base e) of d Return the logarithm (base 10) of d Return the remainder of d1/d2 with same sign as d1 Return the square root of d Return d1 raised to the power d2

Suppose part of a program is as below double length; length = 5.5; length = 7.0; cout << length; You will get 7.0 as output. a = a+2; //this statement will first evaluate a+2 and then store the value back into variable a (it is implied that the variable a must have been assigned a value before this statement) instead of the above statement we can write a += 2; // this statement has the same effect as the statement above a *= b+5; is same as a =a * (b+5); length /= 5; is same as length = length /5;

fmod(d1,d2) double sqrt(d) pow(d1,d2) double double

Function abs(i)

(header file : stdlib.h) Type Purpose int Return the absolute value of i