ultra-noob question from "let us c" by yashavant kanetkar

This is a discussion on ultra-noob question from "let us c" by yashavant kanetkar within the C Programming forums, part of the General Programming Boards category; Hi, I have no prior programming experience and am teaching myself C through this book. I am trying to get ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    25

    ultra-noob question from "let us c" by yashavant kanetkar

    Hi, I have no prior programming experience and am teaching myself C through this book. I am trying to get this program to work. Here it is:

    Code:
    #include <math.h>
    main()
    {
          int a;
          a=pow(3,2);   /*I don't know why this doesn't work*/
          printf("%d",a);
          
          }
    The screen at the bottom of the compiler (Bloodshed Dev-C++) says:

    [Warning] converting to 'int' from 'double'

    The line I commented on is highlighted in red in the compiler. The program is to raise 3 to the power of 2.

    Thanks!

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    The message says the pow() funtion returns a data type of double, not int.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    25
    Quote Originally Posted by Dino View Post
    The message says the pow() funtion returns a data type of double, not int.

    Thanks. Is there is another function I can use to return an int? Alternatively, if there's some better way to fix it, I'm appreciative of any suggestions.

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    You can cast the pow() function to return an int.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    25
    How do you do that?

  6. #6
    Registered User
    Join Date
    Sep 2008
    Posts
    58
    Code:
    a = (int) pow(3,2);
    Try that.

  7. #7
    Registered User
    Join Date
    Nov 2008
    Posts
    25
    Quote Originally Posted by blurx View Post
    Code:
    a = (int) pow(3,2);
    Try that.
    Thanks everyone. I got it to work by doing what I've posted below. Is that part at the top correct (with the two #includes)? I did it because after I followed blurx's suggestion it highlighted the printf line and said printf was undeclared. As you can see, I started to learn C 2 or 3 days ago, so I'm appreciative of all the help.

    Code:
    #include <math.h>
    #include <stdio.h>
    main()
    {
          int a;
          a = (int) pow(3,2);   /*This works now.*/
          printf("&#37;d",a);
          getchar();
          
          }
    Last edited by newbcore; 11-29-2008 at 09:46 PM.

  8. #8
    Registered User
    Join Date
    Nov 2008
    Posts
    25
    Quote Originally Posted by blurx View Post
    Code:
    a = (int) pow(3,2);
    Try that.

    Also, why does this work?

  9. #9
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    493
    Quote Originally Posted by newbcore View Post
    Also, why does this work?
    Because pow() function returns double and
    you are casting it int.[Note that this may result in loss of precision if arguments
    supplied in pow are double].

    Also main returns int.
    implicit main should not be used.
    http://faq.cprogramming.com/cgi-bin/...&id=1043284376
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Noob question (redeclaring a array)
    By Demon.killer in forum C Programming
    Replies: 8
    Last Post: 10-21-2006, 12:06 PM
  2. Noob Question
    By bumfluff in forum C++ Programming
    Replies: 4
    Last Post: 11-06-2005, 03:36 AM
  3. Noob here...a simple question.
    By The_Mayor in forum C Programming
    Replies: 4
    Last Post: 06-08-2005, 12:48 AM
  4. simple noob question: HWND, HDC etc.
    By freedik in forum Windows Programming
    Replies: 12
    Last Post: 08-19-2003, 03:59 AM
  5. Replies: 5
    Last Post: 11-01-2002, 05:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21