Multiply

Discussion in 'C++' started by MJK, Oct 13, 2007.

  1. MJK

    MJK Guest

    Hello All,

    My question may look silly:

    Why the following program does not print any output?

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>

    int main()
    {
    int i,j;
    i=1000;
    j=i*i;

    printf("%d %d\n",i,j);

    printf("TEST\n");

    return(0);
    }


    Thanks,
    MJK
    MJK, Oct 13, 2007
    #1
    1. Advertising

  2. MJK <> wrote in news:1192241830.668389.300410
    @e34g2000pro.googlegroups.com:

    > Why the following program does not print any output?


    What environment? Where is stdout pointing?
    Kenneth Porter, Oct 13, 2007
    #2
    1. Advertising

  3. MJK

    MJK Guest

    On Oct 12, 10:24 pm, Kenneth Porter <>
    wrote:
    > MJK <> wrote in news:1192241830.668389.300410
    > @e34g2000pro.googlegroups.com:
    >
    > > Why the following program does not print any output?

    >
    > What environment? Where is stdout pointing?


    Hello,

    I am running it on LINUX.
    Program is working when 'i' is small but when 'i' is bigger than
    '100,000' the result is incorrect!
    when i=100,000 then j which is j=i*i is equal to j=1410065408

    Thanks,
    MJK
    MJK, Oct 13, 2007
    #3
  4. MJK

    Jack Klein Guest

    On Sat, 13 Oct 2007 02:37:26 -0000, MJK <> wrote in
    comp.lang.c++:

    > On Oct 12, 10:24 pm, Kenneth Porter <>
    > wrote:
    > > MJK <> wrote in news:1192241830.668389.300410
    > > @e34g2000pro.googlegroups.com:
    > >
    > > > Why the following program does not print any output?


    Please leave enough context for somebody to understand what you are
    talking about without having to read other posts.

    Here is the original program you posted:

    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <math.h>
    >
    > int main()
    > {
    > int i,j;
    > i=1000;
    > j=i*i;
    >
    > printf("%d %d\n",i,j);
    >
    > printf("TEST\n");
    >
    > return(0);
    > }


    Now back to your quoting of Kenneth's reply to your original post:

    > >
    > > What environment? Where is stdout pointing?

    >
    > Hello,
    >
    > I am running it on LINUX.
    > Program is working when 'i' is small but when 'i' is bigger than
    > '100,000' the result is incorrect!
    > when i=100,000 then j which is j=i*i is equal to j=1410065408


    Why don't you post a correct question? You originally posted, your
    exact words, that the program "does not print any output."

    Now you are telling us it DOES produce an output, completely different
    from your original post. Now you are telling us that sometimes it
    produces what you think is the correct output, and sometimes it
    produces what you think is incorrect output.

    Actually, the output is never incorrect, because the cases that you
    think are incorrect involve undefined behavior, where there is no
    correct or incorrect result.

    The undefined behavior is caused by arithmetic overflow on a signed
    int.

    Compile and execute this program:

    #include <stdio.h>
    #include <limits.h>

    int main(void)
    {
    printf("The maximum value of an int is %d\n", INT_MAX);
    return 0;
    }

    Note the output.

    Then use a calculator or pencil and paper and calculate the value of
    100,000 times 100,000.

    Compare the result of the calculation with the output of the program
    above.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Oct 13, 2007
    #4
  5. MJK

    Guest

    On Oct 13, 7:17 am, MJK <> wrote:
    > Hello All,
    >
    > My question may look silly:
    >
    > Why the following program does not print any output?
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <math.h>
    >
    > int main()
    > {
    > int i,j;
    > i=1000;
    > j=i*i;
    >
    > printf("%d %d\n",i,j);
    >
    > printf("TEST\n");
    >
    > return(0);
    >
    > }
    >
    > Thanks,
    > MJK








    int takes 4 bytes in GCC.means a signed int at maximum can hold
    2147483648.but whan i=100000 then j will be 10000000000.hence the
    output.
    , Oct 13, 2007
    #5
  6. On 2007-10-13 04:17, MJK wrote:
    > Hello All,
    >
    > My question may look silly:
    >
    > Why the following program does not print any output?
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <math.h>
    >
    > int main()
    > {
    > int i,j;
    > i=1000;
    > j=i*i;
    >
    > printf("%d %d\n",i,j);
    >
    > printf("TEST\n");
    >
    > return(0);
    > }


    I would like to point out that while the above code is valid C++ it is
    also valid C in which case it might be better to ask in comp.lang.c.

    The equivalent C++ (as in a program that makes use of the facilities
    provided by C++) would be something like this:

    #include <iostream>

    int main()
    {
    int i = 1000;
    int j = i * i;

    std::cout << i << " " << j << "\n;
    std::cout << "TEST\n";
    }

    --
    Erik Wikström
    =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=, Oct 13, 2007
    #6
  7. MJK

    Jack Klein Guest

    On Sat, 13 Oct 2007 11:11:23 GMT, Erik Wikström
    <> wrote in comp.lang.c++:

    > On 2007-10-13 04:17, MJK wrote:
    > > Hello All,
    > >
    > > My question may look silly:
    > >
    > > Why the following program does not print any output?
    > >
    > > #include <stdio.h>
    > > #include <stdlib.h>
    > > #include <math.h>
    > >
    > > int main()
    > > {
    > > int i,j;
    > > i=1000;
    > > j=i*i;
    > >
    > > printf("%d %d\n",i,j);
    > >
    > > printf("TEST\n");
    > >
    > > return(0);
    > > }

    >
    > I would like to point out that while the above code is valid C++ it is
    > also valid C in which case it might be better to ask in comp.lang.c.


    comp.lang.c is both unwilling and unqualified to discuss the behavior
    of this program when it is translated by a C++ compiler. In fact,
    comp.lang.c is both unwilling and unqualified to discuss whether the
    sample program is valid C++.

    > The equivalent C++ (as in a program that makes use of the facilities
    > provided by C++) would be something like this:


    This statement is really quite wrong, at least as worded. Here on
    comp.lang.c++ I am qualified to say that the program is valid C++ as
    written, and makes use of the stdio library facility that is in fact
    required to be provided by every single conforming hosted C++
    implementation. It also includes two unnecessary standard headers
    required to be provided.

    If you mean, "don't use features inherited from C when C++ specific
    alternatives exist", please say so. Don't word your suggestion so it
    implies that the printf() function is not a "facility" provided by
    C++, because it most certainly is.

    > #include <iostream>
    >
    > int main()
    > {
    > int i = 1000;
    > int j = i * i;
    >
    > std::cout << i << " " << j << "\n;
    > std::cout << "TEST\n";
    > }


    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Oct 15, 2007
    #7
  8. MJK wrote:
    >> MJK <> wrote in news:1192241830.668389.300410
    >> @e34g2000pro.googlegroups.com:
    >>
    >>> Why the following program does not print any output?


    > Program is working when 'i' is small but when 'i' is bigger than
    > '100,000' the result is incorrect!


    First you say that the program doesn't output anything. Now you say
    that it is outputting something (but the wrong value)?

    The maximum size if int is determined by your hardware. In your case
    it's probably 2^31-1 (2147483647). You are simply experiencing an
    overflow.
    Juha Nieminen, Oct 15, 2007
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jam
    Replies:
    3
    Views:
    2,780
    Martin Thompson
    Sep 15, 2004
  2. Jam
    Replies:
    2
    Views:
    729
  3. Pascal Peyremorte

    logic_std and multiply and array index

    Pascal Peyremorte, Aug 19, 2005, in forum: VHDL
    Replies:
    6
    Views:
    2,042
    Pascal Peyremorte
    Aug 29, 2005
  4. Replies:
    2
    Views:
    2,906
    Hubble
    Sep 13, 2005
  5. Replies:
    2
    Views:
    19,085
    Thomas Stanka
    Dec 15, 2005
Loading...

Share This Page