-1

I am having problems with the following code:

#include<stdio.h>
#include<conio.h>
float area_crcl(int);
float area_rect(int,int);

void main()
 {
 int n,a,b,r;
 float area;
 clrscr();
  printf("\nEnter your choice-\n1.Area of circle\n2.Area of Square\n3.Area of Rectangle\n");
  scanf("%d",&n);
   switch(n)
     {
     case 1:printf("\nEnter the radius of circle..\n");
        scanf("%d",&r);
        area=area_crcl(r);
        printf("\nArea of circle is %d\n",area);
        break;

     case 2:printf("\nenter the edge of square\n");
        scanf("%d",&a);
        area=area_rect(a,a);
        printf("\nArea of square is %d\n",area);
        break;

     case 3:printf("\nenter the lenght n breadth of rectangle\n");
        scanf("%d%d",&a,&b);
        area=area_rect(a,b);
        printf("\nArea of rectangle is %d\n",area);
        break;

     default:printf("\nU entered wrong choice..\n");
   }
  getch();
 }

 float area_crcl(int r)
   {
    float area;
    area=3.14*r*r;
    return area;
   }

 float area_rect(int a,int b)
   {
    float area;
    area=a*b;
    return area;
   }

The output which I am getting is :

Enter your choice- 1.Area of circle 2.Area of Square 3.Area of Rectangle 1

Enter the radius of circle.. 2

Area of circle is 0

Why am I getting 0 as output?

dreamcrash
  • 47,137
  • 25
  • 94
  • 117
DD24
  • 61
  • 7
  • 6
    `pi=3.14`... what a wonderful precision – Synxis Jul 13 '13 at 18:21
  • @Synxis you know, there's **no** `M_PI` in ``... Nor is there `` at all... >. –  Jul 13 '13 at 18:28
  • 4
    Also, ***`int`*** `main()`. –  Jul 13 '13 at 18:29
  • 1
    Furthermore, another one to @OP: next time check the documentation of the function you are trying to use - don't make assumptions. That's not any good. –  Jul 13 '13 at 18:29
  • 1
    @H2CO3; I think he is using **Turbo C/C++**, that's why he used `void main()` – haccks Jul 13 '13 at 18:31
  • @haccks He could still use `int main()` and `return 0` could not he ? – 0decimal0 Jul 13 '13 at 18:35
  • @PHIfounder; I used **Turbo C++** and unfortunately `int main`, `return 0` never worked for me! – haccks Jul 13 '13 at 18:36
  • 1
    @haccks oops !! that was a close call, actually I used it just for few days and before I could understand it better , I just moved on, I think almost everybody starts with turbo C when starting. – 0decimal0 Jul 13 '13 at 18:40
  • 3
    @haccks: I have no sympathy for anyone still using Turbo C(++). There are far less ancient compilers out there for free...and apparently they're more conforming, too. – cHao Jul 13 '13 at 18:40
  • 1
    @cHao If you look into some developing countries or some others too the small institutes still start with turbo C , So either the student have to be a born programmer or a family encompassing a legacy of programming to start with something better , so at least in this case they need sympathy. – 0decimal0 Jul 13 '13 at 18:43
  • 4
    @PHIfounder: Or he could, i dunno, google for "free c compiler" and read the very first result...or any of the others on the first page, for that matter. Nope. No sympathy. :P – cHao Jul 13 '13 at 18:51
  • @cHao haha , Well obviously :) – 0decimal0 Jul 13 '13 at 18:53

5 Answers5

7

Your print statement is:

printf("\nArea of circle is %d\n",area);

area is a float and you're using a %d format string, which is meant for int variables. That won't work - use %e, %f, %g, or %a.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
  • Dude i have n another doubt now.. why m i gettng value 0 ?? ex if the float value of area is 11.236 in that case %d should print integer part(11.236) i.e. 11 ... why 0 ..??!! – DD24 Jul 14 '13 at 14:21
  • @DD24, who told you it would print the integer part? It doesn't work like that. You *must not* use `%d` to match with anything but an `int`. – Carl Norum Jul 14 '13 at 15:33
  • there's an another question .. check this out .. http://stackoverflow.com/questions/17643161/about-pointers-in-c – DD24 Jul 14 '13 at 20:07
