No compiler/runtime error?

This is a discussion on No compiler/runtime error? within the C Programming forums, part of the General Programming Boards category; This is definitely a newbie question. I've got a very simple example where I expected to get a compile or ...

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    2

    No compiler/runtime error?

    This is definitely a newbie question. I've got a very simple example where I expected to get a compile or runtime error, but it actually works, albeit with the wrong answer:
    Code:
    #include <stdio.h>
    int square(int x) {
        return x*x;
    }
    int main() {
        float a = 12.5;
        int b = square(a);
        printf("%f^2 = %d\n", a, b);
        return 0;
    }
    If I compile it and run it, I get the following:
    Code:
    12.500000^2 = 144
    It's the wrong answer and the wrong data type for the function call, but the C compiler allows it. Can someone point me to a reason?

    Many thanks!

  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,490
    Quote Originally Posted by lkr1919 View Post
    It's the wrong answer and the wrong data type for the function call, but the C compiler allows it. Can someone point me to a reason?
    It is called implicit type conversion, and the float 12.5 is automatically converted into the intefer, 12.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,012
    The compiler should give a warning.

    But there's only one reasonable thing that the programmer could mean to do, so it does it.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    957
    C is a weakly typed language (or, to paraphrase Peter van der Linden, C programmers think "strong typing" means hitting the keyboard harder). Arithmetic types (which is to say integers and floating point values) can be freely converted between each other, and the compiler is not required to complain. Note that this includes char, which is an integer. With a cast, C allows pointers and integers to be converted between each other, as well.

    What this means is, be careful with types. You've seen the conversion between floating point an integral types, but there can also be issues when converting between signed and unsigned integral types, for example. The free conversion of types can be useful, but it is a double-edged sword.

  5. #5
    Registered User
    Join Date
    Jun 2012
    Posts
    2
    That makes sense. Many thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compile time error or runtime error?
    By George2 in forum C# Programming
    Replies: 3
    Last Post: 05-07-2008, 07:08 AM
  2. is there a runtime error here?
    By rnx40401 in forum C++ Programming
    Replies: 18
    Last Post: 10-29-2007, 07:36 PM
  3. runtime cin error
    By _lazycat_ in forum C++ Programming
    Replies: 5
    Last Post: 10-14-2007, 01:58 AM
  4. Runtime Error
    By Highland Laddie in forum C++ Programming
    Replies: 9
    Last Post: 09-16-2005, 03:29 PM
  5. runtime error
    By poorman in forum C Programming
    Replies: 1
    Last Post: 06-23-2002, 03:19 AM

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