0% found this document useful (0 votes)
17 views

Data Types and Operators of C

Uploaded by

Lena Devaraj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Data Types and Operators of C

Uploaded by

Lena Devaraj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Data types are the keywords, which are used for assigning a type to a variable.

 Primary (Fundamental)datatypes
 Derived data types
 User defined data types

Primary (Fundamental)datatypes

1. Integer(int)-Integer are whole numbers with a range. Integer occupy one word of
storage. C has three class of integer storage that is short int, int, long int all in signed and
unsigned forms. The storage size of int data type is 2 or 4 or 8 byte. int (2 byte) can store
values from -32,768 to +32,767.int (4 byte) can store values from -2,147,483,648 to
+2,147,483,647.

2. Character(char)- Character data type allows a variable to store only one


character.Storage size of character data type is 1. We can store only one character using
character data type.“char” keyword is used to refer character data type.For example, ‘A’
can be stored using char datatype. You can’t store more than one character using char
data type.Please refer C – Strings topic to know how to store more than one characters in
a variable.
3. Floating point(float)-Float data type allows a variable to store decimal values. Storage
size of float data type is 4.We can use up-to 6 digits after decimal using float data
type.For example, 10.456789 can be stored in a variable using float data type
4. Double -precision floating point(double)- Double data type is also same as float data type
.The range for double datatype is from 1E–37 to 1E+37. A double data type number uses
64 bits (8 bytes) It gives a precision of up to 14 digits.

5. Void - The void has no values. This is usually used to specify the type of functions. The
type of function is said to be void when it does not return any value to the calling
function. It can also play a role of a generic type, meaning that it can represent any of the
other standard types.

Derived data types


The derived data type consists of Arrays (used to manage large numbers of objects of the same
type) , Functions (does a specific job), Pointers (represents the address and type of a variable or a
function).
User defined data types
Structures (Different data items that make up a logical unit are grouped together as a structure
data type)
 Unions (A union permits references to the same location in memory to have different
types) and
 Enumeration (used to define variables that can only be assigned certain possible values
throughout the program)

1.9. Declaration of variables

After designing suitable variable names, we must declare them to the compiler. The declaration
does 2 things:

 It tells the compiler what the variable is


 It specifies what type of data the variable will hold

Primary Data Type Declaration


A variable can be used to store a value of any data type Syntax

data-type v1,v2,…vn;

where v1,v2…vn are the names of variables. Example int count; double ratio

1.10. Defining symbolic constants

We often use certain unique constants in a program. These constants may appear repeatedly in a
number of places in the program. A constant is 3.142, for “pi” value. The “pi” value can be used
for many purposes in a program. We face two problems in the subsequent use of such programs.

 Problem in modification of the program


 Problem in understanding the program

Assignment of such constants to a symbolic name frees us from these problems. The symbolic
constant can be defined as follows:
#define symbolic_name value of constant
// Example
#define STRENGTH 100
#define PASS_MARK 50

Symbolic names are sometimes called constant identifiers. The following are the rules for
declaring symbolic constants,

 Symbolic names have the same form as variable names.


 no blank space between ‘#’ and the word ‘define’
 # must be the first character in the line
 a blank space is must between the #define and the symbolic name
 after definition, the symbolic name should not be assigned to any other value within the
program
 symbolic names are not declared for data types
 #define must not end with semicolon (;)
 #define may appear anywhere in the program but before it is referenced in the program
1.11. Declaring variable as constants

The need of certain values of variables to remain constant during the execution of a program.
This can be done with by declaring the variable with the qualifier const at the time of
initialization.
Ex: const int class_size=40;
(value of the int variable class_size cannot be modified by the program).

1.12. Overflow and underflow of data

 Problem of data overflow occurs when the value of a variable is either too big or too
small for the data to hold
 In floating point conversions, the values are rounded off to the number of significant
digits
 An overflow normally results in the largest possible value a machine can hold and
underflow results in zero
 The overflow problem may occur if the data type does not match the value of the constant
 ‘C’ does not provide any warning or indication of integer overflow. It simply gives
incorrect results. So care should be taken to define correct data types for handling I/O
values

