473,788 Members | 3,030 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Typecast of one argument to printf impacts other ???

When the below program is compiled and executed,

#include <stdio.h>

#define MASK 0xFFFFFFULL

main() {
unsigned long long a;
unsigned long b, c;

a = 0x12345678ULL;
b = a & MASK;
c = 0x00345678UL;

printf("%d %d\n", sizeof(unsigned long), sizeof(unsigned long
long));
printf("0x%x %lu \t 0x%x %lu \t 0x%x %lu \n",
a & MASK, a & MASK, b, b, c, c);

}

the result from a BigEndian machine is

4 8
0x0 3430008 0x0 3430008 0x345678 3430008

Surprisingly, the result of 'a & MASK' and 'b' are 0x0, when 0x%x
format is used.

And the result from a Little Endian machine is

4 8
0x345678 0 0x345678 0 0x345678 3430008

Again more surprisingly, the results are 0 when %lu is used. Why these
unexpected results ?

Also, when the printf is changed like this,

printf("0x%x %lu \t 0x%x %lu \t 0x%x %lu \n",
(unsigned long)(a & MASK), a & MASK, b, b, c, c);

the result is

0x345678 0 0x345678 3430008 0x345678 3430008

in a Big Endian machine and

0x345678 3430008 0x0 3430008 0x345678 3430008

in a little endian.

And with the below

printf("0x%x %lu \t 0x%x %lu \t 0x%x %lu \n",
(unsigned long)(a & MASK), (unsigned long)(a & MASK), b, b,
c, c);

the results are as expected

0x345678 3430008 0x345678 3430008 0x345678 3430008

in both the machines.

How does typecasting of one argument affect printed values of others ?
Or is there anyother reason behind it and what I see is a misconception
of some hidden fact ?

Nov 15 '05 #1
3 2023

na******@yahoo. co.in wrote:
When the below program is compiled and executed,

#include <stdio.h>

#define MASK 0xFFFFFFULL

main() {
unsigned long long a;
unsigned long b, c;

a = 0x12345678ULL;
b = a & MASK;
c = 0x00345678UL;

printf("%d %d\n", sizeof(unsigned long), sizeof(unsigned long
long));
printf("0x%x %lu \t 0x%x %lu \t 0x%x %lu \n",
a & MASK, a & MASK, b, b, c, c);
For the first vararg parameter, you're passing an unsigned long long
when the corresponding format specifier, %x, expects an unsigned int.
For the second vararg parameter, you're passing an unsigned long long
when %lu expects an unsigned long. Mismatching format specifiers with
the parameter types is *undefined behavior*. The compiler is free to do
ANYTHING IT WISHES.

However, I suppose you want a practical, "what is most-likely happening
behind the scenes" answer. Okay, here is the most-likely reason:

In a typical implementation, when you call printf, the parameters are
pushed onto the stack from right to left, and so, your stack looks like
this:

(separated into 4-byte groups -- big endian)
00345678 c -- bottom of stack
00345678 c
00345678 b
00345678 b
00345678 a & MASK (least significant 4 bytes)
00000000 (most significant 4 bytes)
00345678 a & MASK
00000000
ptr_to_format_s tring -- top of stack (popped first)

And so, printf pops off the pointer to the format string and starts
parsing the format string. It encounters an %x, which means to display
the *unsigned int* parameter in hexadecimal format. And so, since an
unsigned int is likely to be 4 bytes on your platform, 4 bytes are
popped off the stack, and so 00000000 is popped and represented in
hexadecimal. Then %lu is encountered and since it expects an unsigned
long, which is 4 bytes on your platform, 4 bytes are popped, which is
00345678 in hex, or 3430008 in decimal. etc.

Solution: use the right format specifiers: %llx and %llu
}

the result from a BigEndian machine is

4 8
0x0 3430008 0x0 3430008 0x345678 3430008

Surprisingly, the result of 'a & MASK' and 'b' are 0x0, when 0x%x
format is used.

And the result from a Little Endian machine is

4 8
0x345678 0 0x345678 0 0x345678 3430008

Again more surprisingly, the results are 0 when %lu is used. Why these
unexpected results ?

Also, when the printf is changed like this,

printf("0x%x %lu \t 0x%x %lu \t 0x%x %lu \n",
(unsigned long)(a & MASK), a & MASK, b, b, c, c);

the result is

0x345678 0 0x345678 3430008 0x345678 3430008

in a Big Endian machine and

0x345678 3430008 0x0 3430008 0x345678 3430008

in a little endian.

And with the below

printf("0x%x %lu \t 0x%x %lu \t 0x%x %lu \n",
(unsigned long)(a & MASK), (unsigned long)(a & MASK), b, b,
c, c);

the results are as expected

0x345678 3430008 0x345678 3430008 0x345678 3430008

in both the machines.

How does typecasting of one argument affect printed values of others ?
Or is there anyother reason behind it and what I see is a misconception
of some hidden fact ?


Nov 15 '05 #2
On 15 Oct 2005 05:19:02 -0700, na******@yahoo. co.in wrote:
When the below program is compiled and executed,

#include <stdio.h>

#define MASK 0xFFFFFFULL

main() {
unsigned long long a;
unsigned long b, c;

a = 0x12345678ULL;
b = a & MASK;
c = 0x00345678UL;

printf("%d %d\n", sizeof(unsigned long), sizeof(unsigned long
long));
sizeof evaluates to a size_t which is an unsigned integer of some
type. %d requires an int so you have potential undefined behavior
here. A common recommendation is to use %lu and cast the sizeof to
unsigned long.
printf("0x%x %lu \t 0x%x %lu \t 0x%x %lu \n",
a & MASK, a & MASK, b, b, c, c);
Here have more undefined behavior.

%x requires an unsigned int. a & MASK is an unsigned long long.

%lu requires an unsigned long. a & MASK is still an unsigned long
long.

%x requires an unsigned int. b is an unsigned long. So is c.

}

