✏️ Explanatory Question
Point out the errors, if any, in the following C statements and write the correct statements.
(a) int = 314.562 * 150 ;
(b) name = ‘Ajay’ ;
(c) varchar = ‘3’ ;
(d) 3.14 * r * r * h = vol_of_cyl ;
(e) k = ( a * b ) ( c + ( 2.5a + b ) ( d + e ) ;
(f) m_inst = rate of interest * amount in rs ;
(g) si = principal * rateofinterest * numberofyears / 100 ;
(h) area = 3.14 * r ** 2 ;
(i) volume = 3.14 * r ^ 2 * h ;
(j) k = ( (a * b ) + c ) ( 2.5 * a + b ) ;
(k) a = b = 3 = 4 ;
(l) count = count + 1 ;
(m) date = '2 Mar 04' ;
| Statement | Error Reason | Correct Statement |
|---|---|---|
| (a) int = 314.562 * 150 ; | int is a keyword and cannot be used as a variable name. | result = 314.562 * 150 ; |
| (b) name = ‘Ajay’ ; | Strings must be enclosed within double quotation marks (" "). | name = "Ajay" ; |
| (c) varchar = ‘3’ ; | If varchar is an integer variable, single quotes should not be used. | varchar = 3 ; |
| (d) 3.14 * r * r * h = vol_of_cyl ; | The left side of assignment operator (=) must be a variable. | vol_of_cyl = 3.14 * r * r * h ; |
| (e) k = ( a * b ) ( c + ( 2.5a + b ) ( d + e ) ; | Multiplication operators (*) are missing and brackets are incorrect. | k = (a * b) * (c + (2.5 * a + b) * (d + e)) ; |
| (f) m_inst = rate of interest * amount in rs ; | Variable names cannot contain spaces. | m_inst = rate_of_interest * amount_in_rs ; |
| (g) si = principal * rateofinterest * numberofyears / 100 ; | There is no syntax error in this statement. | si = principal * rateofinterest * numberofyears / 100 ; |
| (h) area = 3.14 * r ** 2 ; | C language does not support the exponent operator (**). | area = 3.14 * r * r ; |
| (i) volume = 3.14 * r ^ 2 * h ; | The ^ operator is a bitwise XOR operator, not a power operator. | volume = 3.14 * r * r * h ; |
| (j) k = ( (a * b ) + c ) ( 2.5 * a + b ) ; | Multiplication operator (*) is missing between brackets. | k = ((a * b) + c) * (2.5 * a + b) ; |
| (k) a = b = 3 = 4 ; | Assignment cannot be made to a constant value. | a = b = 3 ; |
| (l) count = count + 1 ; | There is no error in this statement. | count = count + 1 ; |
| (m) date = '2 Mar 04' ; | Multiple characters cannot be enclosed within single quotes. | date = "2 Mar 04" ; |
#include
int main()
{
float r = 5, h = 10;
float volume, area;
area = 3.14 * r * r;
volume = 3.14 * r * r * h;
printf("Area = %.2f\n", area);
printf("Volume = %.2f\n", volume);
int count = 0;
count = count + 1;
printf("Count = %d\n", count);
return 0;
}
Area = 78.50
Volume = 785.00
Count = 1