0% found this document useful (0 votes)
2 views53 pages

Unit2 DataTypes

The document provides an overview of the C programming language, focusing on data types, operators, and type conversion. It outlines primary data types such as integers, floats, and characters, as well as derived and user-defined data types. Additionally, it explains various operators including arithmetic, relational, logical, and special operators, along with type conversion methods.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views53 pages

Unit2 DataTypes

The document provides an overview of the C programming language, focusing on data types, operators, and type conversion. It outlines primary data types such as integers, floats, and characters, as well as derived and user-defined data types. Additionally, it explains various operators including arithmetic, relational, logical, and special operators, along with type conversion methods.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

BASICS OF C LANGUAGE

Data Types
• Data types specifies that kind of data that we want to
store in a variable.
• The Data type of a variable also determines how much
storage space is required to store a variable.
• Data type is used for declaration of a variable.
• For ex: int a,b,c;
In this example, a,b,c are variables names (identifier) that has
integer data type means numeric value will be stored in a,b and c
variable.
• There are four types of Data types −
– Primary Data type/Basic Data Type
– Void data type
– Derived Data type
– User Defined data type
1. Primary Data type/Basic Data Type:
1. Integer Data Type
2. Float Data Type
3. Character Data Type
4. Double Data Type
• Integer data type requires 2 bytes size in
memory.
• 2 bytes=16 bits (1 byte=8 bits)
• int a=10; 10 Binary 1010

0000000000001010
• Range of integer data type is -32768 to +32767.
• For Integer data type %d control string/Format
string is used.
Data type Range Byte Format
Size
2. Float Data type
• Float data types are used to store real numbers.
• We can use float keyword for declaring a float variable.
– For ex: float no1;
In this example, we declare no1 variable of float data type.
• For Float data type %f control string/Format string is used.
• Float data type requires 4 byte size in memory.

3. Double Data type


• Double data types are used to store real numbers.
• We can use double keyword for declaring a double variable.
– For ex: double no1;
In this example, we declare no1 variable of double data type.
• For double data type, %lf control string/Format string is used.
• Double data type requires 8 byte size in memory.
Long Double Data type
• Long Double data types are used to store real numbers.
• We can use long double keyword for declaring a long double
variable.
– For ex: long double no1;
In this example, we declare no1 variable of long double data
type.
• For long double data type, %Lf control string/Format string is
used.
• Long double data type requires 10 byte size in memory.

Data Type Range Byte Size Format


4. Character Data type
• Character data type is used to store single character like
alphabet, numbers or any special character that is specified in
single quote.
• We can use char keyword for declaring a character variable.
– For ex: char a;
In this example, we declare a variable of character data type.
• For character data type %c control string/Format string is used.
• Character data type requires 1 byte size in memory.
1 Byte
2. void Data Type :
• void data type means no value is available.
• void data type specifies that it does not return
any value.
• It is used with function and pointer.
• It is not used to declare a variable.
3. Derived Data Types:
• Derived data types are data types which are derived from primary
data types.
• The derived data types are also called as user-defined data types
or secondary data types.
Primary Data Types

Derived Data Types

• To declare derived data types, following concepts are used:


Arrays
Structures
Unions
4. User Defined Data Type:
• A data type which is defined by user is known as
user defined data type.
• Two keywords are used to declare user defined
data type.
1. typedef
2. enum
1. typedef:
typedef keyword is used to give user defined
data type name to an existing data type.
For example:
typedef int priyanka;
priyanka a=1;

In above example,
In first example, we give user defined name
“priyanka” for int data type.
So, I can use priyanka instead of int data type.
2. enum:
• enum is also user-defined data type which is
used to assign an integer index value from a
specific set of values.
• enum keyword is used to declare enumerated
data type.
• Example:
• Operators-
✔ Operator is a symbol that is used to perform
specific operation.
✔ There are eight operators:
▪ Arithmetic Operators
▪ Relational Operators
▪ Logical Operators
▪ Assignment Operators
▪ Increment and Decrement Operators
▪ Ternary Operators
▪ Bit-wise Operators
▪ Special Operators
❖ 1. Arithmetic Operators :
• Arithmetic Operator is used to perform arithmetic operation on two operands.
• Syntax: <Operand1> <Arithmetic operator> <Operand2>
• Here, Operand can be variable or value.
• For ex:
A+B , A and B are variables.
10 + 20, 10 and 20 are values.
• % operator is performed on integer numbers only (not used on
float numbers).
• There are five arithmetic operators:
Output:
• Precedence/Priority of Operator:
✔ When we have more then one operator in an equation,
equation is evaluated based on the precedence of an
operator.
✔ An operator which has high priority will be evaluated
first.
✔ If more than one operator have same priority then they
will be evaluated from left to right.
✔ Precedence of operator is shown in following table.
Operator Operation Precedence
() Parentheses 1
/ * % Division, Multiplication, Remainder 2
+ - Addition, Subtraction 3
• For ex: 8 * ( 3 + 5 * 2 / 2 – 2 ) % 2

