What is the difference between char and signed char, short and unsigned
short, int and signed int, long and unsigned long? 4 1876
BigMan wrote: What is the difference between char and signed char, short and unsigned short, int and signed int, long and unsigned long?
Well, obviously, the range of values that they represent is different.
Signed types can represent negative numbers whereaas unsigned types use
the same bit patterns to represent large positive numbers.
But of course C and C++ don't have any run-time checks to stop you from
subtracting 1 from an unsigned 0. So it is tempting to think that the
types are not actually any different since in many common cases, e.g.
when you are doing addition and subtraction, the bit patterns will end
up the same whatever the types.
But there are plenty of places where it does matter. For example, when
you do formatted input/output, conversions (e.g. assign a shorter type
to a longer type - is it sign-extended?), comparisons (is 11111111
greater or less than 00000000?), and so on.
Using the correct types also helps the compiler to give you sensible
warnings.
--Phil.
Phil Endecott wrote: But of course C and C++ don't have any run-time checks to stop you from subtracting 1 from an unsigned 0.
Nor should they, since the result is well-defined.
--
Pete Becker
Dinkumware, Ltd. ( http://www.dinkumware.com)
Sorry! What I meant was if there is a difference between char and
signed char, short and signed short, int and signed int, long and
signed long. I.e. does int (or char, short, long) means signed int (or
signed char, signed short, signed long respectively)?
On 12 Jun 2005 11:18:20 -0700, "BigMan" <Bi****@abv.b g> wrote: What is the difference between char and signed char, short and unsigned short, int and signed int, long and unsigned long?
char, signed char and unsigned char are special cases: they are three
separate types. Most compilers will have a compile parameter enabling
you to influence how plain char is treated -- either as signed or
unsigned. But plain char is a distinct type.
signed int and int, OTOH, are synonymous, as are signed long and long,
signed short and short, etc. IOW, integral types are signed unless you
write unsigned.
--
Bob Hairgrove No**********@Ho me.com This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Sidney Cadot |
last post by:
Hi all,
Just browsing through my newly-acquired C99 spec, I was reading on the
memcmp() function (7.21.4.1). It has a rather peculiar wording:
int memcmp(const void *s1, const void *s2, size_t n);
1. The memcmp function compares the first n characters of the object
pointed to by s1 to the first n characters of the object pointed to by s2.
|
by: Ken Tough |
last post by:
Seems like a simple thing to find out, but I'm struggling. I have
googled, but everything I find is about implicit conversion,
not explicit.
Is this implementation-specific, or does ANSI/ISO lay out what
should happen for:
--------------------------
signed char sc;
unsigned char uc;
|
by: Matt |
last post by:
I have 2 questions:
1. strlen returns an unsigned (size_t) quantity. Why is an unsigned
value more approprate than a signed value? Why is unsighned value less
appropriate?
2. Would there be any advantage in having strcat and strcpy return a
pointer to the "end" of the destination string rather than returning a
|
by: Randy Yates |
last post by:
In Harbison and Steele's text (fourth edition, p.111)
it is stated,
The C language does not specify the range of integers that the
integral types will represent, except ot say that type int may not
be smaller than short and long may not be smaller than int.
They go on to say,
Many implementations represent characters in 8 bits, type short in
|
by: Sunil |
last post by:
Hi all,
I am using gcc compiler in linux.I compiled a small program
int main()
{
printf("char : %d\n",sizeof(char));
printf("unsigned char : %d\n",sizeof(unsigned char));
printf("short : %d\n",sizeof(short));
printf("unsigned short : %d\n",sizeof(unsigned short));
printf("int : %d\n",sizeof(int));
| |
by: dhnriverside |
last post by:
Hi guys
I'm using Microsoft Word from asp.net, using VB Script CreateObject etc to
load Word on the client.
It keeps coming up with the "run active x control" dialog, saying that
MSWord is not signed.
2 questions... why is it not signed? I'd have though MS would sign all its
ActiveX controls? and is there anyway to prevent the dialog while maintaining
|
by: jacob navia |
last post by:
Hi
Suppose you have somewhere
#define BOOL int
and somewhere else
typedef BOOL int;
|
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
|
by: John Harrison |
last post by:
I have a problem. I want to compare an integral value, n, against three
half open ranges as follows
[-A, 0) // range 1
[0, B) // range 2
[B, C} // range 3
Each range corresponds to a different outcome and if the integral value
isn't within any of the ranges, that's a fourth outcome. So far so easy,
the problem is that n is a signed quantity and A, B, C are unsigned
|
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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...
| |
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...
|
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...
|
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 launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
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
|
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |