473,507 Members | 11,372 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 !!!
Nov 14 '05 #1
8 1589
On Mon, 28 Mar 2005 14:17:49 +0900, "±èµ¿±Õ"
<na*******@game.hs.kr.NOSPAMOK> wrote:
#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>>
Nov 14 '05 #2
do*@dot.dot writes:
On Mon, 28 Mar 2005 14:17:49 +0900, "±èµ¿±Õ" <na*******@game.hs.kr.NOSPAMOK>
wrote:

[...]
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #3
±èµ¿±Õ wrote:
#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. 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 !!!


Nov 14 '05 #4
"±èµ¿±Õ" wrote:

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

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson

Nov 14 '05 #5
On Mon, 28 Mar 2005 14:17:49 +0900, in comp.lang.c , "±èµ¿±Õ"
<na*******@game.hs.kr.NOSPAMOK> wrote:
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.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
Nov 14 '05 #6
In article <do********************************@4ax.com>, Barry Schwarz wrote:
On Mon, 28 Mar 2005 14:17:49 +0900, "±èµ¿±Õ"
<na*******@game.hs.kr.NOSPAMOK> wrote:
float d = 512 * 512;

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.

--
John W. Temples, III
Nov 14 '05 #7
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"?
Nov 14 '05 #8
On Mon, 28 Mar 2005 03:46:55 -0500, do*@dot.dot wrote:
On Mon, 28 Mar 2005 14:17:49 +0900, "±èµ¿±Õ" <na*******@game.hs.kr.NOSPAMOK>
wrote:

< various integer overflows, snipped>
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.

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
Nov 14 '05 #9

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

Similar topics

8
2279
by: Will | last post by:
I just discovered Python and looked briefly at one of the tutorials for beginners... It looks a lot like the old Command line Basic... I'm sure it does much more but... 1 - Can you create...
20
10084
by: | last post by:
If I need to check if a certain value does exist in a field, and return either "yes" or "not" which query would be the most effestive?
9
5363
by: Thelma Lubkin | last post by:
I've been looking at code that handles string manipulation and I keep seeing variable names, and function names, too, followed by a '$'. I've also found a variable followed by a '%' symbol, but...
3
4384
by: Richard Lewis Haggard | last post by:
We are having a lot of trouble with problems relating to failures relating to 'The located assembly's manifest definition with name 'xxx' does not match the assembly reference" but none of us here...
8
2357
by: Neil | last post by:
Hello Just to let you know this not homework, I'm learning the language of C on my own time.. I recently tried to create a escape for user saying printf ("Do you want to continue? (y or n)");...
14
2928
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does K = parseInt('09') set K to 0? ----------------------------------------------------------------------- ...
4
1718
tpgames
by: tpgames | last post by:
Here is the code for 1 question and answer, and the play again code. When I use a play again code, the answers do not erase. What code am I missing? Thanks! <tr><td class="line1" colspan="4">...
65
3528
by: Aditya | last post by:
Hi I am using a line of as char recvedValues = {'\0'}; in my code. I get a warning as near initialization for recvedValues. I am using gcc 3.4 Can anybody please explain the meaning. Thanks...
11
2683
by: MonkeeSage | last post by:
A quick question about how python parses a file into compiled bytecode. Does it parse the whole file into AST first and then compile the AST, or does it build and compile the AST on the fly as it...
7
4748
by: ghd | last post by:
In Windows XP when does the JVM start (JRE version 1.4 and higher)? And when does it halt? Does the JVM start when we launch a java application (or by executing java classfile)? And does the JVM...
0
7221
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
7029
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7481
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5619
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5039
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3190
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1537
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
411
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.