=8 * ( 3 + 10 / 2 – 2 ) % 2

=8 * ( 3 + 5 – 2 ) % 2

=8 * ( 8 – 2 ) % 2

=8 * 6 % 2
=48%2
=0
• In equation, when two or more operands have
different data types then answer will be
displayed in that data type which has higher data
byte size.
For ex: When we want to perform addition on two
operands:
Data type of Variables: Answer/ Output

Character variable + Integer Variable Integer

Integer variable + Float Variable Float

Float variable + Double Variable Double

Double variable + Long Double Variable Long Double


❖ 2. Assignment Operator:
✔ Assignment operator = is used to assign a value to
a variable.
✔ Syntax: <variable> = value/variable/expression;
✔ For ex:
1. x=10; In this example, value 10 is assigned to x
variable.
2. x=y; In this example, value of y variable is
assigned to x variable.
3. x=(a+b+c); In this example, answer of (a+b+c)
expression is assigned to x variable.
• Short hand assignment operator:
• When we use operator with assignment operator =, then
it is known as short hand assignment operator.
• Short hand assignment operator is used when we have
same operand or expression at left and right side of
assignment operator.
• There are various short hand assignment operators:
Short hand Meaning Example (Assume A=10)
assignment
operator
+= A+=1; means A=A+1; A=11
-= A-=1; means A=A-1; A=9
/= A/=1; means A=A/1; A=10
*= A*=1; means A=A*1; A=10
%= A%=1; means A=A%1; A=0
• A+=(B*D); means A=A+(B*D)

• A-=(C/D); means A=A-(C/D)


❖ 3. Increment - Decrement Operator:
✔ Increment operator(++) is used to increment a
value of variable by 1.
✔ Decrement operator(--) is used to decrement a
value of variable by 1.
✔ For example:
1. a=6;
a++; means a=a+1; a= 7
2. a=14;
a--; means a=a-1; a=13
✔ Three are two types of increment operators:
1. Post Increment operator (variable ++)
–When ++ increment operator is used after a variable then it
is known as post increment operator.
–Post increment operator assign a initial value to a variable
then increase the value of variable by 1.
–For ex: a=6;
printf(“\n %d”,a++); a=6;
printf(“\n %d”,a); a=7;
2. Pre Increment operator (++variable)
–When ++ increment operator is used before a variable then
it is known as pre increment operator.
–Pre increment operator increase the value of variable by 1
and then assign a value to variable.
–For ex: a=6;
printf(“\n %d”,++a); a=7;
printf(“\n %d”,a); a=7;
✔ Three are two types of decrement operators:
1. Post Decrement operator (variable --)
–When -- decrement operator is used after a variable then it is
known as post decrement operator.
–Post decrement operator assign a initial value to a variable then
decrease the value of variable by 1.
–For ex: a=6;
printf(“\n %d”,a--); a=6;
printf(“\n %d”,a); a=5;

2. Pre Decrement operator (--variable)


–When -- decrement operator is used before a variable then it is
known as pre decrement operator.
–Pre decrement operator decrease the value of variable by 1 and
then assign a value to variable.
–For ex: a=6;
printf(“\n %d”,--a); a=4;
printf(“\n %d”,a); a=4;
4. Relational Operators:
• A relational operator is used to compare two values.
• It checks the relationship between two operands. If
the condition is true, it returns 1; if the condition is
false, it returns value 0.
• There are 6 relational operators:
1. > Greater than
2. >= Greater than equals to
3. < less than
4. <= less than equals to
5. == equals to
6. != does no equal to
For ex:
• For ex:
• If(5>5) condition is false, so it returns 0.
• If(5>=5) condition is true, so it returns 1.
• If(5<5) condition is false, so it returns 0.
• If(5<=5) condition is true, so it returns 1.
• If(5==5) condition is true, so it returns 1.
• If(5!=5) condition is false, so it returns 0.
• 5. Bitwise Operator
• Bitwise operator is used to perform operation
on bits- 0 and 1.
• Bit-wise AND ( & ):
• The result of bitwise AND is 1 if the corresponding bits of two
operands is 1, otherwise the result of corresponding bit evaluated as
0.
• Bit-wise OR ( | ):
• The result of bitwise OR is 1 if any one of the corresponding bit of two
operands is 1, otherwise the result of the corresponding bit evaluated
as 0.
• Bit-wise NOT ( ~ ):
• The result of bit-wise NOT is the one’s complement of the operand.
i.e. ~N=-(N+1) ex: ~7=-(7+1) =-8
• Bit-wise XOR ( ^ ):
• The result of bit-wise XOR is 1 if the corresponding bit of two
operands is opposite, otherwise the result of the corresponding bit
evaluated as 0.
• Left Shift ( << ):
• The value of the left operand is shifted to left by the value specified
by the right operand.
• Right Shift ( >> ):
• The value of the left operand is shifted to the right by the value
specified by the right operand.
• For example:
A=5 Binary of 5 = 0 1 0 1
A=5 Binary of 5 = 0 1 0 1
B=7 Binary of 7 = 0 1 1 1
B=7 Binary of 7 = 0 1 1 1
OR: A | B = 0 1 1 1
AND : A & B = 0 1 0 1
A=5 Binary of 5 = 0 1 0 1
A=5 Binary of 5 = 0 1 0 1 B=7 Binary of 7 = 0 1 1 1
NOT: ~A : -(N+1) =-6 XOR: A ^ B = 0 0 1 0

