c program Arithmetic Instruction

This part of lesson deals with c language programming instructions and its first part is published here.Here is the continuation for that.

A arithmetic statement could be of three types. These are as follows:

1 . Integer mode arithmetic statement This is an arithmetic statement in which all operands are either integer variables or integer constants.

Ex: int i, king, issac, noteit ;
i = i + 1 ;
king = issac * 234 + noteit - 7689 ;

2 . Real mode arithmetic statement This is an arithmetic statement in which all operands are either real constants or real variables.

Ex: float qbee, antink, si, prin, anoy, roi ;
qbee = antink + 23.123 / 4.5 * 0.3442 ;
si = prin * anoy * roi / 100.0 ;

3 . Mixed mode arithmetic statement This is an arithmetic statement in which some of the operands are integers and some of the operands are real.

Ex: float si, prin, anoy, roi, avg ;
int a, b, c, num ;
si = prin * anoy * roi / 100.0 ;
avg = ( a + b + c + num ) / 4 ;

Execution of an arithmetic statement : Firstly, the right hand side is evaluated using constants and the numerical values stored in the variable names. This value is then assigned to the variable on the left-hand side.

Note :

1 . C allows only one variable on left-hand side of =. That is, z = k * l is legal, whereas k * l = z is illegal.

2 . In addition to the division operator C also provides a modular division operator. This operator returns the remainder on dividing one integer with another. Thus the expression 10 / 2 yields 5, whereas, 10 % 2 yields 0. Note that the modulus operator (%) cannot be applied on a float. Also note that on using % the sign of the remainder is always same as the sign of the numerator. Thus –5 % 2 yields –1, whereas, 5 % -2 yields 1.

3 . An arithmetic instruction is often used for storing character constants in character variables.

char a, b, d ; a = 'F' ;
b = 'G' ; d = '+' ;

When we do this the ASCII values of the characters are stored in the variables. ASCII values are used to represent any character in memory. The ASCII values of ‘F’ and ‘G’ are 70 and 71.

4 . Arithmetic operations can be performed on ints, floats and chars.

Thus the statements,

char x, y ;
int z ; x = 'a' ;
y = 'b' ;
z = x + y ;

are perfectly valid, since the addition is performed on the ASCII values of the characters and not on characters themselves. The ASCII values of ‘a’ and ‘b’ are 97 and 98, and hence can definitely be added.

5 . No operator is assumed to be present. It must be written explicitly. In the following example, the multiplication operator after b must be explicitly written.

Ex:

a = c.d.b(xy) is the usual arithmetic statement and
b = c * d * b * ( x * y ) is the statement in C .

6. Unlike other high level languages, there is no operator for performing exponentiation operation.
Thus following statements are invalid.

a = 3 ** 2 ;
b = 3 ^ 2 ;

For exponentiation we shall follow the following process.

#include
main( )
{ int a ;
a = pow ( 3, 2 ) ;
printf ( “%d”, a ) ;
}

Here pow( ) function is a standard library function. It is being used to raise 3 to the power of 2. #include is a preprocessor directive. It is being used here to ensure that the pow( ) function works correctly.
IF STATEMENT

MULTIPLE STATEMENTS IN IF

IF AND ELSE

NESTED IF AND ELSE


BREAK

CONTINUE AND DO WHILE IN C LANGUAGE

SWITCH IN C PROGRAMMING

FUNCTIONS IN C PROGRAMMING


Functions and usage in C part two

Coding in C functions

No comments:

Post a Comment