Why does the answer is 0?

±

±èµ¿±Õ

#include"stdio.h"

main()
{
int a = 128 * 128 * 128 * 128 * 128 * 128 * 128 * 128 ;
int a1 = 256 * 256 * 256 * 256;
char b = 256 * 256 * 256 * 256;
long c = 256 * 256 * 256 * 256;
float d = 512 * 512;
double e = 64 * 64 * 64 * 64 * 64 * 64 * 64 * 64 * 64 * 64 * 64 * 64 * 64 *
64 * 64 * 64;
double e1 = 256 * 256 * 256 * 256;
printf("a= %d \nb= %d \nc= %d \nd= %d \nd= %f \ne=%lf \n", a,b,c,d,d,e);
printf("a1= %d \ne1=%lf\n",a1,e1);
}

//anyway, all the same , 0 !!!
 
B

Barry Schwarz

#include"stdio.h"

main()
{
int a = 128 * 128 * 128 * 128 * 128 * 128 * 128 * 128 ;
int a1 = 256 * 256 * 256 * 256;
char b = 256 * 256 * 256 * 256;
long c = 256 * 256 * 256 * 256;
float d = 512 * 512;
double e = 64 * 64 * 64 * 64 * 64 * 64 * 64 * 64 * 64 * 64 * 64 * 64 * 64 *
64 * 64 * 64;
double e1 = 256 * 256 * 256 * 256;
printf("a= %d \nb= %d \nc= %d \nd= %d \nd= %f \ne=%lf \n", a,b,c,d,d,e);
printf("a1= %d \ne1=%lf\n",a1,e1);
}

//anyway, all the same , 0 !!!
Unless sizeof(int) is greater than four on your system, most of your
code invokes undefined behavior. If sizeof(int) is two, then d also
invokes undefined behavior. The first printf also.

Even if your compiler generates the "expected" code and merely
truncates the values, the lower order portion of each result is always
zero.

Pick an easy one, like d, and work it out in hex.




<<Remove the del for email>>
 
D

dot

#include"stdio.h"

