Karla News

Different Data Types Used in C Language

C supports different types of data, each of which may be represented differently within a computer’s memory. Data type is used to determine what type of value a variable or a constant can contain throughout the program. In C language, different variables contain different data types e.g. Roll No contains an ‘integer’ value whereas percentage contains a ‘float’ value. C language offers a number of variations for the same data type like integer data type can be long int and short int. The intention of providing these variations is to provide integers with different ranges possible.

Many people believe that a programmer working in C language is provided with a very tiny set of data types but Dennis Ritchie has made available to the C programmers a feature that allows them to derive their own data types from the existing one. This way a programmer can decide which data type is to be used depending on these 2 factors:

1. Economize the memory space.
2. Improve the speed of execution of program.

In C language, it is compulsory to declare variables with their data type before using them in any statement. Mainly data types are categorized into 3 categories:-

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

1. Fundamental Data Types

Fundamental data types are further categorized into 3 types. Let us discuss each of this type briefly.

Int (Integer)

Integer data type is used to store numeric values without any decimal point e.g. 7, -101, 107, etc.
A variable declared as ‘int’ must contain whole numbers e.g. age is always in number.

Syntax:

int variable name;

E.g. int roll, marks, age;

Float

Float data type is used to store numeric values with decimal point. In other words, float data type is used to store real values, e.g. 3.14, 7.67 etc. A variable declared as float must contain decimal values e.g. percentage, price, pi, area etc. may contain real values.

Syntax:

Float variable name;

E.g. float per, area;

Char (Character)

Char (Character) data type is used to store single character, within single quotes e.g. ‘a’, ‘z’,’e’ etc. A variable declared as ‘char’ can store only single character e.g. Yes or No Choice requires only ‘y’ or ‘n’ as an answer.

See also  Is Clear Internet Service Any Good?

Syntax:

Char variable name;

E.g. Char chi=’a’, cha;

Void

Void data type is used to represent an empty value. It is used as a return type if a function does not return any value.

Type modifier

Type modifier is used to change the meaning of basic or fundamental data types so that they can be used in different situations. Various type modifiers are- signed, unsigned, short and long.

C language offers 3 different ‘int’ type modifiers – short, int, and long that represents three different integer sizes. C language also offers 3 different floating point type modifiers – float, double, and long double. Two special ‘char’ type modifiers are- signed, unsigned.

Type Size (in bytes) Range

Int (signed/ short) 2 -32768 to 32767
Int (unsigned) 2 0 to 65535
Long (signed) 4 -2,147,483,648 to 2,147,483,648
Long (unsigned) 4 0 to 4,294,467,295
Float 4 3.4*10-34 to 3.4*10-34
Double (long float) 8 1.7*10-308 to 1.7*10-308 -1
Long double 10 3.4*10-4932 to 3.4*10-4932 -1
Char 1 -128 to 127
Signed char 1 0 to 255
Unsigned char 1 -128 to 127

2. Derived Data Types

Data types that are derived from fundamental data types are called derived data types. Derived data types don’t create a new data type; instead, they add some functionality to the basic data types. Two derived data type are – Array & Pointer.

Array

An array is a collection of variables of same type i.e. collection of homogeneous data referred by a common name. In memory, array elements are stored in a continuous location.

Syntax:

[];

E.g. int a[10];
Char chi [20];

According to the rules of C language, 1st element of array is stored at location a[0] , 2nd at a[1] & so on.

Pointer

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

Syntax:

*

Data type is the type whose address we want to store in the pointer variable.
* is a pointer variable.
‘var name’ is the name where the variable is to be stored.

E.g. if we want to store the address of an ‘int’ data type variable into a pointer variable, it is done in the following manner:

int a,*b;
b=&a;

In the above case, variable ‘b’ stores the address of variable ‘a’.

See also  Cingular Wireless Cell Phone Plan Benefits

3. User Defined Data Type

User defined data type is used to create new data types. The new data types formed are fundamental data types. Different user defined data types are: struct, union, enum, typedef.

Struct

A struct is a user defined data type that stores multiple values of same or different data types under a single name. In memory, the entire structure variable is stored in sequence.

Syntax:

Struct < structure name>
{
Var1;
Var2;
—–
—–
};

Structure name is the name of structure that we have to give e.g. if we want to store details of a student as- name, roll, marks then in structure it will be declared in the following manner:

Struct student
{
Char name [20];
Int roll,
Float marks;
};

Now, its variable is declared as: struct

E.g. Struct student s1;

And its variable is accessed using dot (.) operator.

S1.roll, s1.name, s1.marks

Union

A union is a user defined data type that stores multiple values of same or different data types under a single name. In memory, union variables are stored in a common memory location.

Syntax:

Union < tag name>
{
Var1;
Var2;
—–
—-
};

Tag name is the name of union, e.g. if we want to store details of a student as- name, roll, marks then it will be done in the following manner
:
Union student
{
Char name [20];
Int roll,
Float marks;
};
Now, the variable is declared as follows:

struct

e.g. Union student s1;

And its variable is accessed by using dot (.) operator.

S1.roll, s1.name, s1.marks

The only difference between Union and Struct is allocation of memory. ‘Union’ is assigned a memory size equal to the biggest data type used in union declaration whereas ‘struct’ will occupy the size equal to the sum of all variables sizes.

Enum:

An enumeration is a data type similar to a struct or a union. Its members are constants that are written as variables, though they have signed integer values. These constant represent values that can be assigned to corresponding enumeration variables.

Syntax:

Enum {value1, value2, _ _ _ _ _ _, value n};

Enum is the required keyword;
Tag is the name that identifies enumerations having this composition;
Value1, value2, – – – – – – , value n represents the individual identifiers that may be assigned to a variable of this type.

See also  The Pros and Cons of the IRiver Clix MP3 Player

E.g. : Enum colors {black, blue, cyan, green, red, yellow};
Color foreground, background;

First line defines enum and second one defines enum variables. Thus, variables foreground and background can be assigned any one of constant black, blue, – – – – ,yellow.

In declaration black is assigned 0 value, and blue 1, – – – -, and yellow 5. We can also assign the value accordingly as we want to assign, like:

Enum colors {black, blue, cyan=4, green, red, yellow}
Here black=0, blue=1, cyan=4, green=5, red=6, yellow=7

Typedef:

The ‘typedef’ allows the user to define new data-types that are equivalent to existing data types. Once a user defined data type has been established, then new variables, array, structures, etc. can be declared in terms of this new data type.

A new data type is defined as:

Typedef type new-type;

Type refers to an existing data type,
New-type refers to the new user-defined data type.

E.g. : typedef int number

Now we can declare integer variables as:

number roll, age, marks;

It is equivalent to:

int roll, age, marks;

Summary:

1. We can use different variations of the primary/fundamental data types like signed, unsigned, short, long, double and long double.
2. The maximum value a variable can hold depends upon the number of bytes it occupies in the memory.
3. By default, all the variables are signed but a variable can be declared as unsigned, if you want to increase the range of the data type to accommodate greater value without increasing the bytes occupied.
4. While designing a program, a programmer must consider the 2 main factors:
Program is efficient and economizes the memory space consumed, also referred to as space complexity.
Program is executed in the least possible time, also referred to as time complexity.