Dear C people,
C90 doesn't specify if 'char' is either 'signed char' or 'unsigned char'
leaving it to the implementation of the compiler. Has this changed for
C99, or is it still the same?
Thanks,
Jason.
Nov 14 '05
11 2977
Malcolm wrote: "Jason Curl" <j_********@mot orola.com> wrote
01 #include <stdio.h> 02 int ctest(char s) 03 { 04 if (s < 0 || s >= 128) { 05 printf("Out of range\n"); 06 return 0; 07 } else { 08 printf("In range\n"); 09 return 1; 10 } 11 }
file.c:4: warning: comparison is always false due to limited range of data type
and I want to know if the compiler is generating a useless diagnostic in this case (and I ignore it, or figure out how to turn it off), or if it is a problem with my code.
It's a sort of useless diagnostic. What it is complaining about is that char can never go above 128 (assuming signed 8 bit chars). It may also dislike comparing an unsigned for less than zero (assuming unsigned chars). It doesn't ahve the intelligence to realise that the test is potentially meaningful on a different machine.
The way I would get rid of this is to convert s to an int. Make the function take an integer as an argument. This is traditional in C; fgetc() and fputc() work this way, for instance.
Not quite. I don't actually care about the text itself as humans don't need to read it, and I'm treating it literally as a string of random values. The range of the values concerns me as I'm using an array of structures that's indexed by the character value itself. I'm defining that array to be 128 elements and check that the 'char' is in the range of 0 to 127 (for a Posix system) before using it to index that char.
And of course, the actual size of a char is defined to be 8 bits (or more, but 8 in Posix), so I interpret this as I must check the 8th bit somehow.
Using a char for indexing is possibly one of the exceptions to the rule that the machine representation is irrelevant. Are you sure you want 128 buckets indexed by ascii value and that the program might not be clearer, if a little slower, if you indexed alphabetically on alphanumeric values? Remember the program may break if you move to a non-ascii system.
Thanks for your input. You're right that it will break if it moves away
from ASCII systems (e.g. EBCDIC is spread out across 8 bits). I have to
find out though if POSIX allows for different char sets.
<OT>
The actual characters themselves are byte sequences from a terminal
(e.g. VT100). I then receive a sequence of bytes and use this method to
determine what should be done when a key is pressed. Hence for non-ascii
systems this would be a different character string - the byte sequences
remain the same but representation in the C source would have to change.
</OT> It seems a little waste of CPU cycles/code clarity to take the value, typecast it to int (or assign it to an int), make the comparison, then index the array with the int as to avoid the warning.
Probably no code will be executed. The character is held in an integer wide register, and if treated as a char variable the top bits are ignored.
Jason Curl wrote: Malcolm wrote: "Jason Curl" <j_********@mot orola.com> wrote
C90 doesn't specify if 'char' is either 'signed char' or 'unsigned char' leaving it to the implementation of the compiler. Has this changed for C99, or is it still the same?
char may be signed or unsigned, but this is a hangover from K and R and
That's what I originally thought. But there was another post that suggested the defined range of char is 7 bits (0-127),
That's the minimum guaranteed range of char.
It's the intersection of the minimum guaranteed ranges
of signed char and unsigned char.
and in newer standards is different to 'signed char' and 'unsigned char'.
There's three types of char:
1 char
2 signed char
3 unsigned char
Those are three different types.
01 #include <stdio.h> 02 int ctest(char s)
Use
int ctest(int s);
instead.
--
pete This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: wwj |
last post by:
Hi ,all
I want to know the difference between char a and char *p=new
char and the difference between the heap and the stack ,and if the
char a is corresponding to the stack in MEMORY,and char *p=new
char is corresponding to the heap of MEMORY.
Give me some hint.
THANK YOU.
|
by: David |
last post by:
what's the differences between:
int main(int argc,char* argv){
...
}
and:
int main(int argc,char** argv){
...
|
by: wwj |
last post by:
Hi ,all
I want to know the difference between char a and char *p=new
char and the difference between the heap and the stack ,and if the
char a is corresponding to the stack in MEMORY,and char *p=new
char is corresponding to the heap of MEMORY.
Give me some hint.
THANK YOU.
|
by: Hal Styli |
last post by:
Hello,
can anyone explain the difference between the above declarations and why
anyone would stray from the first.
Are the other 2 standard, couldnt find them in K&R2.
Thanks,
Hal.
|
by: jab3 |
last post by:
(again :))
Hello everyone.
I'll ask this even at risk of being accused of not researching adequately.
My question (before longer reasoning) is: How does declaring (or defining,
whatever) a variable **var make it an array of pointers?
I realize that 'char **var' is a pointer to a pointer of type char (I hope).
And I realize that with var, var is actually a memory address (or at
| |
by: tinesan |
last post by:
Hello fellow C programmers,
I'm just learning to program with C, and I'm wondering what the
difference between signed and unsigned char is. To me there seems to
be no difference, and the standard doesn't even care what a normal char
is (because signed and unsigned have equal behavior).
For example if someone does this:
unsigned char a = -2; /* or = 254 */
|
by: T Koster |
last post by:
Hi group,
I'm having some difficulty figuring out the most portable way to read 24
bits from a file. This is related to a Base-64 encoding.
The file is opened in binary mode, and I'm using fread to read three
bytes from it. The question is though, where should fread put this? I
have considered two alternatives, but neither seem like a good idea:
In most cases, the width of a char is 8 bits, so an array of 3 chars
|
by: arnuld |
last post by:
i am trying to understand arrays and char. Stroustrup says, this a
string literal:
"this is a string literal"
he says it is of type /const char/. he also says, because of keeping
compatibilities with previous definitions of C & C++, it is also a
CHAR POINTER a.k.a /char*/. however it is an error, which can not be
caught at untill runtime, to modify a string literal using such
pointer:
|
by: rajm2019 |
last post by:
hi all,
i want to know that what is the actual difference b/w the character
array
& character pointer.then how u will get the addrees of a char array
char str="be silent like u"
char *p1="be eloquent r u"
char *p2;
p2=str;
|
by: amphetaman |
last post by:
Is there a safe (unlikely to cause overflows or segfaults) way to load
a text file into a char ** array? I thought of using getline, but it
needs a fixed-length string, and I don't know how many lines or
characters per line the file has.
|
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: 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,...
|
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: 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...
|
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: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |