ints & floats

This is a discussion on ints & floats within the C Programming forums, part of the General Programming Boards category; Hello The output of code1 gives any garbage value whereas output of code2 gives run time error.Why is this so?why ...

  1. #1
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804

    ints & floats

    Hello
    The output of code1 gives any garbage value whereas output of code2 gives run time error.Why is this so?why both of the codes not showing an error because there are no arguments for %d & %f.
    Code:
    /*CODE1*/
    #include<stdio.h>
    #include<conio.h>
    int main(void)
    {
    clrscr();
    printf("%d");
    getch();
    return 0;
    }
    Code:
    /*CODE2*/
    #include<stdio.h>
    #include<conio.h>
    int main(void)
    {
    clrscr();
    printf("%f");
    getch();
    return 0;
    }
    Thanks

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,777
    It's undefined, so anything can happen. It can crash, it won't crash, it can eat up your computer to burn up your house. Anything can happen.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    As they say, this could fire a nuclear missile but that would be unlikely unless you have required hardware...
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And of course, if you have a good compiler, such as gcc, with warnings enabled, it will actually tell you that you are missing the arguments for printf, which is very handy when you are as bad at writing correct code as I am...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    I would add to the undefined behavior argument of the previous posts that printf() belongs to this particular set of functions taking a variable number of arguments which means the compiler has to do an extra effort to check whether the call is valid or not and this goes beyond the simple compilation of C as the its requires to know the semantic of the parameters (in the case of printf and scanf and equivalents, parse the format string, warnings are enabled for gcc with -Wformat[=2], this won't work for functions not using the standard 'format' parameter)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. efficiency comparing ints and floats
    By MK27 in forum C Programming
    Replies: 14
    Last Post: 02-28-2009, 02:56 PM
  2. Replies: 15
    Last Post: 12-16-2008, 11:11 PM
  3. determining floats or ints
    By quiet_forever in forum C++ Programming
    Replies: 3
    Last Post: 04-15-2007, 07:11 PM
  4. reading int's and floats...
    By flightsimdude in forum C Programming
    Replies: 8
    Last Post: 09-20-2003, 01:17 PM
  5. Replies: 2
    Last Post: 08-05-2002, 02:19 PM

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