SPL Question Solutions
Q1(d):
Identify the syntax errors from the following program. After corrections what will be the output would you expect when you execute it?
#define PI 3.14
int main()
{
int radious;
float perimeter, area
radious = 5
perimeter = 2.0 * PI * radious;
area = PI * radious * radious;
printf("%f, %f", perimeter, area)
return 0;
}
Marks: 2+2
Syntax Errors Identified¶
Here are the specific errors in the code:
- Missing Header File: The program uses
printfbut is missing#include <stdio.h>at the top. - Missing Semicolon (Line 5): The declaration
float perimeter, areais missing a semicolon;at the end. - Missing Semicolon (Line 6): The assignment
radious = 5is missing a semicolon;at the end. - Missing Semicolon (Line 9): The
printfstatement is missing a semicolon;at the end.
Corrected Code¶
#include <stdio.h> // Include standard I/O library
#define PI 3.14
int main()
{
int radious;
float perimeter, area; // Added semicolon
radious = 5; // Added semicolon
perimeter = 2.0 * PI * radious;
area = PI * radious * radious;
printf("%f, %f", perimeter, area); // Added semicolon
return 0;
}
Expected Output¶
When executed, the program calculates the perimeter (\(2 \times 3.14 \times 5\)) and area (\(3.14 \times 5^2\)). By default, the %f format specifier prints 6 decimal places.
Output: