C Language Data Types

C Language Data Types

What is a Data Type?

A data type is a classification of the type of data that a variable or object can hold in computer programming. Data type is an important factor in virtually all computer programming languages, including visual basic, C#, C++ and JavaScript. When programmers create computer applications, both desktop and web-based, data types must be referenced and used correctly, to ensure that the result of the application's functions is correct and error-free.
The most common data types include boolean, integer, floating-point number, character, and alphanumeric string. There are many other data types in programming languages, each serving a specific function and storing data in a particular way. Understanding the different data types allows programmers to design computer applications more efficiently and accurately.

Overview on Data Types:

Data types are used within type systems, which offer various ways of defining, implementing and using them. Different type systems ensure varying degrees of type safety. Formally, a type can be defined as "any property of a programme we can determine without executing the program”.
Almost all programming languages explicitly include the notion of data type, though different languages may use different terminology. Common data types may include:
•    integers,
•    booleans,
•    characters,
•    floating-point numbers,
•    alphanumeric strings.
For example, in the Java programming language, the "int" type represents the set of 32-bit integers ranging in value from -2,147,483,648 to 2,147,483,647, as well as the operations that can be performed on integers, such as addition, subtraction, and multiplication. Colors, on the other hand, are represented by three bytes denoting the amounts each of red, green, and blue, and one string representing that color's name; allowable operations include addition and subtraction, but not multiplication.
Most programming languages also allow the programmer to define additional data types, usually by combining multiple elements of other types and defining the valid operations of the new data type. For example, a programmer might create a new data type named "complex number" that would include real and imaginary parts. A data type also represents a constraint placed upon the interpretation of data in a type system, describing representation, interpretation and structure of values or objects stored in computer memory. The type system uses data type information to check correctness of computer programs that access or manipulate the data.

Data Types in C Language:
C supports several types of data, each of which may be represented differently within the computer’s memory. The basic data types are listed below. Typical memory requirements are also given. The memory requirements for each data type will determine the permissible range of values for that data type. The memory requirements for each data type may vary from one compiler to another. There are basically three categories of data types available in c. they are
1.    Primary data types.
2.    Derived data types
3.    User defined data types.

Primary Data Types:

By the definition of the data type, a data type is used to indicate the type of a data. C   programming language categorized four types of data which come under primary data types. These are the basic data types used in each and every c program
 There are five types of data which come under primary data types in c. they are
•    Integer type (int)
•    Floating type (float)
•    Character type (char)
•    Empty data type (void)
•    Boolean data type (boolean)

Integer Data Types:

Integers are whole numbers with a range of values, range of values are machine dependent. Generally an integer occupies 2 bytes memory space and its value range limited to -32768 to +32767(that is, -215to +215-1). A signed integer use one bit for storing sign and rest 15 bits for number.
To control the range of numbers and storage space, C has three classes of integer storage namely short int, int and long int. All three data types have signed and unsigned forms. A short int requires half the amount of storage than normal integer. Unlike signed integer, unsigned integers are always positive and use all the bits for the magnitude of the number. Therefore, the range of an unsigned integer will be from 0 to 65535. The long integers are used to declare a longer range of values and it occupies 4 bytes of storage space.
Syntax: int <variable name>;
int num1;
short int num2;
long int num3;
Example: 5, 6, 100, 2500.

Floating Point Data Types:

The float data type is used to store fractional numbers (real numbers) with 6 digits of precision. Floating point numbers are denoted by the keyword float. When the accuracy of the floating point number is insufficient, we can use the double to define the number. The double is same as float but with longer precision and takes double space (8 bytes) than float. To extend the precision further we can use long double which occupies 10 bytes of memory space.
Syntax: float <variable name>;
float num1;
double num2;
long double num3;
Example: 9.125, 3.1254.

Character Data Type:

Character type variable can hold a single character and are declared by using the keyword char. As there are singed and unsigned int (either short or long), in the same way there are signed and unsigned chars; both occupy 1 byte each, but having different ranges. Unsigned characters have values between 0 and 255; signed characters have values from –128 to 127.
Syntax: char <variable name>;
char ch = ‘a’;
Example: a, b, g, S, j.
Void Type:
The void type has no values therefore we cannot declare it as variable as we did in case of integer and float. The void data type is usually used with function to specify its type.
Syntax: void variable_name;
Boolean type:
The _Bool keyword denotes a value that can hold either zero or one. If another numeric or pointer type is assigned to a boolean the value stored is 0 if the numeric or pointer value evaluates to 0, otherwise it is one.
Syntax: boolean <identifier/variable name>;
Example: boolean a, b, c;