A=5 Binary of 5 = 0 1 0 1
Shift left operation: A<<1= 1 0 1 0
Shift right operation: A>>1= 0 0 1 0
6. Logical Operators:
• Logical operators are used to check multiple
conditions.
• Logical operators are used with decision making
statements.
• There are 3 logical operators:
7. Ternary Operator or Conditional Operator:
• ? And : are ternary operator that is used to check
condition.
• If condition is true then true block will be
executed, otherwise false block will be executed.
• Syntax:
Variable=condition?trueblock:falseblock;
Ex:
In Funfair, If child age is >=14, then consider entry
fees=40, otherwise consider entry fees=20.

entry_fees=(age>=14)?40:20;

In this example, condition (age>=14) will be


checked. If condition is true then 40 is assigned
to enrty_fees. If condition is false then 20 is
assigned to entry_fees.
8. Special Operator:
• In C language, there are various special operators
like , (comma),
sizeof(),
& (address operator),
* (pointer operator), etc.
For example,
int a,b,c; comma is used to declare multiple
variables.
printf(“%d”,sizeof(a)); sizeof operator is used
find out byte size of any variable.
scanf(“%d”,&a); & specifies address of a.
Type Conversion:
• Type conversion is a conversion from one data
type to another.
• There are two type of type conversions:
– 1. Implicit Type conversion
– 2. Explicit Type conversion
• 1. Implicit type conversion:
– Implicit type conversion is done by compiler
automatically. (Automatic Type Conversion)
– In equation, when two or more operands have different
data types then answer will be displayed in that data type
which has higher data byte size.
– Implicit Type Conversion :char int float double
– For ex:
int a=6;
char c=‘A’; //It is converted into ASCII value.
a=a+c; //addition of int and character variables
printf(“\n Addition is:%d:,a);

Output: Addition is:71


a=6+65=71
• 2. Explicit type conversion:
– Explicit type conversion is done by user.
– Syntax: (type) expression
For example:
int a=5;
double d=20.5;
a=a+(int)d ; //double is converted into integer
printf(“additiion is:%d”,a);

Output: addition is:25


In this example, double variable is converted into
integer, so answer will be displayed in integer.
Mathematical Functions:
• Mathematical Functions are used to perform
mathematical operations.
• For mathematical functions, we can include
math.h header file.
• Mathematical System defined Functions:
• pow() : pow() function is used to find out power of any
value.
• Syntax: pow(variable,base) or pow(value,base);
• Ex:
a=pow(8,2); a=64
a=pow(5,3); a=125
• sqrt() : sqrt() function is used to find out square root of
any value.
• Syntax: sqrt(variable) or sqrt(value);
• Ex:
a=sqrt(9); a=3
a=sqrt(81); a=9
• ceil(x) : ceil() function is used to find out smallest
integer value greater than or equal to x.
• Syntax: ceil(variable); or ceil(value);
• Example: a=ceil(3.5); a=4
a=ceil(5.9); a=6
a=ceil(3); a=3
a=ceil(9.5); a=10
a=ceil(-3.4); a=-3
a=ceil(-5.8); a=-5

-5 -4 -3 -2 -1 0 1 2 3 4 5 6
• floor(): floor() function is used to find out largest
integer value less than or equal to x.
• Example: a=floor(3.5); a=3
a=floor(5.9); a=5
a=floor(3); a=3
a=floor(9.5); a=9
a=floor(-3.4); a=-4
a=floor(-5.8); a=-6
• Input Output Functions:
• printf()
• scanf()
• getchar() : This function is used to get a character
from user.
Syntax: variable_name=getchar();
Ex: char c;
c=getchar();

• putchar() : This function is used to display a


character on console screen.
Syntax: putchar(variable_name);
Ex: putchar(c);
• gets() : This function is used to get a string
from user.
Syntax: gets(variable_name);
Ex: char str[10];
gets(str);

• puts() : This function is used to display a string


on console screen.
Syntax: puts(variable_name);
Ex:
puts(str);

You might also like