main()
{
int a = 128 * 128 * 128 * 128 * 128 * 128 * 128 * 128 ;

Overflow ... 128^8 = 72,057,594,037,927,936
int a1 = 256 * 256 * 256 * 256;

Depending on your compiler, another overflow ... 256^4 = 4,294,967,296

You'd need 64 bits to hold this value. It's 1 more than can be stored in an
unsigned 32 bit variable... more than double what a signed 32bit variable
can hold.
char b = 256 * 256 * 256 * 256;

An obvious overflow ... char = -128 to + 127

long c = 256 * 256 * 256 * 256;

Depending on your compiler, yet another overflow.
float d = 512 * 512;

and so on...

To start ... try something painfully simple like this...


double a;
int b;
a = 3.1416;
b = 125;
printf("a = %f b = %d", a, b);

What do you get?


There are limits to what you can do... check your compiler's documentation,
find out the max and min for these variable types. Obey them.

Also, when displaying these values be careful of the formatting characters
for printf, always use the documented formatters for the variable types you
are displaying.
 
K

Keith Thompson

[...]
char b = 256 * 256 * 256 * 256;

An obvious overflow ... char = -128 to + 127

Or 0 to 255 (if plain char is unsigned), or -127 to +127 (if plain
char is signed but not two's-complement), or other ranges if
CHAR_BIT > 8.
 
M

manoj1978

±èµ¿±Õ said:
#include"stdio.h"

main()
{
int a = 128 * 128 * 128 * 128 * 128 * 128 * 128 * 128 ;
int a1 = 256 * 256 * 256 * 256;
char b = 256 * 256 * 256 * 256;
long c = 256 * 256 * 256 * 256;
float d = 512 * 512;
double e = 64 * 64 * 64 * 64 * 64 * 64 * 64 * 64 * 64 * 64 * 64 * 64 * 64 *
64 * 64 * 64;
double e1 = 256 * 256 * 256 * 256;
put a cast on right side for double,otherwise expression will be an
int and will overflow and it will be undefined behaviour.
in all other cases right hand side expression will over flow 4 byte
int.
 
C

CBFalconer

±èµ¿±Õ said:
#include"stdio.h"

main()
{
int a = 128 * 128 * 128 * 128 * 128 * 128 * 128 * 128 ;
int a1 = 256 * 256 * 256 * 256;
char b = 256 * 256 * 256 * 256;
long c = 256 * 256 * 256 * 256;
float d = 512 * 512;
double e = 64 * 64 * 64 * 64 * 64 * 64 * 64 * 64 * 64 * 64 *
64 * 64 * 64 * 64 * 64 * 64;
double e1 = 256 * 256 * 256 * 256;
printf("a= %d \nb= %d \nc= %d \nd= %d \nd= %f \ne=%lf \n", a,b,c,d,d,e);
printf("a1= %d \ne1=%lf\n",a1,e1);
}

//anyway, all the same , 0 !!!

Absolutely correct. 0 is a suitable value of undefined behaviour.
 
M

Mark McIntyre

int a = 128 * 128 * 128 * 128 * 128 * 128 * 128 * 128 ;

Compiling...
clc_test.c
g:\users\mark\c\clc_tests\clc_test.c(5) : warning C4307: '*' :
integral constant overflow
g:\users\mark\c\clc_tests\clc_test.c(6) : warning C4307: '*' :
integral constant overflow
g:\users\mark\c\clc_tests\clc_test.c(7) : warning C4307: '*' :
integral constant overflow
g:\users\mark\c\clc_tests\clc_test.c(8) : warning C4307: '*' :
integral constant overflow
g:\users\mark\c\clc_tests\clc_test.c(10) : warning C4307: '*' :
integral constant overflow
g:\users\mark\c\clc_tests\clc_test.c(12) : warning C4307: '*' :
integral constant overflow
Linking...
//anyway, all the same , 0 !!!

what do you expect - almost none of these numbers fit into the type
you've used, so you've invoked undefined behaviour and your compiler
is entitled to zap all your other results. .

Turn up your compiuler warning levels and read the messages.
 
J

John Temples

On Mon, 28 Mar 2005 14:17:49 +0900, "±èµ¿±Õ"
Pick an easy one, like d, and work it out in hex.

I'm not sure that d is an easy one if your expectation is that the way
the expression on the right side of the = is evaluated is influenced
by the type of the variable on the left side of the =, which is a
common misconception.
 
P

Peter Shaggy Haywood

Groovy hepcat ±èµ¿±Õ was jivin' on Mon, 28 Mar 2005 14:17:49 +0900 in
comp.lang.c.
Why does the answer is 0?'s a cool scene! Dig it!
#include"stdio.h"

Wrong! That should be

#include <stdio.h>

[Snippage.]
long c = 256 * 256 * 256 * 256;

[More snippage.]
printf("a= %d \nb= %d \nc= %d \nd= %d \nd= %f \ne=%lf \n", a,b,c,d,d,e);
^^ ^
Has anyone pointed out this error yet? You're passing a long but
telling printf() to expect an int (%d conversion specifier).
printf("a1= %d \ne1=%lf\n",a1,e1);
^^^
In C 90 there is no %lf conversion specifier. I think (and please
correct me if I'm wrong, sombody) this is allowed by C99. But you're
not using a C99 compiler, otherwise it would have rejected your main()
because you did not specify the return type, and C99 does not have
"implicit int" as C90 did.
You should also have a return statement at the end. Portable return
values are 0, EXIT_SUCCESS and EXIT_FAILURE (the latter two being
macros defined in stdlib.h).

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 
M

Mark L Pappin

Depending on your compiler, another overflow ... 256^4 = 4,294,967,296

You'd need 64 bits to hold this value.

64 would be sufficient, but not necessary.
You would _need_ at least 33 bits.

Sizes using other than a power of 2 of bits _are_ possible, if now
uncommon.

mlp
 
D

Dave Thompson

Depending on your compiler, another overflow ... 256^4 = 4,294,967,296

You'd need 64 bits to hold this value. It's 1 more than can be stored in an
unsigned 32 bit variable... more than double what a signed 32bit variable
can hold.
Nit: more than 32 bits, but not necessarily 64. I don't expect to see
anyone building a 33-bit machine, although C would allow it, but there
have been 36-bit, 48-bit, and 60-bit word machines and at least some
of them have had C implementations.

- David.Thompson1 at worldnet.att.net
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top