Chapter One: Getting started (Let us C)
EXERCISE:
[A] Point out the errors, if any, in the following C statements:
(a) x = (y+3);
Ans: There are no errors in the above statement.
Explanation: This is a assignment statement where 3 is added to the value of y and then assigned to the value of x.
(b) cir = 2 * 3.141593 * r;
Ans. There are no errors in the above statement.
Explanation: This is a assignment statement where 2 is multiplied to the value of π (3.141593) and then multiplied to the value of r (radius) and the result is stored in the memory location having the name cir.
(c) char = '3';
Ans. There is an error in the above statement.
Explanation: This is an assignment statement where the name of the variable is missing.
(d) 4/3*3.14*r*r = vol_of_sphere;
Ans. There is an error in the above assignment.
Explanation: C compiler is designed in a way that it first perform the task and then the result is stored in a variable, here is vice-versa.
(e) volume = a3 ;
Ans. There is an error in the above statement.
Explanation: C compiler doesn't have a way to directly write the exponentiation. Hence the above statement can be written as {volume = a * a * a}.
(f) area = 1/2 * base * height;
Ans. There are no errors in the above statement.
Explanation: This is an assignment statement where the value of height is multiplied by base, value obtained is then multiplied by half and the result is stored in the the variable area.
(g) si = p * r * n / 100;
Ans. There are no errors in the above statement.
Explanation: This is an assignment statement where the value of p is multiplied with r and n and then the obtained value is divided by 100 and stored in the variable si.
(h) area of the circle = 3.14 * r * r;
Ans. There is an error in the above statement.
Explanation: This is also an assignment statement, but here the variable in which the final value needs to be stored is named against the laws of naming a variable in the C compiler.
As there are spaces in the name of the variable and a C compiler doesn't allows spaces in the names of the C compiler.
(i) peri_of_tri = a + b + c;
Ans. There are no errors in the above statement.
Explanation: This is an assignment statement where the value of c is added to b then to a and the result is stored in the variable peri_of_tri.
(j) slope = (y2 -y1) ÷ (x2 - x1);
Ans. There is an error in the above statement.
Explanation: C compiler doesn't have a symbol of ÷ for division rather / is used for divide.
(k) 3 = b = 4 = a;
Ans. There is an error in the above statement.
Explanation: This is an assignment statement which is trying to store the values against the rules of the C compiler, a is stored in 4, where 4 is a constant.
The C compiler allows the constant to be stored in a variable and not vice-versa and the variable is always declared to the LHS of the expression.
(l) count = count + 1;
Ans. There is no error in the above statement.
Explanation: This is an assignment statement where 1 is added to the current value of count and the result is stored in the count.
(m) char ch = '25 April 12';
Ans. ** There won't be any error on running the program with the above statement but the the above mentioned statement is pointless, here were are trying to store a string in a character variable ch and as a character variable can only store one byte of data at a time hence the entire data will be dost and 2 will be stored in the variable ch.
P.S: I hope the explanations are very much clear, for more deeper concepts videos and images will be added soon.
Keep visiting, keep learning!!
No comments:
Post a Comment