473,320 Members | 1,900 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Multiply

MJK
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

Oct 13 '07 #1
7 1646
MJK <ja*******@gmail.comwrote in news:1192241830.668389.300410
@e34g2000pro.googlegroups.com:
Why the following program does not print any output?
What environment? Where is stdout pointing?
Oct 13 '07 #2
MJK
On Oct 12, 10:24 pm, Kenneth Porter <shiva.blackl...@sewingwitch.com>
wrote:
MJK <jafari...@gmail.comwrote 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

Oct 13 '07 #3
On Sat, 13 Oct 2007 02:37:26 -0000, MJK <ja*******@gmail.comwrote in
comp.lang.c++:
On Oct 12, 10:24 pm, Kenneth Porter <shiva.blackl...@sewingwitch.com>
wrote:
MJK <jafari...@gmail.comwrote 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
Oct 13 '07 #4
On Oct 13, 7:17 am, MJK <jafari...@gmail.comwrote:
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 '07 #5
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
Oct 13 '07 #6
On Sat, 13 Oct 2007 11:11:23 GMT, Erik Wikström
<Er***********@telia.comwrote 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
Oct 15 '07 #7
MJK wrote:
>MJK <jafari...@gmail.comwrote 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.
Oct 15 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Zhang Le | last post by:
Hi, I did a small benchmark of matrix-vector multiply operation using Numeric module. I'm a bit suprised to find matrix*col-vector is much faster than row-vector*matrix. I wonder whether other...
2
by: Martin Pettersson | last post by:
Hi all, I'm trying to multiply parent values in xsl. The thing is that I start with a value down in the xml-structure. From that value (in my case 'qty' value) I check the parent value and later...
15
by: christopher diggins | last post by:
Here is some code I wrote for Matrix multiplication for arbitrary dimensionality known at compile-time. I am curious how practical it is. For instance, is it common to know the dimensionality of...
3
by: Dave | last post by:
Hello all, Please consider this code: #ifndef FOO_INCLUDED #define FOO_INCLUDED // File: foo.h class foo {
4
by: trint | last post by:
Why do we have to use decimal.multiply to multiply two intgers? I just want to multiply these two values: int oneThousand = 1000; int watCHTimer1 = value from a textbox; Thanks, Trint
11
by: vips | last post by:
I have a string str="10*12*14" now I want the total of this value i.e. 10 multiply 12 multiply 14 how do i get it ? is there any function in vb.net to do that ?? if i put it as query to...
0
by: komandjaja | last post by:
Hi everybody. I am new here and I don't know much about CSS. However, I have a blog at multiply http://komandjaja.multiply.com and I have customized the CSS codes there to make a new theme. When I...
12
by: Janaka Perera | last post by:
Hi All, We have done a object oriented design for a system which will create a class multiply inherited by around 1000 small and medium sized classes. I would be greatful if you can help me...
14
by: Default User | last post by:
Hi, If I have three 64 bit integers and I want to do this operation on them: x*y/z Lets say that what we are multiplying by (y) is offset by what we are dividing by (z) so that the final...
1
by: Gavin Chen | last post by:
Hello: I tried to install Tk800.015 on SunOS 4.1.4 with perl 5.6.2. At "make test" time, I got the error message as below: collect2: ld returned 2 exit status ld:...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.