473,394 Members | 1,828 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,394 software developers and data experts.

Unsigned Integer mystery

Hello all,
how are you?

let me put a code snippet here. I used microsoft vc++ 6.0 on my windows xp machine.

/**********************************
Expand|Select|Wrap|Line Numbers
  1. unsigned int x1 = 10;
  2. unsigned int y1 = 20;
  3.  
  4. printf("\n (x1-y1) = %d",(x1-y1));  //prints -10
  5.  
  6. if((x1-y1) < 0)
  7.   printf("\n(x1 - y1) < 0");
  8. else
  9.  printf("\n(x1 - y1) > 0"); //prints (x1 - y1) > 0
  10.  
*************************************/

Can any one please tell me what happens when we write
printf ("\n (x1-y1) = %d",(x1-y1)); ?

Does compiler create any temporary variable and assigns value of (x1-y1) to it?

if yes, this temporary variable would be signed int by default ?
if no, then why does it print (x1 - y1) > 0 ?

Thank you in advance,
Sanket
Feb 24 '08 #1
5 1783
Ganon11
3,652 Expert 2GB
When you use the %d format specifier, you are telling printf to print that value as if it were a signed integer. Later, when your if statement checks x1-y1 (or y1-x1), you aren't telling the program to treat them as anything, so it treats the result as an unsigned integer - which cannot be less than 0.
Feb 24 '08 #2
When you use the %d format specifier, you are telling printf to print that value as if it were a signed integer. Later, when your if statement checks x1-y1 (or y1-x1), you aren't telling the program to treat them as anything, so it treats the result as an unsigned integer - which cannot be less than 0.
Thank you Ganon11.

I got the point. That means When we compare them or use them in any expression, it will be treated as unsigned. as an example

Expand|Select|Wrap|Line Numbers
  1. unsigned int x1;
  2. for(x1 = 10; x1>=0; x1--){
  3.         printf("\nx1 = %d  : ",x1);
  4.     }
  5.  
This for loop will never end as x1 is unsigned and it wont be less than 0.
m I going right way ?

Thank you,
Sanket
Feb 24 '08 #3
Ganon11
3,652 Expert 2GB
I'm pretty sure you are correct.
Feb 24 '08 #4
I'm pretty sure you are correct.
Thank you so much for clearing my concepts.
Feb 24 '08 #5
weaknessforcats
9,208 Expert Mod 8TB
Of course s1 can be negative. If it contains 0 and you --x, you have -1.

Unfortunately, the sign bit is part of the data so you now have a really big positive number.

That's why the loop runs forever. It should be for(x1 = 10; x1>0; x1--) to avoid the run-on.
Feb 24 '08 #6

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

Similar topics

34
by: Andy | last post by:
Hi, Are 1 through 4 defined behaviors in C? unsigned short i; unsigned long li; /* 32-bit wide */ 1. i = 65535 + 3; 2. i = 1 - 3; 3. li = (unsigned long)0xFFFFFFFF + 3; 4. li = 1...
10
by: Vijay Kumar R Zanvar | last post by:
Hi clc, Please elaborate the warning: F:\Vijay\C> type unsigned2.c #include <stdio.h> #include <stdlib.h> int
12
by: Peter Ammon | last post by:
When I add an unsigned long long and an int, what type do each of the values get promoted to before the addition is performed? What is the type of the resulting expression? What occurs if the...
9
by: luke | last post by:
Hi everybody, please, can someone explain me this behaviour. I have the following piece of code: long long ll; unsigned int i = 2; ll = -1 * i; printf("%lld\n", ll);
2
by: Christian Christmann | last post by:
Hi, what does the ANSI C-99 standard says when an unsigned variable is assigned a negative number?, i.e. unsigned char a = -100; Is this undefined behavior or should the negative number...
4
by: techie | last post by:
I have defined a number of unsigned integer types as follows: typedef unsigned char uint8; typedef unsigned short uint16; typedef unsigned int uint32; typedfe long long uint64; Is it...
10
by: Jim Langston | last post by:
Is the following well defined? size_t IntVal = 65537; unsigned short Length; if ( IntVal static_cast<unsigned short>( -1 ) ) { std::cout << "Value too long to fit in a short" << std::endl;...
24
by: Paulo Matos | last post by:
Hello, Is it safe to assume a size_t is an unsigned long? (is it forced by the standard?) Thank you, Paulo Matos
7
by: somenath | last post by:
Hi All, I am trying to undestand "Type Conversions" from K&R book.I am not able to understand the bellow mentioned text "Conversion rules are more complicated when unsigned operands are...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.