qualifiers with data type

Thread Starter

anukalp

Joined Jul 28, 2018
158
I am having difficulty to use of qualifiers with data type in c language

There are two types of qualifier

  1. sign : signed and unsigned
  2. size : short and long

<size-sign-data type >
Examples are
Code:
short unsigned int x;
long signed int y;
<sign-size-data type >
Code:
 unsigned int short x;
signed int long  y;
<size-sign-data type > or <sign-size-data type >

Which is correct way to use qualifiers with data type in c programming
 

danadak

Joined Mar 10, 2018
4,057
Quick answer look in header file where data types are defined for your specifc
target.

I always thought integer size to right, its qualifiers to left in the typing. Maybe
an ANSI C expert can answer this one.


Regards, Dana.
 

WBahn

Joined Mar 31, 2012
30,058
<sign-size-data type >
Code:
 unsigned int short x;
signed int long  y;
Wouldn't that be sign-data type-size????

The language standard states that the type specifiers can come in any order, so you can do

int signed short x;

if you want.

By pretty widely-followed convention, the sign specifier goes before the size specifier goes before the type specifier.
 

Thread Starter

anukalp

Joined Jul 28, 2018
158
By pretty widely-followed convention, the sign specifier goes before the size specifier goes before the type specifier.
We have three options sign, size and data type. I don't understand when and which specific combination should be used in programming. I am trying to figure out reason why should that only use in programming.
 

nsaspook

Joined Aug 27, 2009
13,272
We have three options sign, size and data type. I don't understand when and which specific combination should be used in programming. I am trying to figure out reason why should that only use in programming.
Take a look at the C limits.h header. You usually (there are overflow tricks) want all manipulations of X program data to be within the limits of the variable(s) used and optimized for the size of variables used. With embedded programming there are also added considerations of hardware register size, the execution efficiency of bit/byte level manipulations on non-native word sizes and a host of other memory/cpu size/speed factors.

https://www.tutorialspoint.com/c_standard_library/limits_h.htm
 

WBahn

Joined Mar 31, 2012
30,058
We have three options sign, size and data type. I don't understand when and which specific combination should be used in programming. I am trying to figure out reason why should that only use in programming.
There's actually quite a few more modifiers than that. There's also things like 'static', 'extern', 'volatile', 'register', 'const', 'restrict', and 'auto'.

Most of these latter ones are seldom used by casual programmers.

As for the the ones that control sign, size, and data type, those shouldn't be too hard for you to understand and figure out which you can/should use.

You have have two basic primitive data types (ignoring bool, which came along with C99, and pointers): integer and floating point.

If you need values to be exact, integer values, than use an integer data type. If you need or at least can tolerate, values that are not necessarily exact integer values, then you can use a floating point data type.

For integer data types, if you need to work with negative values, then use a signed integer type. If you need to use positive values that are greater than what can be represented by a signed data type, then use an unsigned data type.

For the size, use any size that covers the range of values that you need to work with or, in the case of the floating point types, gives you the precision you need.

For most processors, use an 'int' whenever possible as this is usually the data type that is native to the processor and which executes the fastest and/or results in the smallest code size. That should be your default integer data type and select a modifier when needed because the default 'int' isn't suitable for some reason.

For most applications where you need non-integer values, use a 'double'. This is usually a decent match for most modern processors, but more importantly the 'float' data type, which only has about 7 to 8 sig figs of resolution, is very often too limited and results in round off errors that can be disastrous as well as hard to identify and track down. The 'double' has about 15 sig figs and, for most programs, that is sufficient to avoid these problems.
 

WBahn

Joined Mar 31, 2012
30,058
There are a few odd-ball sizes like the 'short long' at 24-bits that are typically used in DSP audio applications.

http://www.hobbytronics.co.uk/tutorials-code/tutorials-microchip/c18-data-types
Something like that falls under the implementation-defined "extended signed integer types" that is allowed by the standard.

It's actually a clever way to use the provided keywords in a way that is syntactically correct but that most compilers would choke on because it is semantically meaningless to them.

You see a lot of implementation-defined behavior as well as outright violations of the standard when dealing with embedded systems, particularly when targeting the more resource-starved platforms.
 

Thread Starter

anukalp

Joined Jul 28, 2018
158
For integer data types, if you need to work with negative values, then use a signed integer type. If you need to use positive values that are greater than what can be represented by a signed data type, then use an unsigned data type.
okay for 32 bit operation

int n; it indicate that we can store number from - 2147483648 to -2147483647 or 0 to 4294967295

signed int n; same as int n

unsigned int n; it indicate that we can store number from 0 to 4294967295

short int n; it indicate that we can store number from (-32768 to 32767)

short unsigned int n ; same as short int n

short unsigned int n; it indicate that we can store number from 0 to 65536

long int n; it indicate that we can store number from - 2147483648 to -2147483647 or 0 to 4294967295
 

xox

Joined Sep 8, 2017
838
okay for 32 bit operation

int n; it indicate that we can store number from - 2147483648 to -2147483647 or 0 to 4294967295

signed int n; same as int n

unsigned int n; it indicate that we can store number from 0 to 4294967295

short int n; it indicate that we can store number from (-32768 to 32767)

short unsigned int n ; same as short int n

short unsigned int n; it indicate that we can store number from 0 to 65536

long int n; it indicate that we can store number from - 2147483648 to -2147483647 or 0 to 4294967295
Pretty sure all that the standard guarantees is that sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long).

Gcc on my system for example gives me a not a 32-bit but 64-bit long.

In practice I use the following convention:

unsigned char: binary data (eg: typedef unsigned char byte)

char : 8-bit ascii (Forget what the standard says here, a byte is an octet on pretty much any platform)

int: boolean values (ie: typedef int bool)

long: all other integer types

And the reason I like to use 64-bit longs by default is simply due to where they stand with underflows and overflows:

8 bits: trivial

16 bits: easy

32 bits: possible

64-bit: what the hell were you trying to calculate anyway?!!
 

WBahn

Joined Mar 31, 2012
30,058
okay for 32 bit operation

int n; it indicate that we can store number from - 2147483648 to -2147483647 or 0 to 4294967295
No. All integer data types except possibly char are signed unless specified otherwise. So it would be -2,147,483,648 to +2,147,483,647

signed int n; same as int n

unsigned int n; it indicate that we can store number from 0 to 4294967295
Correct.

short int n; it indicate that we can store number from (-32768 to 32767)
Depends on the implementation. That is probably going to be correct on a 32-bit implementation, but all bets are off on a 64-bit implementation.

short unsigned int n ; same as short int n

short unsigned int n; it indicate that we can store number from 0 to 65536
How can you have it both ways? How can it be the same as short in n, which you just claimed could go from -32768 to +32767, and now claim that it can store from 0 to 65536?

Assuming that the short int is 16-bits on a this particular implementation, then the range would be 0 to +65535.

long int n; it indicate that we can store number from - 2147483648 to -2147483647 or 0 to 4294967295
This is very implementation dependent. On some 32-bit implementations, longs are 32-bits and on others they are 64-bits.
 

Thread Starter

anukalp

Joined Jul 28, 2018
158
Assuming that the short int is 16-bits on a this particular implementation, then the range would be 0 to +65535.
This is very implementation dependent. On some 32-bit implementations, longs are 32-bits and on others they are 64-bits.
I just checked on my compiler
Code:
#include<stdio.h>

int main ()
{
    int x1;
    signed int x2;
    unsigned int x3;

    short int x4;
    short signed int x5;
    short unsigned int x6;   
    
    long int x7;
    long signed int x8;
    long unsigned int x9;   
    
    double x10;

    printf("size of x1 : %d \n", sizeof(x1));
    printf("size of x2 : %d \n", sizeof(x2));
    printf("size of x3 : %d \n", sizeof(x3));
    
    printf("size of x4 : %d \n", sizeof(x4));
    printf("size of x5 : %d \n", sizeof(x5));
    printf("size of x6 : %d \n", sizeof(x6));
    
    printf("size of x7 : %d \n", sizeof(x7));
    printf("size of x8 : %d \n", sizeof(x8));
    printf("size of x9 : %d \n", sizeof(x9));
    
    printf("size of x10 : %d \n", sizeof(x10));
    return 0;
}
size of variables
size of x1 : 4
size of x2 : 4
size of x3 : 4
size of x4 : 2
size of x5 : 2
size of x6 : 2
size of x7 : 4
size of x8 : 4
size of x9 : 4
size of x10 : 8
 

Thread Starter

anukalp

Joined Jul 28, 2018
158
How can you have it both ways? How can it be the same as short in n, which you just claimed could go from -32768 to +32767, and now claim that it can store from 0 to 65536?
short signed int indicate it can store largest number +32767

C:
#include<stdio.h>

int main ()
{
    short signed int Number;
    printf("size of Number : %d bytes \n", sizeof(Number));
    printf("Enter the range : ");
    scanf("%d", &Number);

    printf("largest number : %d", Number);
   
    return 0;
}
Experimented output

size of Number : 2 bytes
Enter the range : 32767
largest number : 32767

size of Number : 2 bytes
Enter the range : -32768
largest number : -32768

size of Number : 2 bytes
Enter the range : -32769
largest number : 32767

size of Number : 2 bytes
Enter the range : 65535
largest number : -1
 

Thread Starter

anukalp

Joined Jul 28, 2018
158
short unsigned int indicate it can store largest number +65535

C:
#include<stdio.h>

int main ()
{
    short unsigned int Number;
    printf("size of Number : %d bytes \n", sizeof(Number));
    printf("Enter the range : ");
    scanf("%u", &Number);

    printf("largest number : %u", Number);
    
    return 0;
}
size of Number : 2 bytes
Enter the range : 65535
largest number : 65535
 

WBahn

Joined Mar 31, 2012
30,058
short signed int indicate it can store largest number +32767

C:
#include<stdio.h>

int main ()
{
    short signed int Number;
    printf("size of Number : %d bytes \n", sizeof(Number));
    printf("Enter the range : ");
    scanf("%d", &Number);

    printf("largest number : %d", Number);
  
    return 0;
}
Experimented output

size of Number : 2 bytes
Enter the range : 32767
largest number : 32767

size of Number : 2 bytes
Enter the range : -32768
largest number : -32768

size of Number : 2 bytes
Enter the range : -32769
largest number : 32767

size of Number : 2 bytes
Enter the range : 65535
largest number : -1
Keep in mind that these sizes are for YOU compiler with the selected compiler options (probably the defaults).

Different compilers with have different sizes -- the standard generally sets minimum sizes and ranges and those were set several decades ago.

Always write your code with the expectation that the next time you compile it the size and ranges will be different. If your code needs to count on a variable being a specific size, then use the size-specific typedef types available such as uint16_t, which is typedef'ed to be an unsigned 16-bit integer.
 
Top