4

For floats, use %f in printf. More generally be very careful to match % in printf's with parameters passed. No check is done and this is entirely at the programmer's charge

printf("\nArea of circle is %f\n",area);
Djee
  • 429
  • 3
  • 9
  • 2
    Some compilers will actually check the format string (if you use a literal string there) and warn you about mismatched types. But yeah, don't rely on that. Always check for yourself. – cHao Jul 13 '13 at 18:26
  • Dude i have n another doubt now.. why m i gettng value 0 ?? ex if the float value of area is 11.236 in that case %d should print integer part(11.236) i.e. 11 ... why 0 ..??!! – DD24 Jul 14 '13 at 14:20
2

You are using %d to print float that is your problem .

And one more thing other than your error :

Just don't get stuck with conio.h , OK ... move on in life :)

0decimal0
  • 3,884
  • 2
  • 24
  • 39
  • ohh f**k ... this %d ..shitt mann!! feeling lyk a dumb .. :/ thnx a lot everybody .. for helpng me with thid stupid question.. – DD24 Jul 14 '13 at 04:31
  • @DD24 And also dump the turbo C as soon as possible, use MinGW or cygwin and try to compile through command interface and if you use linux its even better :) – 0decimal0 Jul 14 '13 at 07:23
  • Dude i have n another doubt now.. why m i gettng value 0 ?? ex if the float value of area is 11.236 in that case %d should print integer part(11.236) i.e. 11 ... why 0 ..??!! – DD24 Jul 14 '13 at 14:21
  • @DD24 Look what you see is an undefined behavior which means anything can happen , yes anything ... Actually a `float ` and an `int` are stored differently so there is no conversion going on here, what you see may be any random number. And if I am not wrong ....in standard C the `float` is automatically converted to double so you see the `8 bytes` are used to store `double` and `4 bytes` for `int` , again you are making an exception. – 0decimal0 Jul 14 '13 at 16:37
  • ahhh thnx a lot ..!! it was quite helpfull.. well thnx buddy .. but i wud like to correct u at a place were u said.. float is automatically coverted to double... this automatic type conversion takes place in JAVA bro.. not in C ..i suppose..m i ryt??? and also here in c int is 2 byte long naa?? – DD24 Jul 14 '13 at 19:41
  • there's an another question .. check this out .. http://stackoverflow.com/questions/17643161/about-pointers-in-c – DD24 Jul 14 '13 at 20:06
  • @DD24 I would say , you are not entirely right ,as before `C89` it was automatically promoted to `double` but later on changes were made and this was removed, So either way you don't have to think about that . Now, still there is a difference in how compiler treats them in memory ............. AS for *int is 2 bytes* ...again I would say that is true for `16-bit machine` ... for `32-bit machine` the `int` size is `4 bytes` . – 0decimal0 Jul 15 '13 at 03:18
  • For the **if the float value of area is 11.236 in that case %d should print integer part(11.236) i.e. 11** I .. here you are telling compiler to print an integer while it is float , the truncation which you are assuming, can happen if you do cast that's when you are transforming the value to `int` and decimal digits are truncated, that's it . Now , I would have extended this discussion through chat but your rep is not that much so I will leave it here hoping that I made myself clear. – 0decimal0 Jul 15 '13 at 03:26
0

area is of type float and you are using %d specifier to print it. Functions

 area_rect()
 area_crcl()

both are returning float value which is assigned to area(which is also float)
Change

 printf("\nArea of circle is %d\n",area);

to

 printf("\nArea of circle is %f\n",area);
haccks
  • 104,019
  • 25
  • 176
  • 264
0

printf("\nArea of circle is %d\n",area);

Area is float so above statement will not work, use the correct one below:

printf("\nArea of circle is %f\n",area);

neham
  • 341
  • 5
  • 18