Tuesday, April 1, 2008

Data Types and Data Modifiers in C

Data types in C

A computer program deals with data. It is important for a programmer to understand how data is represented in a program. A datatype is generally a key word that represents a certain type of data. Whenever a new version comes into the market, changes are likely to be incorporated in data types to suit growing needs of the software industry. Always check documentation for details.

  • Basic datatypes are keywords
  • Composite or derived data types or user defined datatypes are data structures that can be used to represent complex data types like arrays, structures etc. They consist of one or more basic data types.

C has 5 basic data types:-

void

char

int

float

double

char:- A variable can be declared as a char if we know that it is going to hold only a single character or a small numeric value.

e.g. char gender;

Here we know that the variable gender may be either ‘f’ , ‘m’, ‘F’, ‘M’.

int:- A variable may be declared as an int if we know that it has to take only integral values 1, 2, 3, …(positive, negative, zero) within a limited range.

e.g. int emp_number ;

Here employee numbers take only integers within a specific range.

float:- A variable may be declared as a float if we feel it may take fractional values.

e.g. float bill_value ;

Here the variable bill_value may be fraction.

double:- It is same as a float, only capable of larger [1]precision and range.

e.g. double n ;

Here the variable n may take a very large or very small value. We may also require a high degree of precision.

void:- void means no space requirement. It is illegal to declare a data type as void. The void data type is useful in functions and pointers.

Size & Range of a Data Type

· Every data type is characterized by 2 main features – size and range.

· Size: Variables are stored in memory. How many bytes are allocated for it in memory? This is referred to as the size of that data type. Size is version dependent.

· Range: What is the range of values that a variable of that type can take? The range depends on the size.

e.g.1: char ch ;

This declaration means:-

· ‘ch‘ is a character data type

· 1 byte is set aside in memory

· ch can take values from -128 to 127.

· Label of the memory location is ‘ch’

· It has an address. Address of the location of ‘ch’ is given by &ch


e.g.2 int k ;

This declaration means:-

· k is an integer data type

· 2 bytes are set aside in memory

· k can take values from -32768 to 32767.

· Label of the memory location is k

· It has an address. Address of the location of k is given by &k.

A programmer has to understand data types, their sizes and ranges. The size and range may change with versions of the C compiler.

Data Type

No. of bytes

Allocated

( Size )

Range of Values

Signed

Unsigned

Char

1

-128 to 127

0 to 255

Int

2

-32768 to 32767

0 to 65535

Float

4


Double

8


Note:-

· Floating point numbers include fractions also. You have known them as set of real numbers.

· Integers cannot have fractional values. They can only hold whole numbers i.e., integers are positive, negative and zero.


Difference between float & double:-

A double is known as a high precision number. A float can have a maximum of 6 digit scaling while a double can have a maximum of 10 digit scaling. Scaling refers to the accuracy with which digits to the right of the decimal point is represented. For e.g on a 6 digit scaling ( as float) , a number like 1.0002345 will be represented as 1.000235 while a double data type would maintain it as 1.0002345. Always check documentation for details.

sizeof( ) :

The unary operator sizeof( ) can be used to determine the memory allocation for a data type or a variable. It returns the number of bytes allocated.

E.g printf(“Memory allocated for an int is %d bytes” , sizeof ( int ) ) ;

If x is a variable in a program then

printf(“Memory allocated for a x is %d bytes” , sizeof ( x ) ) ;


Data Type Modifiers

Data modifiers preceding a data type declaration may effect change in terms of size and range.

Data modifiers in C are:-

signed

unsigned

long

short

const

volatile

signed:- means data can be positive or negative. Applicable to char, int types.

e.g.. signed int x ; x = -15 ;

signed int y = 25 ;

int y = 25 ;

By default data types are considered signed unless otherwise mentioned. Eg.2 and 3 are the same. There is no need to use the modifier signed. Whether we use or not would not make a difference.

Note:

When a datatype is signed, the left most bit is reserved for sign bit. The left most bit is the most significant bit (called MSB).

unsigned:- means the data will hold only positive values. Applicable to char, int data types.

e.g: unsigned int x ;

unsigned char k ;

unsigned long p ;


Note:

If the data type is unsigned then no bit is reserved as sign bit. The range of the data type doubles on the positive side.

Data modifiers & Range of data

It is important to understand that when we specify the data as unsigned, the leftmost bit is available for data. Hence the range also changes.

Data Type

Range

Char

-128 to 127

unsigned char

0 to 255

int

-32768 to 32767

unsigned int

0 to 65535

const: The modifier const does not allow the variable to be modified.

e.g. const int x = 20 ;

volatile: The modifier volatile announces that the object has special

properties relevant to optimization.

long: When a data type is long the memory allocation generally doubles. Subsequently the range also increases.

e.g. long int x ; /*allocates 4 bytes to x */

short: When a data type is short the memory allocation generally halves. Subsequently the range also decreases.

Note: long double p ;

In this case memory allocated is generally 10 bytes and not 16 as you would expect. The effects of data modifiers are compiler dependent.

Understanding bit representation of signed and unsigned integers

· When the data is signed, one bit is reserved as the sign bit. The left most bit i.e., the most significant bit (MSB) is the sign bit. When the MSB is set that is there is 1 in the left most bit of a signed int/char, it means the number is negative.

· C stores negative numbers as their 2’s complement.

· We need not use the key word signed. By default all data types are signed unless otherwise specified.

· When the data is unsigned, no bit is reserved as the sign bit. We have to use the key word unsigned otherwise it will be assumed that the data is signed.

char x = 25 ; /* The bit pattern is 0001 1001 */

char y = -25 ; /* The bit pattern is 1110 0111 */

How ? Get the 2 ’s complement of 25:

Bit representation of 25 --- > 0001 1001

1’s complement of 25 1110 0110

+ 1

2’s complement of 25 1110 0111

Thus –25 is stored internally as 1110 0111

When we attempt to print the data with a %d format specifier, The MSB is first examined. If it is set ( contains a value of 1) then:

· A negative sign is attached to the output

· 2’s complement is again taken on the bit pattern encountered

That value is printed.

Use of long:- The integer range is rather limited. Consider finding factorial of a number which grows at an alarming rate as the number grows. We need to store a value larger than the integer limit or unsigned limit. We have a data modifier called long. A long int is allocated double the number of bytes that an int is allocated. This is essential when we wish to store a large integer value.

e.g long int factoril ; /* 4 bytes */

unsigned long int factoril ; /* 4 bytes */

long float ; /* 8 bytes */

long double ; /* 10 bytes */

Identifiers:
Names of variables, functions, structures, union pointers are all called as identifiers. An identifier is a label of a memory location. An identifier is used to refer to some entity in our programs.

Naming conventions for Identifiers: ( Always check documentation )

· Maximum length 31 characters

· Should start with an alphabet or underscore

· Can contain alphabets, digits 0 to 9, and underscore

· No other character is allowed

Note: It is important to give meaningful names to identifiers

Variable declaration and assignment:
Any variable has to be declared before it can be used by a program

Program:

main( )
{
int x ;
char y ;
/* some statements */
}

Program:

main( )
{
int x ;
/* some statements */
if ( somecondition)
{
int y ;
/* some statements */
}
/* some statements */

}

Assignment:

Giving a value to a variable is called as assignment.

x = 10 ; /* Variable x is assigned a value of 10 */

No comments: