Friday, July 29, 2016

C Basics – Data Types - Type Specifiers in C

A data type or type specifier determines what type of values an object can have and what operations can be performed.

Data types in c are used for declaring variables or functions of different types. The type of a variable determines how much space it occupies in storage and how the bit pattern stored is interpreted.

C offers a standard, minimal set of basic data types. Sometimes these are called "primitive" types. A lot of complex data structures can be developed from these basic data types.

Basic Types:
The C language provides the four basic arithmetic type specifiers char, int, float and double.
Data type Meaning
char The char type is used to store the integer value of a member of the representable character set. That integer value is the ASCII code corresponding to the specified character.
int The int type allows a particular machine to handle integer values in the most efficient way for that machine.
float Single precision real number
double Double precision real number

The size and range of these data types may vary among processor types and compilers. For example, in 16-bit operating systems, the int type is usually 16 bits, or 2 bytes. In 32-bit operating systems, the int type is usually 32 bits, or 4 bytes. Since the sizes of the int and unsigned int types vary, programs that depend on a specific int size may not be portable to other machines. To make programs more portable, you can use expressions with the sizeof operator instead of hard-coded data sizes.

Data type modifiers modify the behavior of variable type to which they are applied. Data type modifiers can be classified into two types: size and sign.

Size Modifier – short and long
Alter the size of the basic data types. The minimum size of short int is 16 bit. The size of int must be greater than or equal to that of a short int. The size of long int must be greater than or equal to a int. The minimum size of a long int is 32 bits.

Sign Modifier – signed and unsigned
Specifies, whether a variable can hold both –ve and +ve numbers, or only +ve numbers. These modifiers can be applied to the data type’s int and char only. Signed integers are represented in two's-complement form. The most-significant bit holds the sign: 1 for negative, 0 for positive and zero.

ANSI has the following rules:

short int <=    int <= long int
float <= double <= long double

What this means is that a 'short int' should assign less than or the same amount of storage as an 'int' and the 'int' should be less or the same bytes than a 'long int'.

Data type Size (byte) Range
char 1 -128 to +127
signed char 1 -128 to +127
unsigned char 1 0 to 255
short 2 -32,768 to +32,767
signed short 2 -32,768 to +32,767
unsigned short 2 0 to 65,535
int 2 -32,768 to +32,767
signed int 2 -32,768 to +32,767
unsigned int 2 0 to 65,535
long 4 -2,147,483,648 to +2,147,483,647
signed long 4 -2,147,483,648 to +2,147,483,647
unsigned long 4 0 to 4,294,967,295
float 4 3.4E-38 to 3.4E+38 with 6 digits of precision
double 8 1.7E-308 to 1.7E+308 with 10 digits of precision
long double 8 3.4E-4932 to 1.1E+4932 with 10 digits of precision
Note: The size and range are compiler dependent.

The maximum value a variable can hold depends upon the number of bytes it occupies in memory. By default all the variables are signed. We can declare a variable as unsigned to accommodate greater value without increasing the bytes occupied.

Enumerated Type:
The enumerated type in C, specified with the enum keyword, is a type designed to represent values across a series of named constants. Each of the enumerated constants has type int.

An enumerated type is declared with the enum specifier and an optional name (or tag) for the enum, followed by a list of one or more constants contained within curly braces and separated by commas, and an optional list of variable names. By default, the first constant in an enumeration is assigned the value zero, and each subsequent value is incremented by one over the previous constant. Specific values may also be assigned to constants in the declaration, and any subsequent constants without specific values will be given incremented values from that point onward. For example, consider the following declaration:

enum colors { 
 RED, 
 GREEN, 
 BLUE = 5, 
 YELLOW 
} paint_color;

This declares the enum colors type; the int constants RED (whose value is 0), GREEN (whose value is one greater than RED, 1), BLUE (whose value is the given value, 5), and YELLOW (whose value is one greater than BLUE, 6); and the enum colors variable paint_color. The constants may be used outside of the context of the enum (where any integer value is allowed), and values other than the constants may be assigned to paint_color, or any other variable of type enum colors.

Void Type:
The type specifier void indicates that no value is available. The keyword void has three uses: to specify a function return type, to specify an argument-type list for a function that takes no arguments, and to specify a pointer to an unspecified type. You can use the void type to declare functions that return no value or to declare a pointer to an unspecified type.

Derived Types:
You can also add your own data types, called "derived types," by declaring new ones based on types already defined. They include (a) Pointer types, (b) Array types, (c) Structure types, (d) Union types and (e) Function types. The array types and structure types are referred collectively as the aggregate types. The type of a function specifies the type of the function's return value.



Related topics:
Const and Volatile - Type Qualifiers in C   |   Variables in C   |   Local and Global Variables in C   |   Constants in C   |   Initialization in C

List of topics: C Programming

No comments:

Post a Comment