473,806 Members | 2,707 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Signed/unsigned mismatch

18 New Member
Expand|Select|Wrap|Line Numbers
  1. unsigned int  _I = _N;           // _N is of type unsigned int 
  2.  
  3. for (_Grp = false; *_Pg != CHAR_MAX && '\0' < *_Pg
  4.                                          && *_Pg < _I - _Np; _Grp = true)
  5. //*_Pg is constant char

In the above code we are comparing constant char with unsigned int(*_Pg < _I - _Np). We are getting a warning of 'warning C4018: '<' : signed/unsigned mismatch' when compiled with highest warning level in VC++. Can anyone help us to get rid of this warning with necessary explaination (Without using #pragma directives) ???
Feb 14 '07 #1
3 5047
willakawill
1,646 Top Contributor
unsigned int _I = _N; // _N is of type unsigned int

for (_Grp = false; *_Pg != CHAR_MAX && '\0' < *_Pg
&& *_Pg < _I - _Np; _Grp = true)
//*_Pg is constant char


In the above code we are comparing constant char with unsigned int(*_Pg < _I - _Np). We are getting a warning of 'warning C4018: '<' : signed/unsigned mismatch' when compiled with highest warning level in VC++. Can anyone help us to get rid of this warning with necessary explaination (Without using #pragma directives) ???
Is there an earth shattering reason for using the underscore character at the start of your variable names? It reads like a nightlmare and absolutely DOES NOT make you a super cool programmer
An unsigned char is 0 - 255. A signed char is -128 to 127 so clearly they store incompatible values. The compiler recognises this and warns you about it. You can recast one to the other which will stop the warning. It will not stop the errors in transferring incompatible values though.
Feb 14 '07 #2
tcsvikr
18 New Member
Is there an earth shattering reason for using the underscore character at the start of your variable names? It reads like a nightlmare and absolutely DOES NOT make you a super cool programmer
An unsigned char is 0 - 255. A signed char is -128 to 127 so clearly they store incompatible values. The compiler recognises this and warns you about it. You can recast one to the other which will stop the warning. It will not stop the errors in transferring incompatible values though.




I'm sorry.i have not written the code..My task is just to debug the code given to me and remove all warnings at highest level. I got your reasoning. But I don't know the default conversion of char. Will it be converted to signed int or unsigned int.
Feb 15 '07 #3
willakawill
1,646 Top Contributor
I'm sorry.i have not written the code..My task is just to debug the code given to me and remove all warnings at highest level. I got your reasoning. But I don't know the default conversion of char. Will it be converted to signed int or unsigned int.
This will work:
Expand|Select|Wrap|Line Numbers
  1. *_Pg < unsigned char(_I - _Np)
and this will work:
Expand|Select|Wrap|Line Numbers
  1. *_Pg < char(_I - _Np)
insofar as to remove the warning. It is up to you, in the rest of the code to ensure that this value does not exceed 255 or 127 in the second case
Feb 15 '07 #4

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

Similar topics

19
6485
by: MiniDisc_2k2 | last post by:
Okay, here's a question about the standard. What does it say about unsigned/signed mismatches in a comparison statement: char a = 3; unsigned char b = 255; if (a<b) Now what's the real answer here? If a is converted to unsigned, then b>a. But, if b is converted to signed,then a>b. What's the correct coversion (what is the compiler supposed to do?)
27
14910
by: Marcus Kwok | last post by:
I am getting warnings when comparing a (regular) int to the value returned from std::vector.size() in code similar to the following: int i = //stuff if (i >= vec.size()) The compiler gives me a "signed/unsigned mismatch" warning (I am using the C++ compiler found in Visual Studio .NET 2003, which tells me that the return type is a size_t). I get similar warnings when assigning the
2
2138
by: Rahul | last post by:
Hi, I have a little program as follows : =================== STARTS HERE ================ #include <stdio.h> void f (unsigned long); int main() {
10
3315
by: =?iso-8859-2?B?SmFuIFJpbmdvuQ==?= | last post by:
Hello everybody, this is my first post to a newsgroup at all. I would like to get some feedback on one proposal I am thinking about: --- begin of proposal --- Proposal to add signed/unsigned modifier to class declarations to next revision of C++ programming language
7
5052
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 involved. The problem is that comparisons between signed and unsigned values are machine- dependent, because they depend on the sizes of the various integer types. For example, suppose that int is 16 bits
6
6463
by: Kislay | last post by:
Consider the following code snippet unsigned int i=10; int j= - 2; // minus 2 if(i>j) cout<<"i is greater"; else cout<<"j is greater"; Since i is unsigned , j is greater . I know why , but vaguely . Can
0
9719
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
10624
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10371
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
10374
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
10111
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
7650
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
5684
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4330
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
3
3010
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.