473,537 Members | 2,659 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How is output 0?

1 New Member
#include<stdio.h>
#include<math.h>
#include<conio.h>

void main()
{
printf("%d",(36.0));
getch();
}
how output is 0
Jun 7 '14 #1
13 1659
Luuk
1,047 Recognized Expert Top Contributor
no, it does not output '0', see below:

Expand|Select|Wrap|Line Numbers
  1. luuk@opensuse:~/tmp> cat test.c
  2. #include<stdio.h>
  3. #include<math.h>
  4.  
  5. void main()
  6. {
  7.         printf("%d\n",(36.0));
  8. }
  9. luuk@opensuse:~/tmp> make test
  10. cc     test.c   -o test
  11. luuk@opensuse:~/tmp> ./test
  12. -733485528
  13. luuk@opensuse:~/tmp> ./test
  14. 623260056
  15. luuk@opensuse:~/tmp> ./test
  16. 694159224
  17. luuk@opensuse:~/tmp> ./test
  18. -1080378728
  19. luuk@opensuse:~/tmp> ./test
  20. 2002711112
  21. luuk@opensuse:~/tmp>
  22.  
Jun 7 '14 #2
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.
Jun 8 '14 #3
mHealth
13 New Member
@Luuk
Any idea, why is the output varying? Should it take
first 4 bytes and print?
Jun 8 '14 #4
Luuk
1,047 Recognized Expert Top Contributor
no idea why it's varying, but you should use this:
Expand|Select|Wrap|Line Numbers
  1. printf("%d",(int)(36.0));
  2.  
or
Expand|Select|Wrap|Line Numbers
  1. printf("%4.0f",(36.0));
  2.  
Jun 8 '14 #5
meditation
13 New Member
Because it is reading random memory location.
Jun 9 '14 #6
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.
Jun 11 '14 #7
aswal
38 New Member
@donbock

What does "encoding schemes for floating point numbers" means?
Jun 13 '14 #8
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.
Jun 13 '14 #9
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).
Jun 13 '14 #10
aswal
38 New Member
@donbock

can you please list me the sources where i can learn more about this topic.
Jun 14 '14 #11
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')
Jun 14 '14 #12
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.
Jun 16 '14 #13
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.
Jun 19 '14 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

4
8123
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";...
3
2774
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;
4
15039
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...
24
2666
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);
0
2053
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
32
2738
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...
3
2613
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);
3
4649
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
5
3314
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...
2
3362
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,
0
7534
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. ...
0
7688
jinu1996
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...
1
7281
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
7644
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
5827
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5221
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4847
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
3353
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...
1
930
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.