How to define range for a standard type?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • navrma
    New Member
    • Mar 2008
    • 4

    #1

    How to define range for a standard type?

    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.
  • vivek nandan
    New Member
    • Jan 2011
    • 9

    #2
    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.

    Comment

    • navrma
      New Member
      • Mar 2008
      • 4

      #3
      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.

      Comment

      • vivek nandan
        New Member
        • Jan 2011
        • 9

        #4
        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.

        Comment

        • navrma
          New Member
          • Mar 2008
          • 4

          #5
          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.

          Comment

          • vivek nandan
            New Member
            • Jan 2011
            • 9

            #6
            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.

            Comment

            • Savage
              Recognized Expert Top Contributor
              • Feb 2007
              • 1759

              #7
              In worst case you can use preprocessors #define directive to do so:(not really recomendable,be tter use compiler switch)

              Code:
              #define char signed char

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                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:

                Code:
                signed char MyVariable;  //always signed
                unsigned char MyNextVariable; //never signed
                char MyLastVariable;   //May be signed

                Comment

                • donbock
                  Recognized Expert Top Contributor
                  • Mar 2008
                  • 2427

                  #9
                  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?

                  Comment

                  Working...