1.17.Operators
An operator is a symbol that tells the computer to perform certain mathematical or logical
manipulations. Operators require some data to operate on and such data is called operands.
Operators in C can be classified into following categories:
 Arithmetic Operators
 Relational Operators
 Logical Operators
 Assignment Operators
 Increment and Decrement Operators
 Conditional Operators
 Bitwise Operators
 Special Operators
1.17.1. Arithmetic Operators
C programming language provides all basic arithmetic operators. These are used to perform
mathematical calculations like addition, subtraction, multiplication, division and modulus.
The ‘/’ is integer division which only gives integer part as result after division. ‘%’ is
modulo division which gives the remainder of integer division as result.

1.17.2. Relational Operators


These operators are used to compare the value of two variables.
1.17.3. Logical Operators
These operators are used to perform logical operations on the given two variables. Logical
operators are used when more than one conditions are to be tested and based on that result,
decisions have to be made. An expression which combines two or more relational
expressions is known as logical expression.

1.17.4. Assignment Operators


Assignment operators are used to assign the result of an expression to a variable. The most
useful assignment operator in C is ‘=’. C also has a set of following shorthand assignment
operators.
var op = exp;
is the same as the assignment
var = var op exp;
where var is a variable, op is arithmetic operator, exp is an expression. In this case, ‘op=’ is
known as shorthand assignment operator.

1.17.5. Increment and Decrement Operators


programming allows the use of ++ and – operators which are increment and decrement
operators respectively. Both the increment and decrement operators are unary operators. The
increment operator ++ adds 1 to the operand and the decrement operator – subtracts 1 from
the operand. The general syntax of these operators are:

Increment Operator: m++ or ++m;


Decrement Operator: m--or --m;
In the example above, m++ simply means m=m+1; and m-- simply means m=m-1;
Increment and decrement operators are mostly used in for and while loops.
++m and m++ performs the same operation when they form statements independently but
they function differently when they are used in right hand side of an expression.
++m is known as prefix operator and m++ is known as postfix operator. A prefix operator
firstly adds 1 to the operand and then the result is assigned to the variable on the left whereas
a postfix operator firstly assigns value to the variable on the left and then increases the
operand by 1. Same is in the case of decrement operator.
For example,
X=10;
Y=++X;

1.17.6. Conditional Operators


Conditional operators return one value if condition is true and returns another value is
condition is false.The operator pair “?” and “:” is known as conditional operator. These pair
of operators are ternary operators. The syntax is:
expression1 ? expression2 : expression3 ;
This syntax can be understood as a substitute of if else statement.
Consider an if else statement as:
if (a > b)
x=a;
else
x=b;
Now, this if else statement can be written by using conditional operator as:
x = (a > b) ? a : b ;
1.17.7. Bitwise Operators
In C programming, bitwise operators are used for testing the bits or shifting them left or

right.
1.17.8. Special Operators
C programming supports special operators like comma operator, sizeof operator, pointer
operators (& and *) and member selection operators (. and ->).
i). Comma Operator
The comma operator can be used to link the related expressions together. A comma linked
expression is evaluated from left to right and the value of the right most expression is the
value of the combined expression.
For example:
x = (a = 2, b = 4, a+b)
In this example, the expression is evaluated from left to right. So at first, variable a is
assigned value 2, then variable b is assigned value 4 and then value 6 is assigned to the
variable x. Comma operators are commonly used in for loops, while loops, while exchanging
values.
ii). Sizeof() operator
The sizeof operator is usually used with an operand which may be variable, constant or a data
type qualifier. This operator returns the number of bytes the operand occupies. Sizeof
operator is a compile time operator. The sizeof operator is usually used to determine the
length of arrays and structures when their sizes are not known. It is also used in dynamic
memory allocation.
x = sizeof (a);
y = sizeof(float);

1.18. Expressions
Arithmetic expression in C is a combination of variables, constants and operators written in a
proper syntax. C can easily handle any complex mathematical expressions but these
mathematical expressions have to be written in a proper syntax. Some examples of
mathematical expressions written in proper syntax of C are:

1.19. Evaluation of expression


Expressions are evaluated using an assignment statement of the form:
Variable=expression;
The expression evaluated first and the result then replaces the previous value of the variable on the left
hand side. All the variables used in the expression must be assigned values before evaluation attempted.
Examples of evaluation statement are

The blank space around an operator is optional and adds only to improve readability. When these
statement are used in program, the variable a,b,c and d must be defined before they are used in the
expressions.

