#include<stdio.h>
#include<math.h>
#include<conio.h>
void main()
{
printf("%d",(36.0));
getch();
}
how output is 0
13 1659 Luuk 1,047
Recognized Expert Top Contributor
no, it does not output '0', see below: -
luuk@opensuse:~/tmp> cat test.c
-
#include<stdio.h>
-
#include<math.h>
-
-
void main()
-
{
-
printf("%d\n",(36.0));
-
}
-
luuk@opensuse:~/tmp> make test
-
cc test.c -o test
-
luuk@opensuse:~/tmp> ./test
-
-733485528
-
luuk@opensuse:~/tmp> ./test
-
623260056
-
luuk@opensuse:~/tmp> ./test
-
694159224
-
luuk@opensuse:~/tmp> ./test
-
-1080378728
-
luuk@opensuse:~/tmp> ./test
-
2002711112
-
luuk@opensuse:~/tmp>
-
donbock 2,426
Recognized Expert Top Contributor
The "%d" argument to printf tells it to decode the next argument as an int. However, you passed a double. It should come as no surprise that the output is garbled.
@Luuk
Any idea, why is the output varying? Should it take
first 4 bytes and print?
Luuk 1,047
Recognized Expert Top Contributor
no idea why it's varying, but you should use this: -
printf("%d",(int)(36.0));
-
or
Because it is reading random memory location.
donbock 2,426
Recognized Expert Top Contributor
It is not reading random memory locations.
The displayed value varies because different compiler implementations use different encoding schemes for floating point numbers. You should never write code that takes advantage of implementation-specific characteristics such as floating point encoding scheme.
@donbock
What does "encoding schemes for floating point numbers" means?
Luuk 1,047
Recognized Expert Top Contributor
In simple English, it's the way how floating point numbers are stored in memory.
A human will 'store' the value of 2/10 as 'zero dot two' (or something)
A computer, or compiler, might do that differently.
donbock 2,426
Recognized Expert Top Contributor
In general, computer memory only contains integer numbers. However, we want the computer to handle other kinds of things (such as floating point number and strings). This is accomplished by encoding these other things as integer numbers (or sequences of integer numbers).
The example you are probably most familiar with is the ASCII encoding for representing printable characters as integer numbers (65 means 'A', 66 means 'B', etc). However, this is not the only way to encode characters (see EBCDIC).
@donbock
can you please list me the sources where i can learn more about this topic.
Luuk 1,047
Recognized Expert Top Contributor
@donbock:
"In general, computer memory only contains integer numbers"
In general, computer memory only contains binary (zero's and/or one's)
Claiming that a computer stores integer numbers is a simplification.
When a computer stored the next sequence of bits:
01000001
A human wants to read this as an integer, and starts calculation:
1*2^0+0*2^1+0*2^2+...+1*2^6 = 65
But if this person was counting in hexadecimal he would say:
41
(4*16+1=65; but also written in binary: '0100 0001')
donbock 2,426
Recognized Expert Top Contributor
@Luuk: yes, I was deliberately simplifying.
By the way, 01000001 (binary), 65 (decimal), and 41 (hexadecimal) are all integers. It doesn't matter that they use different bases.
@aswal: you can start by looking up character encoding and IEEE Floating Point in Wikipedia.
Luuk 1,047
Recognized Expert Top Contributor
@donbock: NO, 41 is not an integer (in this context)
It is a hexadecimal representation of the integer 65.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Mark Wilson CPU |
last post by:
This must be easy, but I'm missing something...
I want to execute a Perl script, and capture ALL its output into a PHP
variable. Here are my 2 files:
-------------------------------------
test3.pl
-------------------------------------
print "PERL Hello from Perl! (plain print)<br>\n";
print STDERR "PERL This is text sent to STDERR<br>\n";...
|
by: edgekaos |
last post by:
Is method 2 valid?
Method 1:
wstring input = L"STRING";
wstring output = input;
transform(output.begin(), output.end(), output.begin(), towupper);
Method 2:
wstring input = L"STRING";
wstring output;
|
by: Kevin Mansel via .NET 247 |
last post by:
Ok, basically this is my problem. I'm building a console app tocall a dos program. So i'm using the Shell command to call theprogram, now depending on what happens, I want to read theoutput that this program returns. I'm just missing the stepshere. I know that I can set the Shell command to an integer,but this only returns a 0 to me telling me...
|
by: kalamantina |
last post by:
#include "stdafx.h"
#include <stdio.h>
#define output( x ) printf( #x "\r\n" );fflush( stdout )
class CMyBase
{
public:
CMyBase()
{
output( CMyBase() );
f(*this);
|
by: newbie |
last post by:
i'm a newbie of c language.
can anyone help me to implement the code so that I can get the
ciphertext from the output.
thanks.
#ifndef _3DES_H
#define _3DES_H
#ifndef uint8
#define uint8 unsigned char
#endif
| |
by: spibou |
last post by:
Is the output of the C preprocessor deterministic ? What I mean
by that is , given 2 compilers which conform to the same standard,
will their preprocessors produce identical output given as input
the same file ? If not then how much variation is allowed ? Is it
just a bit more or less white space here and there or could could
there be larger...
|
by: MatsL |
last post by:
Hi,
This is seriously driving me crazy, could anyone explain to me why
neither of these doesn't produce XHTML compliant output (it is being
called in Render() btw):
output.WriteLine("<img src=\"" + vars + " />");
output.AddAttribute(HtmlTextWriterAttribute.Src,
(string)vars);
output.RenderBeginTag(HtmlTextWriterTag.Img);
|
by: undshan |
last post by:
I am writing a code that needs to open a file, create an output file, run through my function, prints the results to the output file, and closes them within a loop. Here is my code:
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include "util.h"
//Main Loop
|
by: amit.uttam |
last post by:
Hey everyone,
I've recently jumped big time into python and I'm working on a
software program for testing automation. I had a question about proper
logging of output. What I would like is:
1. a function syslog (to log output to log file only)
2. a function stdout (to log output to stdout only)
3. a function sslog (to log output to both...
|
by: gabosom |
last post by:
Hi! I've been breaking my head trying to get the output variables from my Stored Procedure.
This is my SP code
CREATE PROCEDURE GetKitchenOrderDetail(
@idService int,
--outPut Variables
@idUser int OUTPUT,
|
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. ...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
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...
|
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...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
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...
| |
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...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |