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
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 :
- Fundamental Data Types
- Derived Data Types
- 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 :
- typedef (type definition)
- 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;
No comments:
Post a Comment