Sei sulla pagina 1di 35

Data Types

Data type is used to define the type of value to


be used in a program.
Based on the type of value specified in the
program, specified amount of required bytes
will be allocated to the variables used in the
program.
Three main types:
Primary data type
Derived data type
User defined data type
1. Primary data type
All C Compilers accept the following
fundamental data types.

Integer Type
Integers are whole numbers with a machine
dependent range of values.
C has 3 classes of integer storage namely short
int, int and long int.
All of these data types have signed and
unsigned forms.
Floating Point Types
Floating point number represents a real
number with 6 digits precision.
Floating point numbers are denoted by the
keyword float.
When the accuracy of the floating point
number is insufficient.
Void Type
Using void data type, we can specify the type
of a function.
It is a good practice to avoid functions that
does not return any values to the calling
function.
Character Type
A single character can be defined as a
character type of data. Characters are usually
stored in 8 bits of internal storage.
2. Derived Data Types
Data types that are derived from fundamental
data types are called derived data types.
Derived data types dont create a new data
type, instead they add some functionality to
the basic data types.
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.

According to the rules of C language, 1st
element of array is stored at location a[0] ,
2nd at a[1] & so on.
Eg.
int a[10];
char chi [20];
Pointer
A pointer is a special variable that holds a
memory address (location in memory) of
another variable.
Data type is the type whose address we want
to store in the pointer variable.
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:
Eg.
int a,*b;
b=&a;
In the above case, variable 'b' stores the
address of variable 'a'.
Function
A function is a complete and independent
program which is used (or invoked) by the
main program or other subprograms.

Structure
A structure 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.

Eg.
struct student
{
Char name [20];
int roll,
float marks;
};
Now, its variable is declared as: struct
Eg.
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.

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:
Eg.
union student
{
Char name [20];
int roll,
float marks;
};

Now, the variable is declared as follows:
Eg.
Union student s1;
And its variable is accessed by using dot (.)
operator.
s1.roll, s1.name, s1.marks
Difference between structure and union
The only difference between Union and Structure
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.
3. User Defined Data Types
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 Tag {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.
Eg.
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 7.
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,
Eg.
typedef int number
New-type refers to the new user-defined data
type
Now we can declare integer variables as:
number roll, age, marks;
It is equivalent to:
int roll, age, marks;


Operators
Operator is a Symbol that tells or instructs
the Compiler to perform certain
Mathematical or Logical manipulations
(Calculations).
Operators are used in a program to work
on data and variables.
Operators can be classified into a number of
categories. They include.
1. Arithmetic operators
2. Relational operators
3. Logical operators


4. Assignment operators
5. Increment and Decrement Operators
6. Conditional Operators
7. Bitwise Operators
8. Special Operators
9. Unary operators
10. Equality operators
1. Arithmetic Operators
Arithmetic operators are used to perform
Arithmetic operations.


Examples of arithmetic operators are
x + y
x - y
-x + y
a * b + c
-a * b etc.,
Here a, b, c, x, y are known as operands.
The modulus operator is a special operator in
C language which evaluates the remainder of
the operands after division.

2.Relational Operators
A relational operator compares two operands
to determine whether one is greater than,
greater than or equal to, less than, less than
or equal to the other Often.
It is required to compare the relationship
between operands and bring out a decision
and program accordingly.
C supports the following relational operators.


A simple relational expression contains only one
relational operator and takes the following form.
exp1 relational operator exp2
Where exp1 and exp2 are expressions, which may
be simple constants, variables or combination of
them.
Eg.
6.5 <= 25 TRUE
-65 > 0 FALSE
10 < 7 + 5 TRUE



3. Logical Operators
C has the following logical operators, they
compare or evaluate logical and relational
expressions.

Logical AND (&&)
If both the expressions to the left and to the
right of the logical operator is true then the
whole compound expression is true.
Eg.
a > b && x = = 10
Logical OR (||)
The logical OR is used to combine 2
expressions or the condition evaluates to true
if any one of the 2 expressions is true.
Eg.
a < m || a < n
Logical NOT (!)
The logical not operator takes single
expression and evaluates to true if the
expression is false and evaluates to false if the
expression is true.
Eg.
! (x >= y) the NOT expression evaluates to
true only if the value of x is neither greater
than or equal to y

4. Assignment Operators
The Assignment Operator evaluates an
expression on the right of the expression and
substitutes it to the value or variable on the
left of the expression.
Eg. x = a + b

5. Increment and Decrement Operators
The increment and decrement operators are
one of the unary operators which are very
useful in C language.
They are extensively used in for and while
loops.
The syntax of the operators is given below
1. ++ variable name
2. variable name++
3. variable name
4. variable name

Potrebbero piacerti anche