0% found this document useful (0 votes)
33 views9 pages

Std-10-Computer-Chapter 11 Data types, Operators and Expression in C Language

Ch 11 computer

Uploaded by

saratsahu4453
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
33 views9 pages

Std-10-Computer-Chapter 11 Data types, Operators and Expression in C Language

Ch 11 computer

Uploaded by

saratsahu4453
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 9

Chapter 11

Data types, Operators and Computer


Expression in C Language Class 10

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.

BASIC DATA TYPES OF C


 ‘C’ language supports basic data types called integer, decimal and character.
 Integer data type is represented using keyword int.
 Decimal data type is represented using keyword float.
 Character data type is represented using keyword char.
 We also have one more primitive (Basic) data type known as void.
 An identifier is associated with its data type by using the syntax:
Data type identifier 1 [,identifier2, identifier3,………………., identifier n];
 The content written in square brackets for the above given syntax are optional.

Presented by Nuzhat Memon 1


INTEGER
 Integer data types are represent by ‘int’ keyword
 In ANSI C the data type int uses 4 bytes of memory space.
 For example, int date,month,year;
 All these 3 variables ‘date’,’month’ and ‘year’ are signed integer ie they are capable of
storing both a positive (+) or negative (-) whole number with no fractional part in it.
 In many situations we may not require a negative value at all. It is possible to allocate
only positive values in C language.
 For example, unsigned int marks;
 ‘marks’ is identifier of type integer and are capable of storing only positive (+) value.
int 4 bytes of memory space (Store are - 2n-1 to 2n-1 -1
capable of storing both a positive - 2 32-1 to 2 32-1 -1
or negative whole number) - 2 31 to 2 31 -1
Range: -2147483648 to 2147483647 - 2147483648 to 2147483648-1
Delimiter : “%d” -2147483648 to 2147483647
unsigned int 4 bytes of memory space.(Store only 0 to 2n -1
positive integer) 0 to 232-1
Range: 0 to 4294967295 0 to 4294967296-1
Delimiter : “%u” 0 to 4294967295
long int It is also possible to increase the
range of integer number by using
prefix long with the integer data type.
Long int uses 8 bytes of memory
space and Delimiter is “%ld”
 In the above formula, n refers to number of bits required. 1 byte is equal to 8 bits.

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

EMPTY DATA SET


 C provides a special data type identified by keyword void.
 This data type has no value; hence we denote it to be empty.
Presented by Nuzhat Memon 2
int main(){ void main(){
…… …….
….... …….
return 0; }
}
 Function in C may return a value. int main function may return a integer value. So
last statement in int main function is the return statement.
 If we don’t want the function to return a value then we can prefix it with void.

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.

Type Definition (typedef)


typedef existing_datatype variable;
typedef long double num;

Presented by Nuzhat Memon 3


 In this statement, “num” is an alias to ‘long double’ data type. So after providing
alias, we can use num only instead of writing ‘long double’.
 Here C does not allow us to create a new data type. Instead it allows us to give an
alias to an existing data type.

Enumerated data type (enum)


enum identifier (value1, value2,value3,………………..,value n};
enum day{mon,tues, wed,thur,fri,sat};
printf(“%d”,thur);
 The variable ‘day’ defined using keyword ‘enum’ can be assigned any numeric
value. By default, it gives 0,1,2 and so on.
 In this statement, the value of mon is 0 so the value of thur will be printed as 3.
enum money{rupee=10,dollar=50,pound=75,yen=100};
printf(“%d”,dollar);
 The values are given in curly brackets. So the value of dollar will be 50 as given.

DERIVED DATA TYPE


 C is an extensible language.
 Derived data types are derived from primitive(basic) datatype.
 Array, structure, union, pointers are examples of derive data types.

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.

Presented by Nuzhat Memon 4


Arithmetic Operators
 To perform an arithmetic operation C provides various basic operators like ‘+’, ‘-‘,
‘ ’*’, “/’, ‘%’
Operator Meaning Example
+ Addition of two operands or unary plus 2+8
– Subtraction of two operands or unary minus 2-8
* Multiplication of two operands 2*8
/ Division of two operands resulting in Quotient 8/2
% Division of two operands resulting in Reminder 8%3
% operator known as Modulo operator
Note it can’tt be used with float operands.

 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)

Mixed Mode Arithmetic


 In
n this mode of arithmetic we are able to use integer as well as float operands within an
expression.

Presented by Nuzhat Memon 5


 The output will depend upon the date type of the variable to which value is assigned.
result = 25.75 * 5;
 In mixed mode arithmetic, C allows automatic conversion of lower range data type to
higher range data type. So in above statement, result will be 128.75 (conversion to
higher range data type ie float)

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.

Presented by Nuzhat Memon 6


EXAMPLE:
int first=15, second=20, result; int first=15, second=20, result;
result=first + second++; result=first + ++second;
= 15 + 20 = 15 + 20
= 35 = 36
 The post-increment operator, when used in an expression uses the old value of a variable
to evaluate the expression, then it increments the value of variable by 1.
 The pre-increment operator, when used in an expression, first increment the value of
variable by 1 then uses the new value of a variable to evaluate the expression.

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. &
*

Presented by Nuzhat Memon 7


Evaluation of Expression
result = first + second * third – fourth;
 Here first multiplication would be done then be added and then subtracted.
 If two operators with same priority appear in an expression, then they will be evaluated
from left to right.

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

 It is possible to override the internal conversion by performing a process called type


casting.
(datatype) variable or (data type) constant

Presented by Nuzhat Memon 8


Storage classes
 The variables used on C program are generally stored in primary memory of the
computer.
 It is also possible to store some variables in registers.
 C language provides us four storage classes namely automatic, external, register and
static.
 These storage classes allows user to specify the location where he/she wants to store
the variable.

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;

Presented by Nuzhat Memon 9

You might also like