472,789 Members | 1,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

unexpected output

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 12662
dev7060
626 Expert 512MB
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
Thanks for replying. the outputs says 4294967293 which is nowhere near what i interpret.
Mar 1 '20 #3
dev7060
626 Expert 512MB
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 Expert 2GB
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
Hi thanks to both. i understand it now :)
Mar 15 '20 #6
decebi
1
int to unsigned int covert by compiler.
Jul 8 '20 #7
donbock
2,426 Expert 2GB
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 Expert Mod 8TB
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 Expert 2GB
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 64KB
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 Bit
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
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...
2
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...
1
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...
2
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...
5
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...
2
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...
7
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
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
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
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

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.