Data Types and their Format Specifier In C - Programming Languages

Latest

Friday, 30 December 2016

Data Types and their Format Specifier In C

DATA TYPE
Data types in c refer to an extensive system 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.
 Mainly there are 3 types of data type.
  1. Primitive Data Type-It is further divided into 6 types.
  • Character(character, char);-A character type (typically called "char") may contain a single letter, digit, punctuation mark, symbol, formatting code, control code, or some other specialized code (e.g., a byte order mark). In C, char is defined as the smallest addressable unit of memory. On most systems, this is 8 bits; Several standards, such as POSIX, require it to be this size. Some languages have two or more character types, for example a single-byte type for ASCII characters and a multi-byte type for Unicode characters. The term "character type" is normally used even for types whose values more precisely represent code units, for example a UTF-16 code unit as in Java and JavaScript. Characters may be combined into strings. The string data can include numbers and other numerical symbols but will be treated as text.
  •  Integer (integer, int, short, long, byte)f precisions:-An integer data type can hold a mathematical integer. Integers may be either signed (allowing negative values) or unsigned (whole numbers only). Typical sizes of integers are:

    Size (bytes) Size (bits) Names Signed range Unsigned range
    1 byte 8 bits Byte, octet, minimum size of char in C −128 to +127 0 to 255
    2 bytes 16 bits x86 word, minimum size of short and int in C −32,768 to +32,767 0 to 65,535
    4 bytes 32 bits x86 double word, minimum size of long in C, actual size of int for most modern C compilers, pointer for IA-32-compatible processors −2,147,483,648 to +2,147,483,647 0 to 4,294,967,295
    8 bytes 64 bits x86 quadruple word, minimum size of long long in C, actual size of long for most modern C compilers, pointer for x86-64-compatible processors −9,223,372,036,854,775,808 to +9,223,372,036,854,775,807 0 to 18,446,744,073,709,551,615
    unlimited / 8 unlimited Bignum –( ( 2unlimited ) / 2 ) to +( ( 2unlimited ) / 2 ) 0 to 2unlimited
    Literals for integers can be written as regular Hindu-Arabic numerals, consisting of a sequence of digits and with negation indicated by a minus sign before the value. However, most programming languages disallow use of commas for digit grouping. Examples of integer literals are:
  • Floating-point number (float, double, real, double precision):-A floating-point number represents a limited-precision rational number that may have a fractional part. These numbers are stored internally in a format equivalent to scientific notation, typically in binary but sometimes in decimal. Because floating-point numbers have limited precision, only a subset of real or rational numbers are exactly representable; other numbers can be represented only approximately.

    Many languages have both a single precision (often called "float") and a double precision type.
    Literals for floating point numbers include a decimal point, and typically use "e" or "E" to denote scientific notation. Examples of floating-point literals are:
  • 20.0005
  • Fixed-point number :-(fixed) with a variety of precisions and a programmer-selected scale:-A fixed-point number represents a limited-precision rational number that may have a fractional part. These numbers are stored internally in a scaled-integer form, typically in binary but sometimes in decimal. Because fixed-point numbers have limited precision, only a subset of real or rational numbers are exactly representable; other numbers can be represented only approximately. Fixed-point numbers also tend to have a more limited range of values than floating point, and so the programmer must be careful to avoid overflow in intermediate calculations as well as the final results

  • Boolean:- logical values true and false.
  • Reference :-(also called a pointer or handle), a small value referring to another object's address in memory, possibly a much larger one.
2. Derived Data Type:-Data types that are derived from fundamental data types are called derived data types. Derived data types don't create a new data type but,instead they add some functionality to the basic data types. In C, two derived data type are : Array & Pointer.

  • Array : An array is a collection of variables of same type. They are stored in contagious memory allocation.
e.g

int a[10];
char chi [20];  

  • Pointer :
A pointer is a special variable that holds a memory address (location in memory) of another variable.

int i=10;
int *j;
j=&i;

 3.Use Defined Data type:-Sometimes, the basic set of data types defined in the C language such as int, float etc. may be
insufficient for your application.  In circumstances such as these, you can create your own data
types which are based on the standard ones.  There are three mechanisms for doing this in C:

The typedef keyword allows the programmer to create new data types :- in a way.
You typically use the typedef keyword to improve the readability of your code and as a means
of abbreviating it.
For example, the statement
    typedef unsigned char byte;
creates a new datatype called byte which is short-hand for unsigned char.  You can the use
this new type to declare variables as follows:
    byte a; 
 2.Structures:- Structures are aggregate data types.  For example, you can create a structure which contains
an integer, a float and a string.  This grouping of data types into a single structure may have
particular meaning to your application.  For example, a banking application will require some
form of customer record which will probably consist of an account number, name, address,
balance and presumably other additional items.  It seems logical to store these different items
in such a way that they are accessible as a unit or using a single reference.  The struct keyword
allows us to design such a collection of data items.   A banking customer record structure could
be defined as follows:
struct Customer_Record
{
    char CustomerName[50]; /* Customer name of up to 50 characters */
    char AccountNumber[11]; /* 10 digit Account number */
    float Balance;
    char Address[1024]; /* Include 1k for storage of the customer address.
}


Unions:-Unions allow you to create variables which share a common memory space.   Unions are a bit like structures, in that they appear to be an aggregate data type.  However, the main difference is that the union members occupy overlapping memory areas.  Why would you create such a strange variable?  Well, there are several instances in which this could be useful.  For example, you might want 2 different ways of interpreting a given piece of data.  One "view" of this piece of data might be as a stream of 16 bit integers, another might be as a stream of 8 bit bytes.  Don't be fooled into thinking that unions can help you  to preform automatic type conversions from say floats to strings and so on.  They can only allow you to interpret a given collection of bytes in memory in a number of different ways.   Personal experience has shown me that unions are best avoided if at all posisble as they are confusing and their behaviour can depend on the operating system/hardware platform.  In certain circumstances (which usually related to hardware interfacing) I have found them to be useful but these were rare circumstances indeed.  The following example shows how space is assigned to a union in a system which has a 16 bit integer length. 

union SomeUnion 

{  

int i;  

char str[4];

}
Format specifiers:-
The % specifiers that you can use in ANSI C are:
  • Usual variable type Display
  • %c char single character
  • %d (%i) int signed integer
  • %e (%E) float or double exponential format
  • %f float or double signed decimal
  • %g (%G) float or double use %f or %e as required
  • %o int unsigned octal value
  • %p pointer address stored in pointer
  • %s array of char sequence of characters
  • %u int unsigned decimal
  • %x (%X) int unsigned hex value
 

No comments:

Post a Comment