Showing posts with label data types. Show all posts
Showing posts with label data types. Show all posts

Thursday, December 1, 2011

Type Conversions

Type Conversion is the conversion of one operand's value to a different data type either implicitly or explicitly. Type conversion may occur when any of the following conditions are met :
  1. Whenever a value of one type is assigned to a variable of another type.
  2. When an operator converts the type of its operand(s) before performing operation.
  3. When a value of one type is explicitly cast to a different type.
  4. When a value is passed as an argument to a function or is returned from a function.
Type conversion can be either implicit or explicit.

Implicit/Automatic Type Conversion :
An expression may consist of operands belonging to different data types. In those expressions, the evaluation is done after the operands are converted to a common data type. In C, the automatic conversion of the operands is done as follows :
  1. If either operand is of type long double, the other operand is converted to type long double.
  2. If the above condition is not met and either operand is of type double, the other operand is converted to type double.
  3. If the above two conditions are not met and either operand is of type float, the other operand is converted to type float.
  4. If the above three conditions are not met (none of the operands are of floating types), then integral conversions are performed on the operands as follows:
    1. If either operand is of type unsigned long, the other operand is converted to type unsigned long.
    2. If the above condition is not met and either operand is of type long and the other of type unsigned int, both operands are converted to type unsigned long.
    3. If the above two conditions are not met, and either operand is of type long, the other operand is converted to type long.
    4. If the above three conditions are not met, and either operand is of type unsigned int, the other operand is converted to type unsigned int.
    5. If none of the above conditions are met, both operands are converted to type int.
Explicit Conversion / Type Casting
If you want the type conversion that is different from the Implicit conversions, you will have to type casts to explicitly convert types.

It can be done as follows :
(type name) expression


Here type name is the destined data type and expression is the expression is the one that is to be converted to that type.
For example,

Without Type Casting
int x = 5, y=2 ;
float result;
result = x/y;
printf("%f",result);

Output:

2.000000
With Type Casting
int x = 5, y=2 ;
float result;
result = (float) x/y;
printf("%f",result);

Output:

2.500000


Sunday, November 13, 2011

Data Types

Data can be defined as the raw information input to the computer. Data is differentiated into various types in C. They may be classified as :

  1. Fundamental Data Types
  2. Derived Data Types
  3. User Defined Data Types

1. Fundamental Data Types

There are three numeric data types that comprise the fundamental or primary data types. The fundamental data types are: int, float and char. They are also called as pre-defined primitive types associated with the C compiler. The C compiler knows about these types and the operations that can be performed on the values of these types. The following are the declaration statements of variables of different types:
int x;
float f;
char ch;
An integer is a whole number, a float is a real number and a character is represented within single quotes. 
Example:
1 is an integer (whole number)
1.86 is a real number (floating point number)
‘1’ is a character constant
“1” is a string literal

2. Derived Types
Long, double, unsigned, arrays and pointers are the derived types from the fundamental primitive types. A long integer can be declared as follows:

long int i;
long j;


Unsigned numbers are represented as follows:

unsigned int i;

In an unsigned integer all the bits are used to store the value. All the variables those are declared as 'int' are signed by default. In a signed integer, the most significant bit (the left most bit) is a sign bit (used to store the sign of the integer : +ve or -ve).

As for now, the pointers will be discussed in detail in the forthcoming posts.

3. User Defined Data Types 
The users themselves can define an identifer that allows them to represent an existing data type. There are two ways in doing this :
  1. typedef (type definition)
  2. enum (enumerator)
typedef
The type definition's syntax is as follows :
typedef type identifier;
We do not create any new data type but just uses a new name for the identifiers. For example consider the following :
typedef int price;
typedef float height;

Later we can declare variables like this :
price p1,p2;
height w1,w2;

It may seem a bit odd. Why do we need 'typedef' when we can directly use the data types for declaring variables ? Here is the reason : Classify variables according to their uses. Even though all the variables may belong to the same type each may have specific use.

For example :
typedef int price;
typedef int quantity;
price price1=100,price2=150;
quantity qty1=5,qty2=7;
Although all the variables belong to the integer type, they have different uses. This may be useful for easy handling of variables in much larger programs.
enum
Here is the real deal. You can create your own data type with your own values.

Syntax:
enum identifier { Values } variable_list;

The values for the enumerated variable are specified within the curly braces.
Example:
enum direction { NORTH, SOUTH, WEST, EAST };
direction dir1,dir2;