473,516 Members | 3,064 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

unexpected output

3 New Member
Below code outputs garbage value. any idea why so?
Expand|Select|Wrap|Line Numbers
  1. unsigned variable1=1;
  2. int variable2=-4;
  3. printf("%u",variable1+variable2);
  4.  
Mar 1 '20 #1
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.
Mar 1 '20 #2
mariabiz
3 New Member
Thanks for replying. the outputs says 4294967293 which is nowhere near what i interpret.
Mar 1 '20 #3
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.
Mar 1 '20 #4
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.
Mar 8 '20 #5
mariabiz
3 New Member
Hi thanks to both. i understand it now :)
Mar 15 '20 #6
decebi
1 New Member
int to unsigned int covert by compiler.
Jul 8 '20 #7
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:
  1. Declare variable1 as int.
  2. Change %u to %d.
Jul 8 '20 #8
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

Expand|Select|Wrap|Line Numbers
  1.     printf("Number: %d\n", "Hello World");
and the computer will just give it a go, on my computer outputting

Expand|Select|Wrap|Line Numbers
  1. Number: 4210688
Jul 9 '20 #9
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:
Expand|Select|Wrap|Line Numbers
  1. 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.
Jul 9 '20 #10
AjayGohil
83 New Member
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.
Sep 9 '20 #11
sara999898x
1 New Member
I had the same doubt. Thanks so much.
Jan 13 '23 #12

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

Similar topics

0
1648
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...
2
1578
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()
1
2665
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.
2
2158
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...
5
2158
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...
2
1315
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>
7
1529
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 */
13
3574
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;
3
2665
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);
0
7182
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...
0
7408
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
7581
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
7142
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
5714
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
5110
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
3267
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
825
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
488
bsmnconsultancy
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...

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.