473,659 Members | 2,980 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

unsigned short short?

I encountered a problem after we had converted our declarations of
'unsigned short int' to uint16_t. In one instance, whoever did the
conversion failed to delete the 'short' keyword so we had a 'uint16_t
short'. The compiler (GNU 3.3.5) allows this but ignores the original
unsigned keyword and initialzes the variable as a signed short int. I
created a very simple test program just to see what was happening:

#include <stdio.h>

typedef unsigned short my_int16;

int main(int argc, char *argv[]) {

short s = -1;
unsigned short us = -1;
my_int16 my = -1;
my_int16 short mys = -1;

printf("%d, %d, %d, %d\n", s, us, my, mys);
return 0;
}
output is -1, 65535, 65535, -1

So my "unsigned short short" has a value of -1. The compiler gives a
warning for the declaration of 'us' and 'my' since I'm initializing an
unsigned int to a negative value, but it has no problem with my
declaration of an 'unsigned short short'?

Should this be a warning/error? Why is this allowed?

steve

By the way, I quickly googled this error and I know that I'm not the
only one who has declared a uint16_t short, a few other people will
also see some odd behaviour if they're expecting an unsigned value.

Oct 16 '06 #1
4 5184
sl*******@gmail .com wrote:
I encountered a problem after we had converted our declarations of
'unsigned short int' to uint16_t. In one instance, whoever did the
conversion failed to delete the 'short' keyword so we had a 'uint16_t
short'. The compiler (GNU 3.3.5) allows this but ignores the original
unsigned keyword and initialzes the variable as a signed short int. I
created a very simple test program just to see what was happening:
It's technically ill-formed code. Unlike redundant const/volatile via
a typedef, there isn't such a thing for short.
>
So my "unsigned short short" has a value of -1. The compiler gives a
warning for the declaration of 'us' and 'my' since I'm initializing an
unsigned int to a negative value,
Initialializing an unsigned with -1 is valid however.
Oct 16 '06 #2
Ron Natalie posted:
Unlike redundant const/volatile via a typedef, there isn't such a thing
for short.
Please elaborate on that. The "const" in the following typedef is certainly
not redundant.

typedef int const cint;

int main()
{
cint i = 7;

i = 4; /* Opps! */
}

Nor is the "volatile" in the following typedef:

typedef int volatile vint;

int main()
{
vint i = 7;

int *p = &i; /* Opps! */
}

--

Frederick Gotham
Oct 16 '06 #3
Frederick Gotham wrote:
Ron Natalie posted:
>Unlike redundant const/volatile via a typedef, there isn't such a thing
for short.

Please elaborate on that. The "const" in the following typedef is certainly
not redundant.
I'm talking about the following:

int const const foo; // ILL-FORMED
typedef int const cint;
const cint goo; // LEGAL, the redundant const
// from the typedef is allowed.
short short int foo; // ILL-FORMED
typedef short int sint;
short sint foo; // ILL-FORMED, no exemption for repeated
// short like there is for const
Oct 16 '06 #4
sl*******@gmail .com wrote:
Should this be a warning/error? Why is this allowed?
You didn't tell your tool to behave like an ANSI/ISO conforming
implementation of C++.

Most C and C++ compilers enable their extensions by default. This means
that they can misinterpret or even reject some strictly conforming
programs, and fail to diagnose programs which require a diagnostic.

For any compiler you happen to be using, it behooves you to find out
how to make it behave in a conforming way.

With gcc, there are two flags for this: -ansi to disable the
non-conforming extensions, and -pedantic to request all required
diagnostics.

I have an installation of GCC 3.4.3 here.

The C front end, even without -ansi or -pedantic, diagnoses "uint16_t
short x" with this message:

error: long, short, signed or unsigned used invalidly for `x'

The C++ front end from 3.4.3, however, is silent, unless given
-pedantic, in which case it yields the same message.

Oct 16 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

34
16664
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 - 3;
16
5117
by: TTroy | last post by:
Hello, I'm relatively new to C and have gone through more than 4 books on it. None mentioned anything about integral promotion, arithmetic conversion, value preserving and unsigned preserving. And K&R2 mentions "signed extension" everywhere. Reading some old clc posts, I've beginning to realize that these books are over-generalizing the topic. I am just wondering what the difference between the following pairs of terms are: 1)...
20
5342
by: Hanzac Chen | last post by:
Hi, I don't understand why this could happen? The Code 1 will output `fff9' and the Code 2 will output `1' How could the `mod 8' not have effect? /* Code 1 */ #include <stdio.h> #include <stdlib.h>
4
15343
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 necessary to explicitly cast from one type of unsigned integer type to another even though they do so implicitly?
10
5628
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; } else
6
6445
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
2
4564
by: sam.barker0 | last post by:
Hi guys, I am trying to form an IPV6 address string from the address bytes contained in a unsigned char buffer char tempstring; sprintf(tempstring, "%x:%x:%x:%x:%x:%x:%x:%x",htons(*((unsigned short *)(buf.GetStart()))),htons(*((unsigned short *)(buf.GetStart() +2))),htons(*((unsigned short *)(buf.GetStart()+4))),htons(*((unsigned short *)(buf.GetStart()+6))),htons(*((unsigned short *)(buf.GetStart() +8))),htons(*((unsigned short...
28
19406
by: Fore | last post by:
Hello I am looking for some effecient way to convert a 32 bit unsigned integer to a 16 bit signed integer. All I want is the lower 16 bits of the 32 bit unsigned integer , with bit 15 (0..15) to used as the sign bit for the 16 bit signed integer. Any ideas/help greatly appreciated. Thanks.
3
6396
by: mathieu | last post by:
Could someone please tell me what is wrong with the following -ugly- piece of c++ code. Why when I explicititely set the template parameter my gcc compiler start getting confused: bla.cxx: In function 'int main()': bla.cxx:25: error: call of overloaded 'foo(short unsigned int*&)' is ambiguous bla.cxx:2: note: candidates are: void foo(OutputType*) bla.cxx:10: note: void foo(PixelType*)
0
8428
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
8335
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,...
0
8747
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
8528
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
8627
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...
0
7356
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6179
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
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2752
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 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.