Derived Data Types:
Derived data types in C Programming Language are those C data types which are derived from the fundamental data types using some declaration operators.
The basic derived data types in C are:
•    Arrays
•    Functions
•    Classes
•    Structures
•    Pointers
•    Unions
Arrays can be defined as a set of finite and homogeneous data elements. Each element of an array is referenced using an index.
For example:
If the name of an array is AR which have 5 elements then the array will be represented as:
AR[0], AR[1], AR[2], AR[3], AR[4]
Here, these subscripts which are containing the digit are known as an index.
There are various types of arrays namely:
•    One dimensional
•    Two dimensional
•    Multi dimensional
The elements of an array are indexed from 0 to size-1. It is necessary to declare the size of an array before initialization. An array can be initialized by placing the elements of an array within the curly braces.
For example, an array initialization can be done in following manner:
int AR [5] = {5, 2, 6, 8, 3};
Function groups a number of program statements into a unit and gives it a name. This unit can be invoked from other parts of a program. A computer program cannot handle all the tasks by itself. Instead its requests other program like entities – called functions in C – to get its tasks done. A function is a self contained block of statements that perform a coherent task of same kind. The name of the function is unique in a C Program and is Global. It means that a function can be accessed from any location within a C Program. We pass information to the function called arguments specified when the function is called. And the function either returns some value to the point it was called from or returns nothing. We can divide a long C program into small blocks which can perform a certain task. A function is a self contained block of statements that perform a coherent task of same kind. We first declare the function and then at the end of the program we define the function.
In general, the first line of the function can be written as
data –type name (type 1 arg 1,type 2 arg 2,….,type n arg n)
Where data-type of the item that is returned by the function, name represents the function name, and type 1, type 2…, type n represent the data types of the arguments arg 1, arg 2,…, arg n.
A pointer is a variable that holds the address of the memory space. We can say that if one variable can hold the address of another variable then it is said that the first variable is pointing to the second. A pointer is declared in a following manner:
int *temp;
i.e. first we have to declare the type and then the variable name precede by an * (asterisk) sign. The pointer variable always occupies 2 bytes of memory.
A data structure is a group of data elements grouped together under one name. These data elements, known as members, can have different types and different lengths.
Data structures are declared using the following syntax:
Syntax: struct structure_name {member_type1 member_name1; member_type2 member_name2;
member_type3 member_name3 ;…} object names;
Where structure_name is a name for the structure type, object_name can be a set of valid identifiers for objects that have the type of this structure. Within braces { } there is a list with the data members, each one is specified with a type and a valid identifier as its name. Structure is different from an array in the sense that an array represents an aggregate of elements of same type whereas a structure represents an aggregate of elements of arbitrary types.

Unions allow one same portion of memory to be accessed as different data types, since all of them are in fact the same location in memory. Its declaration and use is similar to the one of structures but its functionality is totally different:
Syntax: union union_name {member_type1 member_name1; member_type2 member_name2; member_type3 member_name3; . .} object_names;
All the elements of the union declaration occupy the same physical space in memory. Its size is the one of the greatest element of the declaration. all of them are referring to the same location in memory, the modification of one of the elements will affect the value of all of them. We cannot store different values in them independent of each other. One of the uses a union may have is to unite an elementary type with an array or structures of smaller elements. The exact alignment and order of the members of a union in memory is platform dependant. Therefore be aware of possible portability issues with this type of use.
A class is a collection of variables and function under one reference name. it is the way of separating and storing similar data together. Member functions are often the means of accessing, modifying and operating the data members (i.e. variables). It is one of the most important features of C++ since OOP is usually implemented through the use of classes.
User Defined Data Types:
User defined data type is used to create new data types. The new data types formed are fundamental data types. Programmer can create an identifier that denotes an already existing data type with the help of user defined data type. The programmer defined data type identifier is then used in a program to declare variables. Its purpose is to redefine the name of an existing data type.
Syntax: typedef;
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.
Example: 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.
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.
Example: typedef int number
Now we can declare integer variables as:
number, roll, age, marks;
It is equivalent to:
int roll, age, marks;

The C Language supports several type modifiers you may apply to your program's variables. The type modifiers change the amount of storage allocated to the variable and the range of values it may represent.
•    signed
•    unsigned
•    short
•    long
signed Type Modifier:
All data types are “signed” by default. Signed Data Modifier implies that the data type variable can store positive values as well as negative values.
It is used as follows:
signed data-type name < [>= value<]>;
Where data-type is the data type (char or int) of the variable.
name is the name of the variable.
value is the value to assign to the variable.
Unsigned Type Modifier:
If we need to change the data type so that it can only store only store positive values, “unsigned” data modifier is used.
It is used as follows:
unsigned data-type name < [>= value<]>;
Where data-type is the data type (char or int) of the variable.
name is the name of the variable.
value is the value to assign to the variable.
Long Type Modifier:
Sometimes while coding a program, we need to increase the Storage Capacity of a variable so that it can store values higher than its maximum limit which is there as default. In such situations or programs, we need to make use of the “long” data type qualifier. “long” type modifier doubles the “length” of the data type when used along with it. This variable will take 4 Bytes in memory.
It is used as follows:
< [> {signed|unsigned}<]> long <[>int<]> name <[>= value<]>;
name is the name of the variable.
value is the value to assign to the variable.

Short Type Modifier:
A “short” type modifier does just the opposite of “long”. If one is not expecting to see high range values in a program and the values are both positive & negative. This variable will take only 1 Byte in memory.
It is used as follows:
< [> {signed|unsigned} <]> short < [>int<]> name < [>= value<]>;
name is the name of the variable.
value is the value to assign to the variable.
We can apply the above mentioned modifiers to integer (int) and character (char) base types.
1. Integer Type Modifiers:
•    We can use all the above mentioned Type Modifiers for int data type in C Language
•    Short int and long int are also termed as short and long respectively.
•    Int is signed & unsigned by default.
2. Character Type Modifiers:
•    Variables of type char consume 1 byte in memory.
•    Char can be signed or unsigned only.
•    They have a range of -128 to 127 and 0 to 255 for signed & unsigned respectively.
3. Float Type & Double Type Modifier:
There are 3 types of float type modifiers as given below:
•    float
•    double
•    long double
Double is same as long float. Float type occupies 4 bytes of memory. Type double occupies 8 bytes. Long double occupies 10 bytes. The exception to this is long double, which modifies the size of the double data type to 10 bytes.

A type qualifier neither affects the range of values nor the arithmetic properties of the declared object. They are used to indicate the special properties of data within an object. Two type qualifiers available in c are:
const: The const type qualifier declares an object that is not changed at run-time.
const float pi = 3.14159f;

volatile: The volatile type qualifier declares an object whose value may be changed by something external to the code in which it appears.
volatile float current Temperature = 40.0;


Post a Comment