Data - Types and Operators Unit-II
Data - Types and Operators Unit-II
Data Types
2
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, the amount of
Bytes(memory) will be allocated to the variables used in the program.
• Data types are broadly classified into four main types. They are:
• Basic (or) Primary data type ( Fundamental Data Types)
• Derived data type
• User defined data type.
• Void data type
• Primary Data Type
• Integers are represented as int, character are represented as char, floating
point value are represented as float, double precision floating point are
represented as double and finally void are primary data types.
• Primary data type offers extended data types. Long int, long double are
extended data types.
Integer Data Type
• Integer data type can store only the whole numbers.
The void data type is an empty data type that is used as a return type for the
functions that return no value in C.
Example:
void function(int n)
int function(void)
7
Limits of Data Types –
#include <stdio.h> printf("SHRT_MAX : %d\n", SHRT_MAX);
#include <limits.h> printf("SHRT_MIN : %d\n", SHRT_MIN);
void main() printf("UCHAR_MAX : %d\n", UCHAR_MAX);
{ printf("UINT_MAX : %u\n", (unsigned int) UINT_MAX);
printf("CHAR_BIT : %d\n", CHAR_BIT); printf("ULONG_MAX : %lu\n", (unsigned long)
ULONG_MAX);
printf("CHAR_MAX : %d\n", CHAR_MAX);
printf("USHRT_MAX : %d\n", (unsigned short)
printf("CHAR_MIN : %d\n", CHAR_MIN);
USHRT_MAX);
printf("INT_MAX : %d\n", INT_MAX);
return 0;
printf("INT_MIN : %d\n", INT_MIN);
}
printf("LONG_MAX : %ld\n", (long) LONG_MAX);
printf("LONG_MIN : %ld\n", (long) LONG_MIN);
printf("SCHAR_MAX : %d\n", SCHAR_MAX);
printf("SCHAR_MIN : %d\n", SCHAR_MIN);
Limits of Data Types –
1. Automatic variables
2. External variables
3. Static variables
4. Register variables
Storage Classes –
Automatic variables A variable declared inside a function without any
storage class specification, is by default an automatic variable.
• They are created when a function is called and are destroyed automatically
when the function exits.
• Automatic variables can also be called local variables because they are local to
a function. By default they are assigned garbage value by the compiler.
main()
{
int detail;
//or
auto int detail; //Both are same
}
Storage Classes –
• External variables: Extern stands for external storage class. Extern
storage class is used when we have global functions or variables
which are shared between two or more files.
• Keyword extern is used to declaring a global variable or function
in another file to provide the reference of variable or function
which have been already defined in the original file.
• The variables defined using an extern keyword are called as global
variables. These variables are accessible throughout the program.
Notice that the extern variable cannot be initialized it has already
been defined in the original file.
Storage Classes –
First File: main.c
#include <stdio.h>
extern i;
main()
{
printf("value of the external integer is = %d\n", i);
}
Second File: original.c
#include <stdio.h>
i=48;
Result:
value of the external integer is = 48
Storage Classes –
Static variables Instead of creating and destroying a variable every
time when it comes into and goes out of scope, static is initialized
only once and remains into existence till the end of program.
• A static variable can either be internal or external depending upon
the place of declaration.
• Scope of internal static variable remains inside the function in which
it is defined.
• External static variables remain restricted to scope of file in which
they are declared.
• They are assigned 0 (zero) as default value by the compiler.
Storage Classes –
#include<stdio.h>
void test(); //Function declaration
int main()
{
test();
test();
test();
return 0;
}
void test()
{
static int a = 0; //Static variable
a = a+1;
printf("%d\t",a);
}
Output:
1 2 3
Storage Classes –
For example:
int students ( int david, int susan, int mary, int john );
• In this example, the identifiers (david, susan, mary , and john ) have scope beginning at their
declarations and ending at the closing parenthesis. The type of the function students is
"for the actual parameter names to be used function returning int with
four int parameters." In effect, these identifiers are merely placeholders after the
function is defined.
C Scopes
• Function Scope – A label is the only kind of identifier that always has Function
Scope. It can be used any where within the function in which an identifier -
labeled statement specifying the label name appears.
For example:
int func1(int x, int y, int z)
{
label: x += (y + z); /* label has function scope */
if (x > 1)
goto label;
}
int func2(int a, int b, int c)
{
if (a > 1)
goto label; /* illegal jump to undefined label */
}
Formatted Input and Output
Formatted Output: printf
• printf() provides the formatted output conversion.
• The syntax is as follows:
• int ptintf(char *format,...)
• It returns number of characters it printed.
• ... indicated the variable number of arguments.
example:
printf("control string %s",str);
• It takes two types of arguments
1. Ordinary characters
2. Conversion Specifications %
Formatted Input and Output
CHARACTER ARGUMENT TYPE PRINTED AS
d int decimal number
O int unsigned octal number
x,X int unsigned hexa decimal
number
u int unsigned decimal
number
c char single character
s char* print characters from the
string
f float Fractional number
Formatted Input and Output
#include<stdio.h>
int main()
{
int c;
c=getchar();
while(c!=EOF)
{
putchar(c);
c=getchar();
}
}
Operators
30
Classification of Operators
Classification Based on Number of Operands
Based upon the number of operands on which an operator operates, the
operators are classified as:
• Unary Operators: A unary operator operates on only one operand.
For example, in the expression -3, - is a unary minus as it operates on
only one operand.
• Binary Operators: A binary operator operates on two operands. It
requires an operand towards left and right. Example 3-4.
• Ternary Operator: It operates on three operands. Conditional
operator is the only ternary operator in C.
Operators based on classification
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
5. Conditional Operators
6. Special Operators (Miscellaneous Operators)
7. Bitwise operators
8. Increment and decrement operators
9. Unary operators
10. Equality operators
Miscellaneous Operators
1. Function call operator (())
2. Array subscript operator ([])
3. Member Selection operators
1. Direct Member access operator (Dot Operator(.))
2. Indirect member access operator (Arrow Operator (->))
4. Indirection operator (*)
5. Conditional operator
6. Comma operator
7. sizeof operator.
8. Address operator (&)
Arithmetic Operators
Operator Meaning Details
#include<stdio.h>
Output:
void main()
The biggest value is 125
{
int x=125,y=100,z;
z=((x>y)?x:y);
printf(“The biggest value is %d”,z);
}
Special Operators
• Special operators are known as separators. They are
• Ampersand ( & )
• Braces ( { } )
• Colon ( : )
• Ellipsis ( … )
• Asterisk ( * )
• Brackets ( [] )
• Comma ( , )
• Hash ( # )
• Parenthesis ( () )
• Semicolon ( ; )
Special Operators
Ampersand ( & )
• It is also known as address operator. It is used before the variable name. It indicates memory
location of the variable.
• It is denoted by ‘&’.
• Using ‘&’ prefix to the variable name it gives the address of that variable.
Example:
#include<stdio.h>
void main()
{
int n=10;
printf(“\n value of n is : %d”,n);
printf(“\n value of &n is : %u”, &n);
}
Output
value of n is : 10
Value of &n is:1002
Special Operators
Asterisk ( * )
Asterisk ( * ) is also known as indirection operator. It is used before identifier
name. It indicates creation of pointer variable.
Braces ( { } )
The opening brace ( { ) and closing brace ( } ) specify the start and end of
compound statement in a function.
Brackets
Brackets [] also referred as array subscript operator. It is used to indicate single
and multi dimensional arrays.
Eg: int x[10]; float l[10][20];
Colon ( : )
Colon ( : ) is used in labels. It is used in unconditional control statement i.e., in
goto statement.
Special Operators
Comma Operator ( , )
It is used to link expressions together. It is used together with variables to separate
one variable from another. It is used in for loop. It has the lowest precedence among
operators
Eg: for(n=1,m=10;n<=m; n++, m++)
int a,b,c;
sum= (x=5,y=3,x+y);
Ellipsis ( … )
Ellipsis ( … ) are three continuous dots with no white spaces between them. It is
used in function prototype. It indicates that the function can have any number of
arguments.
Eg: void fun(char s,int n, float f, …);
Hash ( # )
Hash ( # ) is also known as pound sign. It is used to indicate preprocessor directives.
Eg: #include<stdio.h>
Special Operators
Parenthesis ( () )
Parenthesis ( () ) is also known as function call operator. It is used to indicate
the open and end of function prototypes, function call, function parameters.
Parentheses are used to group expressions.
Semicolon ( ; )
Semicolon ( ; ) is a statement delimiter. It is used to end a C statement.
Eg: g=d+h;
Bitwise Operators
7 0 1 11
4 0 1 00
7 & 4 =4 0 1 00
Bitwise OR (|) operator
• It takes two bits at a time and perform OR operation .
• Bitwise OR returns 0 when both the bits are 0. Otherwise it
returns 1.
For Example: 7 | 4
7 0 1 11
4 0 1 00
7 1 4 =7 0 1 11
Bitwise XOR (^) operator
• It takes two bits at a time and perform XOR operation.
• Bitwise XOR returns 1 when two bits are different.
Otherwise it returns 0
For Example: 7 ^ 4
7 0 1 11
4 0 1 00
7 ^ 4 =3 0 0 11
Bitwise NOT (~) operator
• Bitwise NOT (~) is a Unary operator.
• Its job is to complement each bit one by one.
• Result of NOT is 0 when the bit is 1, and the result is 1 when
the bit is 0.
For Example: ~7
7 ~ 0 1 11
8 1 0 00
~7 = 8
Difference between bitwise and logical
operators
#include<stdio.h>
int main()
{
char x=1, y=2; //x=1(0000 0001),
//y=2 (0000 0010)
if(x&y) //1&2 = 0 (0000 0000)
printf(“Result of x & y is 1”);
if(x&&y) //1&&2 = TRUE && TRUE = TRUE =1
printf(“Result of x && y is 1”);
}
Bitwise Left Shift (<<) operator
Left Shift << operator is a binary operator.
V << 1
3 = 0_ _0 _0 _0 0
_ 0_ 1_ _1
Left shift by
one position _ 0_ 0_ 0_ 0_ 1_ 1_ _0
0
Result of v << 1 = 6
How left shift works???
• Left shifting is equivalent to left operand is multiplied by 2
power(right operand)
Example:
1 is our right
V= 3
operand
V << 1
1
Result of V << 1 = [ 3X2 ]=6
3 is our left
operand
2
Result of V << 2 = [ 3 X 2 ] = 12
Bitwise right shift (>>) operator
Right Shift >> operator is a binary operator.
Result of v >> 1 = 1
Contd..
• Right shifting is equivalent to division by 2 power(right operand)
2
V= 32 Result of V >> 2 = [ 32 / 2 ] = 8
Bitwise Operators
X = ++ a; X = a ++;
X a X a
6 5 6 5 5 6
X = 6, a=6 X = 5, a=6
C program to perform increment operation
#include<stdio.h>
void main( )
{
int a,b,x=10,y=10; Output
a = x++; The value of a : 10
b = ++y; The value of b : 11
printf(“The value of a is: %d”,a);
printf(“The value of b is: %d”,b);
}
Decrement Operators
• It is used to decrease the value of the Operand by 1. There are two
types of Decrement Operators in C Language. They are pre
decrement operator and post decrement operator.
Prefix decrement Operator
• It is used to decrease the value of the variable by 1. Here the value of 1 is subtracted to
the variable first along with the given variable value.
• Eg: --g ------> prefix decrement (Decrement g, then evaluate g)
• int x = 5;
• int y = --x; // x is now equal to 4, and 4 is assigned to y
#include<stdio.h>
Output
void main( ) The value of a : 10
{ The value of b : 9
int a,b,x=10,y=10;
a = x--;
b = --y;
printf(“The value of a is: %d”,a);
printf(“The value of b is: %d”,b);
}
Unary Operators
• Unary operators act on single operand to produce new value. It precedes
with operands.
Operator Meaning
- Unary minus
+ Unary Plus
++ Increment by 1
-- Decrement by 1
Sizeof Return the size of operand
Eg:
• -786 -0.64 -(a+b)
• When operator is used before variable then it is prefix notation. When
operator is used after variable then it is postfix notation.
Unary Operators
• Eg: x=3
Expression Result
++x 4
x++ 3
--x 2
x-- 3
• Size-of Operator
• This operator is used to return the size of string or character. It cannot be used in
together with Integers. The syntax is
• sizeof(variable-name);
Output:
#include<stdio.h>
void main() The value of x is 2
{
int x;
printf(“The value of x is %d”,sizeof(x));
}
Precedence of Operators