1.20. Precedence of arithmetic Operator


At first, the expressions within parenthesis are evaluated. If no parenthesis is present, then
the arithmetic expression is evaluated from left to right. There are two priority levels of
operators in C.
High priority: * / %
Low priority: + -
The evaluation procedure of an arithmetic expression includes two left to right passes
through the entire expression. In the first pass, the high priority operators are applied as they
are encountered and in the second pass, low priority operations are applied as they are
encountered.

 Suppose, we have an arithmetic expression as: x = 9 – 12 / 3 + 3 *2 – 1. This


expression is evaluated in two left to right passes as:

First Pass
Step 1: x = 9-4 + 3 * 2 – 1
Step 2: x = 9 – 4 + 6 – 1
Second Pass
Step 1: x = 5 + 6 – 1
Step 2: x = 11 – 1
Step 3: x = 10

 But when parenthesis is used in the same expression, the order of evaluation gets
changed.
For example,x = 9 – 12 / (3 + 3) * (2 – 1)
When parentheses are present then the expression inside the parenthesis are evaluated first
from left to right. The expression is now evaluated in three passes as:

First Pass
Step 1: x = 9 – 12 / 6 * (2 – 1)
Step 2: x= 9 – 12 / 6 * 1
Second Pass
Step 1: x= 9 – 2 * 1
Step 2: x = 9 – 2
Third Pass
Step 3: x= 7
 There may even arise a case where nested parentheses are present (i.e. parenthesis inside
parenthesis). In such case, the expression inside the innermost set of parentheses is evaluated
first and then the outer parentheses are evaluated.For example, we have an expression as:

x = 9 – ((12 / 3) + 3 * 2) – 1.The expression is now evaluated as:

First Pass:
Step 1: x = 9 – (4 + 3 * 2) – 1
Step 2: x= 9 – (4 + 6) – 1
Step 3: x= 9 – 10 -1
Second Pass
Step 1: x= - 1 – 1
Step 2: x = -2

Operator Precedence The precedence is used to determine how an expression involving more
than one operator is evaluated. There are distinct levels of precedence and an operator may
belong to any of these levels. The operators at higher level of precedence are evaluated first. The
number of evaluation steps is equal to the number of operators in the arithmetic expression.

The rules for evaluation of expression are as follows:


1. First, parenthesized sub expressions from left to right are evaluated.
2. If parentheses are nested, the evaluation begins with the innermost sub-expression.
3. The precedence rule is applied in determining the order of application of operators in
evaluating sub-expressions.
4. The associativity rule is applied when to or more operators of the same precedence appear
in a subexpression.
5. Arithmetic expressions are evaluated from left to right using the rules of precedence.
6. When parentheses are used, the expressions within parentheses assume highest priority.
1.17. Type conversion in expressions
Implicit Type Conversion
C automatically converts any intermediate values to the proper type so that expression can be
evaluated without losing any significance. This automatic conversion is known as implicit
type conversion. During evaluation, if the operands are of different types, the lower type is
automatically converted to the higher type before the operation proceeds. And the result is
always of the higher type.
For example: 5/2 = 2
5/2.0 = 2.5 (Implicit type conversion)
5.0/2 = 2.5 (Implicit type conversion)
Explicit Type Conversion
There are instances when we want to force a type conversion in a way that is different from
the automatic conversion. This problem is solved by converting locally one of the variables
to the higher type.
For Example: 5/2 = 2 (float)
5/2 = 2.5 (Explicit type conversion)
5/ (float)2 = 2.5 (Explicit type conversion)
Explicit type conversion is also known as type casting. There are instances when we want to
force a type conversion in a way that is different from the automatic conversion. This
problem is solved by converting locally one of the variables to the higher type
1.18. Operator precedence and Associativity
The operators of the same precedence are evaluated either from ‘Left-to-Right’ or from ‘Right-
to-Left’, depending on the priority (or precedence) level. This is known as the associativity
property of operator. Precedence rules decide the order in which different operators are applied.
Associativity rule decides the order in which multiple occurrences of the same level operator are
applied. The precedence and associativity of some of the operators is as follows:

1.19. Mathematical function

Mathematical function is frequently used in analysis of real-life problems. Most of the c


compilers support these basic math functions. Table list some standard math functions.

You might also like