473,408 Members | 2,444 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Strange Output

#include <stdio.h>
#include <limits.h>

void printbin(void* num) {
size_t i, j;
unsigned char *pc = (unsigned char* )&num;
pc += sizeof(num) - 1;

for (i = 0 ; i < sizeof(num) ; pc--, i++ ) {
for ( j = 0; j < CHAR_BIT ; j++ ) {
putchar( *pc & ( 1 << (CHAR_BIT - j - 1) ) ? '1' : '0');
}
putchar(' ');
}
return ;
}

int main(void) {

size_t i, j;
float obj = 1234.0;
float obj1 = 1234.0;
unsigned char *pc = (unsigned char* )&obj;
pc += sizeof(obj) - 1;

for (i = 0 ; i < sizeof(obj) ; pc--, i++ ) {
for( j = 0; j < CHAR_BIT ; j++ ) {
putchar( *pc & ( 1 << (CHAR_BIT - j - 1) ) ? '1' : '0');
}
putchar(' ');
}
puts("");

printbin(&obj1);
return 0;
}

This is the output :
01000100 10011010 01000000 00000000
00000000 00011101 11111110 11100100

Why is it so ? It seems very strange.Can anyone please help me
understand whats going on here?

PS: I am using a little-endian processor
Aug 21 '08 #1
4 1322
KellyB wrote:
[...]
void printbin(void* num) {
unsigned char *pc = (unsigned char* )&num;
^^
bogus &. num is a pointer. Remover the '&'
Aug 21 '08 #2
KellyB wrote:
#include <stdio.h>
#include <limits.h>

void printbin(void* num) {
size_t i, j;
unsigned char *pc = (unsigned char* )&num;
pc += sizeof(num) - 1;

for (i = 0 ; i < sizeof(num) ; pc--, i++ ) {
for ( j = 0; j < CHAR_BIT ; j++ ) {
putchar( *pc & ( 1 << (CHAR_BIT - j - 1) ) ? '1' : '0');
}
putchar(' ');
}
return ;
}
That has a major problem,
which is related to the reply that you got from Martin Ambuhl.

As it stands, there is no way to determine
the size of the object that (num) points to.

sizeof(num) equals sizeof(void *).
You need:

#include <stddef.h>
void printbin(void* num, size_t size);

--
pete
Aug 21 '08 #3
pete wrote:
KellyB wrote:
>#include <stdio.h>
#include <limits.h>

void printbin(void* num) {
size_t i, j;
unsigned char *pc = (unsigned char* )&num;
pc += sizeof(num) - 1;

for (i = 0 ; i < sizeof(num) ; pc--, i++ ) {
for ( j = 0; j < CHAR_BIT ; j++ ) {
putchar( *pc & ( 1 << (CHAR_BIT - j - 1) ) ? '1' : '0');
}
putchar(' ');
}
return ;
}

That has a major problem,
which is related to the reply that you got from Martin Ambuhl.

As it stands, there is no way to determine
the size of the object that (num) points to.

sizeof(num) equals sizeof(void *).
You need:

#include <stddef.h>
void printbin(void* num, size_t size);
Aah ..Thanks Martin and Pete . Got it.
Aug 21 '08 #4
KellyB <ke****@gmail.comwrites:
#include <stdio.h>
#include <limits.h>

void printbin(void* num) {
size_t i, j;
unsigned char *pc = (unsigned char* )&num;
The & is wrong. You already have the address.
pc += sizeof(num) - 1;
And the size will be wrong. The size of the print is not the size of
the object.
>
for (i = 0 ; i < sizeof(num) ; pc--, i++ ) {
for ( j = 0; j < CHAR_BIT ; j++ ) {
putchar( *pc & ( 1 << (CHAR_BIT - j - 1) ) ? '1' : '0');
}
putchar(' ');
}
return ;
}

int main(void) {

size_t i, j;
float obj = 1234.0;
float obj1 = 1234.0;
unsigned char *pc = (unsigned char* )&obj;
pc += sizeof(obj) - 1;
Both OK here, though.
for (i = 0 ; i < sizeof(obj) ; pc--, i++ ) {
There is a subtlety about running pointer loops backwards in that you
can't even construct a pointer that points "before" the first byte of
the object (you don't access it, but even constructing it is
technically undefined behaviour). You won't, in practise, experience
a problem though.
for( j = 0; j < CHAR_BIT ; j++ ) {
putchar( *pc & ( 1 << (CHAR_BIT - j - 1) ) ? '1' : '0');
}
putchar(' ');
}
puts("");

printbin(&obj1);
return 0;
}
--
Ben.
Aug 21 '08 #5

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

Similar topics

2
by: Claudio | last post by:
hi all i put in this email source code so u can copy and paste to verify strange first : in this example bit size is a BYTE ?! second : in the last printf output is wrong ? ? best...
24
by: LineVoltageHalogen | last post by:
Greetings All, I was hoping that someone out there has run into this issue before and can shed some light on it for me. I have a stored procedure that essentially does a mini ETL from a source...
8
by: grundmann | last post by:
Hello, i got a strange compiler error. When compiling the following: // forward declarations typedef AvlTree<LineSegment,LineSegmentComperator> LSTree; void handleEventPoint (const...
8
by: Victor Lamberty | last post by:
Greetings C coders I am new to the world of C and have been trying to compile this program. I got the result that I wanted but outputed it in a strange way it put it before the prompt is there a...
6
by: leonecla | last post by:
Hi everybody, I'm facing a very very strange problem with a very very simple C program... My goal should be to write to a binary file some numbers (integers), each one represented as a sequence...
1
by: Martin Feuersteiner | last post by:
Dear Group I'm having a very weird problem. Any hints are greatly appreciated. I'm returning two values from a MS SQL Server 2000 stored procedure to my Webapplication and store them in...
5
by: soeren | last post by:
Hello, two days ago I stumbled across a very strange problem that came up when we were printing tiny double numbers as strings and trying to read them on another place. This is part of an object...
5
by: Ian | last post by:
Hi everyone, I have found some bizarre (to me...!) behaviour of the Form_Activate function. I have a form which has a button control used to close the form and a subform with a datasheet view...
4
by: stat_holyday | last post by:
Greetings. I'm confused. I'm attemting to create dynamic buttons based on the count of questions in a database. The button has 3 states, blank, selected, and inactive. The script below works great,...
2
by: danep2 | last post by:
Hello all This is a really strange problem. I have code that performs a few calculations based on input from a joystick, and writes these values to a file using basically the following code: ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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,...
0
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
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
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,...

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.