Data Types and Operators of C
Data Types and Operators of C
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.
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.
After designing suitable variable names, we must declare them to the compiler. The declaration
does 2 things:
data-type v1,v2,…vn;
where v1,v2…vn are the names of variables. Example int count; double ratio
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.
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,
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).
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.
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:
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.
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:
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.