This blog is not for beginers to C language. If you are one, read any C book (preferrably k&r) and then come back to this.
1> One divided by 3 is 0?
What is the value of m here ?
int n=1;
int p = 3;
float m;
m = n/p;
Did you say 0.3333. The answer is wrong. m is zero. You see here n and p are integers. Hence n/p gives integer result which is 0. Now m is assigned to 0.00.
float p = 2 ,q;
int m,n;
m=1; n=3;
q = m/n * p;
Now tell me what is the value of q in the above code. Don't tell me that it is 0.6667. It is again zero. Now you may think that as p is a non-integer, the calculation should happen in floating point arithmetic. But that happens after m/n is performed. In a division if both numerator and denominator are integers, result is a truncated integer. This zero is now multiplied by 2 which again gives 0.
2> Modulo operator does not compile ?
float a = 2;
int m = 15 % a;
What is the value of m? It is not 1. The code produces a syntax error which says something like '%' : illegal, right operand has type 'float'. Because Both operands for % operator should be integers.
3> Division by zero is not caught by compiler
float p = 2 ,q;
int m,n;
m=1; n=0;
q = m/n * p;
Do you think 4th line in this code produces syntax error and saves you from further trouble. No. C compilers think you programmers are very smart and even very careful. So the code compiles and then executes and gives a run time exception division by zero. Always ensure that your denominator is not zero.
4> Lazy evaluation
Let us ensure that denominator is not zero when performing a division
float p = 2 ,q;
int m,n,s=3;
m=1; n=0;
if (s == m/n && n!=0)
printf("%d",s);
else
printf("Denominator is zero.");
OK, we have taken care of checking denominator is non-zero. Are we saved? Not yet. This also produces run time error for s==m/n.
Let us interchange the operands of && operator , because && is cumulative i.e. A&&B is same is B&&A. Now our code is
float p = 2 ,q;
int m,n,s=3;
m=1; n=0;
if (n!=0 && s==m/n)
printf("%d",s);
else
printf("Denominator is zero.");
Now the code runs correctly. What went right? Well this is called lazy evaluation. Logical and and or operators have left to right associativity (i.e. they are evaluated from left to right) and the evaluation stops when an unambiguous answer is obtained. If n is zero, first condition is false and irrespective of second condition, the result will be false. Hence C does not evaluate the second operand. So no division by zero and no run time error.
In situations like these, lazy evaluation (also called short circuiting) comes to your rescue.
Last question.
5> Isn't -2 less than 4 ?
May not be.
t = -2 "<" sizeof(int);
Sizeof int is 4 for all 32 bit processors. So -2"<"4 ? Is true. So t must be true that is 1. No! You get t as 0? Howzzat?
sizeof operator yields an unsigned integer. Now < operator has one operand -2 as int and another sizeof(int) as unsigned int which is larger data type. So compiler converts both operands into unsigned. -2becomes 0xfffffffe when converted to unsigned. This is certainly not less than 4. Hence the result.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment