473,574 Members | 2,585 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 5170
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
16643
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
5109
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...
20
5330
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
15334
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
5621
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
6440
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
4555
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...
28
19396
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
6392
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...
0
7815
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...
0
7738
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...
0
8077
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. ...
1
7828
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...
0
8113
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...
0
6476
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...
0
5316
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3755
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...
1
2253
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.