473,326 Members | 2,126 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

How to define range for a standard type?

4
I want to override the standard char range. Currently in my compiler, default char is unsigned. i.e 0 to 255. But I want to override its range to -128 to 127. i.e making it as signed. I know I can simply define signed char.

But is there any way to set the value range for a standard type like char, int , short..

I tried
typedef signed char char;
and obviously it failed, saying
error: multiple types in one declaration
error: declaration does not declare anything

Please help me to override the char with signed char.
Jan 20 '11 #1
8 2376
You cannot redefine char as char is a keyword. If u want to define your own types you can use , typedef signed char schar;// for signed char.
Jan 20 '11 #2
navrma
4
Thanks. But say I have defined char varaible in 1000 modules and I dont want to modify all the modules to use schar instead of char.

I want to globally override or change the default char to be signed char.
Jan 20 '11 #3
char is signed by default in most standard compilers like GCC.If You can verify using following code,
int main()
{
char ch=-100;
printf("%d",ch);
return 0;
}

If above code prints different value on console other than -100 , the default char is unsigned. You can check your compiler settings in this case. Most compilers have options to customize default behavior of some data types.
Jan 20 '11 #4
navrma
4
Thanks Vivek. I am using GCC version 3.4.4 for Power PC machine. Default char is unsigned here. I tried compiler options -fsigned-char and -fchar-signed to overcome this issue. But these options are not getting picked up while compiling (its not giving any error and also no impact on the code). Anyway i will dig them more to find the compiler issue, but meanwhile I wanted to know if I can override the default char using software change or compiler option is the only way to fix the issue.
Jan 20 '11 #5
On systems using the POWER/PowerPC family of processors the option -mcpu=CPU selects code generation for specific CPU models. The possible values of CPU include ‘power’, ‘power2’, ‘powerpc’, ‘powerpc64’ and ‘common’, in addition to other more specific model numbers. Code generated with the option -mcpu=common will run on any of the processors.Try this along with -fsigned-char or -fchar-signed options. Hope this will solve your problem.
Jan 20 '11 #6
Savage
1,764 Expert 1GB
In worst case you can use preprocessors #define directive to do so:(not really recomendable,better use compiler switch)

Expand|Select|Wrap|Line Numbers
  1. #define char signed char
Jan 20 '11 #7
weaknessforcats
9,208 Expert Mod 8TB
char is either signed or unsigned based on a compiler setting. You should be able to change this setting.

You define variables as signed char by saying so:

Expand|Select|Wrap|Line Numbers
  1. signed char MyVariable;  //always signed
  2. unsigned char MyNextVariable; //never signed
  3. char MyLastVariable;   //May be signed
Jan 20 '11 #8
donbock
2,426 Expert 2GB
Are you certain that you want to redefine all instances of char in all of the source files in your program?

Type char should only be used for characters.
Small integers should always be defined as signed char or unsigned char.
If you follow this policy then you will never have to worry about whether plain char is signed or unsigned.

I would go further and question why you need a small integer type. Would your program work just as well if you used int?
Jan 21 '11 #9

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

Similar topics

2
by: wwj | last post by:
hello,everyone.I am a new one here.I want to know how to define a bitstring type in c/c++? and can specify its length.Then I can use it like: bitstring a='100111' ;or so. thank you very much
21
by: Angel Lopez | last post by:
Sorry if I am too naive, but this is my first post... I have a binary variable (it can contain either a 0 or a 1). Is there any way I can define a data type that uses only 1 bit. So far I have...
0
by: Marks | last post by:
Can anybody help me find code for more precision (from double) and with highest range (from decimal) arithmetic (division,mul,sub,add) in C#.I need somthing like 1 × 10-1000 with at least 30...
0
by: Marks | last post by:
Can anybody help me find code for more precision (from double) and with highest range (from decimal) arithmetic (division,mul,sub,add) in C#.I need somthing like 1x10-1000 with at least 30...
6
by: Kay | last post by:
Hi all, In vb6, I can define a custom type like this: Private Type uClient sName As String sMonday As Double sMondayHeadCnt As Integer End Type
1
by: Claire | last post by:
"Comparison to integral constant is useless; the constant is outside the range of type 'int'" How can I fix this compiler warning, please, for the following pseudocode? int m_nError; m_nError =...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.