473,569 Members | 3,054 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 1596
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_Keit h) 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,otherwis e 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.c om, 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*******@gam e.hs.kr.NOSPAMO K> 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 "technicall y correct" English; but since when was rock & roll "technicall y 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.ne t
Nov 14 '05 #9

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

Similar topics

8
2282
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 windows, buttons, user input fields, etc as you can with Visual Basic? 2 - Can you call Windows Procedures or what ever they call them
20
10111
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
5367
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 I've only seen that once. Can someone please explain this for me? I haven't been able to find anything about it. thanks, --thelma
3
4389
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 really understand how this could be an issue. The assemblies that the system is complaining about are ones that we build here and we're not changing...
8
2363
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)"); The scanf statement follows the prompt(printf above) which is the last statement in the while loop, before the "}"
14
2949
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does K = parseInt('09') set K to 0? ----------------------------------------------------------------------- The parseInt function decides what base the number is by looking at the number. It assumes that any number beginning with '0x' or '0X' is...
4
1722
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"> <br> <tr><td class="question" colspan="4"> <!--QUESTION20--> <b> 20) Which picture shows Jessie <br> carrying a Living Stone? </b>
65
3553
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 Aditya
11
2687
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 reads expressions? (If the former case, why can't functions be called before their definitions?) Thanks, Jordan
7
4754
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 halt when the all the java applications on the system end? According to the documentation, the JVM halts in two situations: 1. when you terminate the...
0
7703
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7619
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7930
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7681
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7983
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5228
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3662
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1229
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.