Wednesday, December 24, 2008

How the execution of the arithmetic statement takes place

While writing an arithmetic statement, it is very important for us to understand how the arithmetic statement works.
Firstly, the operation on the right hand side of the assignment operator takes place using the constants and the numerical value stored in the variable names, and then this value is assigned to the variable on the left hand side.
Though arithmetic statements look very simple, yet the beginners often commit mistakes while writing it, so we have to be very careful while writing an arithmetic statement. Let us discuss on the points which we have to keep in mind while writing an arithmetic statement to avoid errors:

a) C program allows only one variable on the left hand of the assignment operator. That is,

a=b+c;          /* Right Arithmetic statement */
b+c=a;          /* Wrong Arithmetic statement */
b) Many a times a statement similar to the arithmetic statement are used for storing character constants in the character variable.
char a,b,c;
a=‘I’;
b=‘G’;
c=‘+’;
c) Arithmetic operation is flexible with integer, float and char type of variable or constants, i.e., it can be performed on int, float and char type of variables or constants. {also see: Types of Arithmetic statement}
Ex:
char a,b;
int res;
a=‘I’;
b=‘G’;
res=a+b;        /* Valid Arithmetic statement */
This statement is absolutely valid arithmetic statement, since the operation is done on two character type of variables and the result is stored on a integer type of variable so the ASCII values of the constants stored in the character type variables and the character itself, are added and stored in the integer type of variable. Thus the ASCII value of I is 73 & G is 71, and hence it can be added to produce an result of 144, so the value of res=144 after the arithmetic operation.

d) Unlike mathematical arithmetic operation, no operator is assumed to be present, it must be written explicitly.
Ex:
a=b.c.d(e+f)        /* Usual arithmetic statement */
a=b*c*d*(e+f)       /* Right C arithmetic statement */
e) Unlike other high level languages, C language doesn’t have any operator for performing exponentiation operation.
Ex:
a=3**2;       /* Invalid C statement */
a=3^2;       /* Invalid C statement */

No comments:

Post a Comment

Please give your valuable comments or queries if you fell its helpful or if you like the contents of the site. Thanks...