Std-10-Computer-Chapter 11 Data types, Operators and Expression in C Language
Std-10-Computer-Chapter 11 Data types, Operators and Expression in C Language
DATA TYPES
Data types specify the type of value that can be stored & the memory space
is required by an identifier
The type of a value that can be assigned to an identifier is known as its Data type.
For example, int date;
In this statement identifier is date with integer data type and has been assigned a
integer value (1 to 31) only. We can’t assign any floating point numbers in this
integer data type. We will use some other data type for floating point numbers.
REAL
Real data type store decimal digits.
Real values in c language supports a data type identified by keyword float. It uses
4 bytes of storage space.
float Float uses 4 bytes of memory space.
“%f” is a delimiter of float
Float value are precise to 6 and at times 7 decimal digits.
double Double uses 8 bytes of memory space
“%lf” is a delimiter of double
Double is more precise than float having 16 and at times 17 decimal digits.
long Long double uses 16 bytes of memory space
double “%Lf” is a delimiter of long double
Example: 95.76 0.9576e2 954.76 0.95476e3
mantissa exponent mantissa exponent
CHARACTER
Character data types are represent by ‘char’ keyword
char uses 1 byte of memory space.
Delimiter : %c
Each character is associated with an integer value called ASCII (American
Standard Code for Information Interchange)
Char - 2n-1 to 2n-1 -1
- 2 8-1 to 28-1 -1
- 2 7 to 27-1
-128 to 128-1
-128 to 127
unsigned char 0 to 2n -1
0 to 28-1
0 to 256 -1
0 to 255
ASSIGNING VALUES TO VARIABLES
Variables once declared can be assigned values during the execution of the
program.
variable = value; eg. age=14; pi=3.14;
It is also possible to initialize and declare variable in same statement.
datatype variable = value; eg. int age=14; float pi=3.14;
USER DEFINED DATA TYPE
C is a flexible language that allows user to create new entities from existing one. User
can create a new data type by making use of existing data type.
Two keywords typedef and enum are used in c for generating user defined data type.
Both these keywords define a variable that can be used as data type.
User defined data types improve the readability of C programs. It allows user to define
data types with meaningful name.
Array
Array is a data structure consists of group of variables having same property.
datatype variable[size];
char name[] = “Nuzhat Memon”;
would define an array called ‘name’ having size 12. The compiler will automatically
decide the length of the array.
int marks[15];
The above statement can define marks of 15 students in a single variable called ‘marks’.
OPERATORS IN C LANGUAGE
An operator is a symbol that identifies the operation that can be performed on operands.
EXAMPLE: 5 + 3 9–7
here 5 and 3 are operands and + is the operator.
In the same way, 9 and 7 are operands and – is the operator.
The operators in C can be categorized into eight types.
Based on the operand used in the expression, the arithmetic can be categorized as
integer arithmetic, real arithmetic or mixed mode arithmetic.
Integer Arithmetic
Arithmetic is considered to be integer arithmetic when the operands used in the
expression are positive
itive or negative whole numbers.
The result of integer arithmetic is always integer.
int total=650, qty=24;
cost=total/qty;
printf(“%d”,cost);
RESULT: 27 (The result of integer arithmetic is always integer)
Real Arithmetic
If all operands used in an expression are float, the expression is called real expression.
The result of real arithmetic is always represented in decimal value.
float total=650, qty=24;
cost=total/qty;
printf(“%f”,cost);
RESULT: 27.083334 (The result of real arithmetic is always d
decimal
ecimal value)
Assignment Operators
The symbol ‘=’ is known as assignment operator.
It is used to assign a constant value or result of an expression to a variable.
eg. int age=10; first=first*(second+third);
This operator is also used as short hand operator.
variable op=constant_value; or variable op=expression;
eg a += 10; //a=a+10; a+=b; //a=a+b;
Relational Operators
The relational operators allow us to compare two similar types of operands and generally
are used to change the flow of execution of program.
Operator Meaning
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
== Equal to
!= Not equal to
SYNTAX
expression1 relational_op expression2 eg. a >= b p != q
Relational operators are used along with decision structures like ‘if’ and control structures
like “for”, “while” and “do…while”.
Increment/Decrement Operators
We need to increase/decrease the value of a variable by 1.
variable += 1 or variable -= 1 is the short hand operator for incrementing /decrementing.
C provides two special unary operators “++” and “_ _“. The “++” is known as increment
operator while “- -“ is known as decrement operator.
The “++” and “--“ are used as either prefix or suffix to a variable.
++ variable or variable ++; - - variable or variable - -
pre increment post increment pre decrement post decrement
Operator Meaning
++x pre-increment
--x pre-decrement
x++ post –increment
x-- post-decrement
The result of this increment/decrement in variable value by 1.
Conditional Operators
Conditional operators are also known as ternary operator.
This operator is identified by combination of two symbols ? and :
Syntax:
condition ? True statement : False statement;
Logical Operators
To evaluate multiple conditions together to give some output.
&& and || are used to combine two conditions.
Operator Sign Use
AND && when all the given conditions must be satisfied
OR || When any one of the given conditions must be satisfied
NOT ! Operand is true, result is false & vice versa
Logical expression gives as an output values either 0 or 1. Here 0 refers to false and 1
refers to true.
Bitwise Operators
Data is stored in the form of bits within the memory location. C allows us to operate
directly at bit level using bitwise operators.
To perform bitwise AND, OR, Exclusive OR(XOR), Left shift and Right shift we require
two operands.
NOT requires one operand only.
& Bitwise AND
| Bitwise OR
~ Bitwise NOT
^ Bitwise Exclusive OR
<< Left shift
>> Right shift
sizeof()
Special Operators ,
sizeof() operator is a special operator used to return size of bytes
.
required to store an entity.
For eg sizeof(int) will result into 4 because data type int uses 4 bytes
of memory space. &
*
Priority of Operators
The multiplication (*) was at higher priority then +, - or /
Operator Operation used for Associativity Precedence
() Function call Left to Right First
[] Array expression
++ Increment Rigth to Left Second
- - Decrement
sizeof() Size of operand
* Multiplication Left to Right Third
/ Division
% Modulo division
+ Binary addition Left to Right Fourth
- Binary subtraction
Type Conversion
An expression can be evaluated only if all the operands involved are of same data type.
This property of an expression requires internal conversion of data types to be
performed.
int
Automatic Variables
All the variables by default have storage class as automatic.
To explicitly define the variable as automatic, we use the keyword auto.
auto datatype identifier;
The definition auto int number, indicates that the variable number is of type integer and
its storage class is automatic.
Such variables are created automatically when the function is called and destroyed
when the function returns the control back to the calling function.
These variables are by default, are assigned a garbage value and are stored in the
primary memory.
External Variables
At times the programmer needs to share a variable between two functions or programs.
We can use storage class external to share variables between two different functions or
programs.
It is necessary though that the variable must be defined as global variable in atleast one
program.’
extern datatype identifier;
The definition extern char choice, indicates that the variable choice is of type character
and its storage class is external.
The default value of the external variable is zero, and they stored in the primary
memory.
Register Variables
To have fast access to variables, we can store the variables in the CPU registers.
C language provides us a storage class called register to store the variables in the CPU
registers.
register datatype identifier;
The register class variables are assigned garbage value by default.
Static Variables
The static storage class when used along with variable makes the variable value
permanent within a specified region.
A static storage class variable is stored permanently in the primary memory and by
default, is assigned value zero.
static datatype identifier;