Below code outputs garbage value. any idea why so? -
unsigned variable1=1;
-
int variable2=-4;
-
printf("%u",variable1+variable2);
-
11 13381 dev7060 639
Recognized Expert Contributor
Below code outputs garbage value. any idea why so?
What garbage value? Negative numbers are represented in 2's complement form.
Thanks for replying. the outputs says 4294967293 which is nowhere near what i interpret.
dev7060 639
Recognized Expert Contributor
You gotta think the way the machine is processing stuff.
Binary of 4 = 100 , 1's complement representation: 011, 2's complement representation: 100
But, sizeof(int) = 4 bytes = 32 bits which means the machine is processing numbers with respect to 32 bits.
Therefore, -4 is represented as 11111111 11111111 11111111 11111100
which is equivalent to (2^32-1)-3
Hence, variable1+variable2 means ((2^32-1)-3)+1 which results in 4294967293 and is not a garbage value.
donbock 2,426
Recognized Expert Top Contributor
One of the arguments to printf() is an expression that adds an unsigned int variable to an int variable. The compiler uses the usual arithmetic conversions to resolve this type discrepancy before evaluating the expression: the int variable is implicitly cast to unsigned int. dev7060's post explains the rest.
Hi thanks to both. i understand it now :)
int to unsigned int covert by compiler.
donbock 2,426
Recognized Expert Top Contributor
My earlier reply was incomplete. Even if both variable1 and variable2 were ints (making the argument expression int), the %u conversion specifier still causes the argument value to be converted to unsigned int inside printf().
To print the expected value of -3: - Declare variable1 as int.
- Change %u to %d.
Banfa 9,065
Recognized Expert Moderator Expert
An important point here is that there is no relation between the format string and what you place after the format string, some modern compilers check during compilation that what is in the string matches what comes afterwards but the actual code produced doesn't.
You pass a string and then you place some data on the stack by putting parameters after it. This follows some built in rules about default data types (i.e. all integer types smaller than an int are promoted to an int before being placed on the stack and floats are promoted to doubles).
So I can write - printf("Number: %d\n", "Hello World");
and the computer will just give it a go, on my computer outputting donbock 2,426
Recognized Expert Top Contributor
In the old days, a common source of bugs in C was from argument type mismatches between a function call and the function definition. For example, passing a double when you call the function but the function itself expected an int. The introduction of function prototypes with ANSI C in 1989 made that kind of bug much less common -- to the point where it may not occur to you to even look for it. However it can still occur.
For example, the function prototype for printf is: - int printf(const char *fmt, …);
Dot-dot-dot tells the compiler to allow any number of parameters to follow and to allow them to be of any type. Dot-dot-dot is why the compiler doesn't notice when you pass an argument to printf that is not compatible with the format string.
Dot-dot-dot is a time machine that takes you back to 1975.
Hi,
%u is used to print unsingned integer. so variable2is Implicitly converted to unsigned integer and for -4 it is 4294967292 and then add to 1 so ans is 4294967293.
I had the same doubt. Thanks so much.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Steve Holden |
last post by:
For quite a while now I've been filling part of the navigation-bar on my
home page with Python-related news extracted from O'Reilly's meerkat
service.
I've been experiencing intermittent problems for the past few days, and now
suddenly the crontab-triggered script has stopped working completely,
apparently due to a change in what Meerkat...
|
by: Tony Gahlinger |
last post by:
I'm learning/experimenting with some simple JS/html markup, running
an apache daemon and mozilla firefox browser in RH 9.
Let's say I run the following markup with one or more of lines 6-10
commented out:
1 <html>
2 <head>
3 <script type="text/javascript">
4 function showinfo()
|
by: kurt.krueckeberg |
last post by:
The second line of this script
<?php
// current directory
echo getcwd() . "<br />";
print ( exec("ls *.*") );
?>
should display the names of the four files (it does in an ssh session)
which are located in public_html, but instead it displays just one
filename, that of this script.
|
by: Gerhard Esterhuizen |
last post by:
Hi,
I am observing unexpected behaviour, in the form of a corrupted class
member access, from a simple C++ program that accesses an attribute
declared in a virtual base class via a chain of virtual method calls.
To further complicate (or perhaps simplify) matters, some compilers
(GCC and MingW) produce the expected behaviour, while others...
|
by: Tom Lam lemontea |
last post by:
Hi all,
This is my very first post here, I've seriously tried some programming
on C, and shown below is my very first program(So you can expect it to
be very messy) that I wrote after I've learned the basics. However,
the output function I wrote seems to repeat unneedingly for 2 times.
My trial on solving it myself have failed. Anyone willing...
| |
by: Evan Carmi |
last post by:
hi,
i am creating a program to go through a directory structure recursively
(including directories below it) and move all files that end in .msf to
a directory above the current level.
the code i have so far is as follows:
<code>
|
by: Sanchit |
last post by:
My program is
#include <fcntl.h>
#include <unistd.h>
int main(void)
{
int fd;
ssize_t nread;
char buf;
/*open file for reading */
|
by: bintom |
last post by:
I ran the following simple code in C++ and got unexpected results:
float f = 139.4;
cout << f;
Output:
139.399994;
|
by: Ayan Dasgupta |
last post by:
int a=5;
int b=a++ + ++a + a++;
printf("%d",b);
the output for this code is 19
int a=5,b;
b=a++ + ++a + a++;
printf("%d",b);
|
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...
|
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: 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: 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.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |