Multiply

M

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
 
M

MJK

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
 
J

Jack Klein

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:
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
 
M

manju.rvce12

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.
 
G

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);
}

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";
}
 
J

Jack Klein

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
 
J

Juha Nieminen

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top