the result from a BigEndian machine is

4 8
0x0 3430008 0x0 3430008 0x345678 3430008

Surprisingly , the result of 'a & MASK' and 'b' are 0x0, when 0x%x
format is used.
You cannot lie to printf and expect things to work, especially since
you know that unsigned long and unsigned long long have different
sizes.

And the result from a Little Endian machine is

4 8
0x345678 0 0x345678 0 0x345678 3430008

Again more surprisingly, the results are 0 when %lu is used. Why these
unexpected results ?
You cannot lie to printf and expect things to work, especially since
you know that unsigned long and unsigned long long have different
sizes.

Also, when the printf is changed like this,

printf("0x%x %lu \t 0x%x %lu \t 0x%x %lu \n",
(unsigned long)(a & MASK), a & MASK, b, b, c, c);

the result is

0x345678 0 0x345678 3430008 0x345678 3430008

in a Big Endian machine and

0x345678 3430008 0x0 3430008 0x345678 3430008

in a little endian.

And with the below

printf("0x%x %lu \t 0x%x %lu \t 0x%x %lu \n",
(unsigned long)(a & MASK), (unsigned long)(a & MASK), b, b,
c, c);

the results are as expected

0x345678 3430008 0x345678 3430008 0x345678 3430008

in both the machines.

How does typecasting of one argument affect printed values of others ?
Or is there anyother reason behind it and what I see is a misconception
of some hidden fact ?


Welcome to the wonderful world of undefined behavior. One
manifestation is "do something to really confuse the user."
<<Remove the del for email>>
Nov 15 '05 #3
na******@yahoo. co.in wrote:
When the below program is compiled and executed,

/* Your program is hopelessly broken. Learn to use specifiers that
match your data types. Also learn that sizeof() returns an unsigned
value. Note the changes below. As a style isssue, using tabs for
output meant to be human-readable is a mistake. All of your
questions are related to your errors. */
#include <stdio.h>

#define MASK 0xFFFFFFULL

int main(void)
{
unsigned long long a;
unsigned long b, c;

a = 0x12345678ULL;
b = a & MASK;
c = 0x00345678UL;

printf("%d %d\n", (int) sizeof(unsigned long),
(int) sizeof(unsigned long long));
printf("%#llx %llu; %#lx %lu; %#lx %lu\n", a & MASK, a & MASK, b,
b, c, c);
return 0;

}
[output]

4 8
0x345678 3430008; 0x345678 3430008; 0x345678 3430008

#include <stdio.h>

#define MASK 0xFFFFFFULL

main() {
unsigned long long a;
unsigned long b, c;

a = 0x12345678ULL;
b = a & MASK;
c = 0x00345678UL;

printf("%d %d\n", sizeof(unsigned long), sizeof(unsigned long
long));
printf("0x%x %lu \t 0x%x %lu \t 0x%x %lu \n",
a & MASK, a & MASK, b, b, c, c);

}

Nov 15 '05 #4

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

Similar topics

5
2093
by: Lars Plessmann | last post by:
I have a problem with typecast methods. Here is a setter of an object: function setKind($kind) { if (is_int((int)$kind) and (strlen($kind)<=6)) { $this->kind = $kind; return true;
9
11836
by: Venkat | last post by:
Hi All, I want to typecast int to std::string how can i do it. Here is the sample code. int NewList; //Fill the NewList with integers values. .......
1
2226
by: masood.iqbal | last post by:
I have a few questions regarding overloaded typecast operators and copy constructors that I would like an answer for. Thanks in advance. Masood (1) In some examples that I have seen pertaining to casting class A to class B, the implementation of the
8
5336
by: Trishia Rose | last post by:
this is something ive always wondered, does it take cpu time at run time to typecast or just at compile time? for example consider the two little bits of code: int a = 5; int b = a; and: char *a = (char*) 5;
4
4185
by: Michael Mair | last post by:
Hi there, actually, I have posted the same question in g.g.help. As there were no answers, I am still not sure whether this is a bug or only something open to the compiler that is seemingly inconsistent or whether my understanding of C is not complete enough. I would appreciate answers or pointers to answers very much.
3
3242
by: Ray Mitchell | last post by:
Hello, I have an array list whose elements are the following types: float int float int ....etc.
5
3003
by: shaanxxx | last post by:
i have statements printf("%f",(double)1); /* works fine although i have sent %f for double */ printf("%c",(int)64); /* works fine although i have sent %c for int */ Need your comment on this. As my understanding, when we pass something on variable argument, its type will be promoted . char is promoted to Int . float is promoted to
7
8135
by: bowlderster | last post by:
Hello,all. I want to get the array size in a function, and the array is an argument of the function. I try the following code. /*************************************** */ #include<stdio.h> #include<stdlib.h> #include<math.h>
8
2302
by: A. Anderson | last post by:
Howdy everyone, I'm experiencing a problem with a program that I'm developing. Take a look at this stack report from GDB - #0 0xb7d782a3 in strlen () from /lib/tls/i686/cmov/libc.so.6 #1 0xb7d4c2f7 in vfprintf () from /lib/tls/i686/cmov/libc.so.6 #2 0xb7d6441b in vsprintf () from /lib/tls/i686/cmov/libc.so.6 #3 0x08049ba0 in character_data::printf (this=0x800, argument=0x0) at character.c:198
0
9655
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9498
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10172
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9964
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7517
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6749
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4069
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 we have to send another system

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.