Home > Back-end >  Getting Unexpected errors
Getting Unexpected errors

Time:10-02

#include <stdio.h>

int main()
{
    int r;
    float pi = 3.142;
printf("Enter the radius of circle to find its area");
scanf("%d",&r);
printf("The area of the circle is %d \n", r*r*pi );
    int h;
    printf("Enter the height of the circle to find its Volume");
    scanf("%d",&h);
printf("The volume of the circle is %d \n", h*r*r*r*pi);
return 0;
}

I am trying to take input and then calculate the area and volume of circle but getting unexpected errors. The first error is this:

practise2.c: In function ‘main’:
practise2.c:4:5: error: expected declaration or statement at end of input

Gives me this when i run
[Running] cd "/home/spyder/Desktop/" && gcc practise2.c -o practise2 && "/home/spyder/Desktop/"practise2
practise2.c: In function ‘main’:
practise2.c:4:5: error: expected declaration or statement at end of input
    4 |     {
      |     ^

[Done] exited with code=1 in 0.133 seconds

CodePudding user response:

  1. Expresion r*r*pi id in float type, use %f format

practise2.c:4:5: error: expected declaration or statement at end of input

  1. Do You maybe have code copied & pasted from net?

Apostrophes may be wrong

CodePudding user response:

I can not replicate the error you are describing in your post, please detail your compile flags.

Also, there are a few problems with your code.

1: You should replace all occurrences of '%d' in your code with '%f' so you can accept floating point numbers perform the correct arithmetic.

2: The math in the following line is incorrect to calculate the volume of a cylinder.

printf("The volume of the circle is %d \n", h*r*r*r*pi);

The above line should in fact be as follows...

printf("The volume of the circle is %d \n", h*r*r*pi);

CodePudding user response:

Basic formatting specifiers used with printf (and variants)

%c  a single character
%s  a string
%d  a decimal integer (assumes base 10)
%i  a decimal integer (detects the base automatically)
%o  an octal (base 8) integer
%x  a hexadecimal (base 16) integer
%p  an address (or pointer)
%f  a floating point number for floats
%u  unsigned decimal integer

Wa alaikum salam

  •  Tags:  
  • c
  • Related