Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Multiply

Reply
Thread Tools

Multiply

 
 
MJK
Guest
Posts: n/a
 
      10-13-2007
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

 
Reply With Quote
 
 
 
 
Kenneth Porter
Guest
Posts: n/a
 
      10-13-2007
MJK <(E-Mail Removed)> wrote in news:1192241830.668389.300410
@e34g2000pro.googlegroups.com:

> Why the following program does not print any output?


What environment? Where is stdout pointing?
 
Reply With Quote
 
 
 
 
MJK
Guest
Posts: n/a
 
      10-13-2007
On Oct 12, 10:24 pm, Kenneth Porter <(E-Mail Removed)>
wrote:
> MJK <(E-Mail Removed)> 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

 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      10-13-2007
On Sat, 13 Oct 2007 02:37:26 -0000, MJK <(E-Mail Removed)> wrote in
comp.lang.c++:

> On Oct 12, 10:24 pm, Kenneth Porter <(E-Mail Removed)>
> wrote:
> > MJK <(E-Mail Removed)> 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
 
Reply With Quote
 
manju.rvce12@gmail.com
Guest
Posts: n/a
 
      10-13-2007
On Oct 13, 7:17 am, MJK <(E-Mail Removed)> 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.

 
Reply With Quote
 
=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=
Guest
Posts: n/a
 
      10-13-2007
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
 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      10-15-2007
On Sat, 13 Oct 2007 11:11:23 GMT, Erik Wikstr÷m
<(E-Mail Removed)> 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
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      10-15-2007
MJK wrote:
>> MJK <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
NEED HELP: multiply and divide with integer in VHDL ledinhkha@gmail.com VHDL 2 12-15-2005 08:06 AM
Multiply using shift, for signed numbers fastgreen2000@yahoo.com VHDL 2 09-13-2005 05:08 AM
logic_std and multiply and array index Pascal Peyremorte VHDL 6 08-29-2005 04:26 PM
HELP ! WHY doesn't SHL multiply by two ?? Jam VHDL 3 09-15-2004 12:16 PM
How to MULTIPLY by fraction ?? (making variable iir) Jam VHDL 2 09-15-2004 11:44 AM



Advertisments