CPDS_UNIT-1(Part-2)_Notes
CPDS_UNIT-1(Part-2)_Notes
C-Operators
An operator is a symbol used to perform arithmetic and logical operations in a
program.
That means an operator is a special symbol that tells the compiler to perform
mathematical or logical operations.
C programming language supports a rich set of operators that are classified as
follows.
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Increment & Decrement Operators
5. Assignment Operators
6. Bitwise Operators
7. Conditional Operator
8. Special Operators
The addition operator can be used with numerical data types and character data
type. When it is used with numerical values, it performs mathematical addition
and when it is used with character data type values, it performs concatenation
(appending).
The remainder of the division operator is used with integer data type only.
Example: Write a C program to illustrate arithmetic operators.
/* Arithmetic Operator * / % + - */
#include<stdio.h>
int main()
{
int a,b,c;
float x;
printf("\nEnter 2 Nos : ");
scanf("%d%d",&a,&b);
c=a+b;
printf("\nTotal : %d",c);
c=a-b;
printf("\nDifference : %d",c);
c=a*b;
printf("\nMul : %d",c);
x=(float)a/(float)b;
printf("\nDiv : %0.2f",x);
c=a%b;
printf("\nMod : %d",c);
return 0;
}
Every relational operator has two results TRUE or FALSE. In simple words, the
relational operators are used to define conditions in a program.
//Relational Operators
#include<stdio.h>
int main()
{
int a=10,b=5;
printf("Greater Than : %d",a>b);
printf("\nLess Than : %d",a<b);
printf("\nGreater Than or Equal : %d",a>=b);
printf("\nLesser Than or Equal : %d",a<=b);
printf("\nEqual : %d",a==b);
return 0;
}
OutPut:
Greater Than : 1
Less Than : 0
Greater Than or Equal : 1
Lesser Than or Equal : 0
Equal : 0
//Logical Operators
int main()
{
int a=32; //>=35 and(&&) <=100
printf("\nLogical And : %d",(a>=35 && a<=100));
printf("\nLogical Or : %d",(a>=35 || a<=100));
printf("\nLogical Not : %d",!(a>=35));
return 0;
}
Output: Logical And : 0
Logical Or : 1
Logical Not : 1
4. Increment & Decrement Operators (++ & --)
The increment and decrement operators are called unary operators because
both need only one operand.
The increment operator adds one to the existing value of the operand and the
decrement operator subtracts one from the existing value of the operand.
C has two special unary operators called increment (++) and
decrement (--) operators.
Unary Increment Operator, denoted by "++"
Unary Decrement Operator, denoted by "--"
}
Output: i = 6, j = 5
5. Assignment Operators (=, +=, -=, *=, /=, %=)
The assignment operators are used to assign right-hand side value (Rvalue) to
the left-hand side variable (Lvalue).
int main()
{
int a=10,b=5; Output : A : 15
//+= A : 5
a+=b; //a=a+5
printf("A : %d",a); //15
a-=10; //a=a-10
printf("\nA : %d",a);
return 0;
}
7. Conditional Operator (? :)
The conditional operator is also called a ternary operator because it requires
three operands.
This operator is used for decision making. In this operator, first we verify a
condition, and then we perform one operation out of the two operations based on
the condition result.
If the condition is TRUE the first option is performed, if the condition is FALSE the
second option is performed.
The conditional operator is used with the following syntax.
1. // conditional operator
2. #include <stdio.h>
3. #include <conio.h>
4. int main()
5. {
6. int age; // variable declaration
clrscr();
7. printf("Enter your age");
8. scanf("%d",&age); // taking user input for age variable
9. (age>=18)? (printf("Eligible for voting")) : (printf("Not eligible for voting"));
10. return 0;
11. }
Output: Enter your age 12
Not eligible for voting
Enter your age 21
Eligible for voting
Example 2: C program to check whether the given number is even or odd using
conditional operator
#include <stdio.h>
int main()
{
int number;
printf(“\n Enter the number”);
scanf(“%d”,&number);
(number % 2 == 0) ? printf("Even Number") : printf("Odd Number");
return 0;
}
Output: Enter the number 5
Odd number
8. Special Operators (sizeof, pointer, comma, dot, etc.)
The following are the special operators in c programming language.
sizeof operator
This operator is used to find the size of the memory (in bytes) allocated for a variable.
This operator is used with the following syntax.
What is an expression?
An expression is a collection of operators and operands that represents a specific
value.
Expression Types in C
In the C programming language, expressions are divided into THREE types. They are as
follows...
Infix Expression
Postfix Expression
Prefix Expression
Infix Expression
The expression in which the operator is used between operands is called infix
expression.
The infix expression has the following general structure.
Postfix Expression
The expression in which the operator is used after operands is called postfix expression.
The postfix expression has the following general structure.
In c programming language the operator precedence and associativity are as shown in the
following table.
A+B*C/D-E%F
Program:
//used to include basic C libraries
#include <stdio.h>
#include <conio.h>
//main method for run c application
int main()
{
//declaring variables
int a,b,result_art_eval;
clrscr();
//Asking the user to enter 2 numbers
printf("Enter 2 numbers for Arithmetic evaluation operation\n");
//Storing 2 numbers in varaibles a and b
scanf("%d\n%d",&a,&b);
//Arithmetic evaluation operations and its result displaying
result_art_eval = a+b*a/b-a%b;
printf("================ARITHMETIC EXPRESSION EVALUATION==============\n");
printf("Arithmetic expression evaluation of %d and %d is = %d \n",a,b,result_art_eval);
printf("=========================================================");
return 0;
}
2. Relational expression evaluation
== (equal to), != (not equal to), > (greater than), < (less than), >= (greater than or equal
to), <= (less than or equal to) operators are said to “Relational expressions”. This
operator works in between operands. Used for comparing purpose. Like A==B, A!=B,
A>B, A<B etc. While we perform the operation with these operators based on specified
precedence order:
A==B || A!=B||A<B||A>B;
C-Program:
//used to include basic C libraries
#include <stdio.h>
//main method for run c application
int main()
{
//declaring variables
int a,b;
int result_rel_eval;
//Asking the user to enter 2 numbers
printf("Enter 2 numbers for Relational evaluation operation\n");
//Storing 2 numbers in varaibles a and b
scanf("%d\n%d",&a,&b);
//Relational evaluation operations and its result displaying
//If we get result as 1 means the value is true and if it is 0 then value is false in C
result_rel_eval = (a<b||a<=b||a>=b||a>b||a!=b);
printf("================RELATIONAL EXPRESSION EVALUATION==============\n");
if(result_rel_eval==1)
{
printf("Relational expression evaluation of %d and %d is = %d \n",a,b,result_rel_eval);
}
else
{
printf("Relational expression evaluation of %d and %d is = %d \n",a,b,result_rel_eval);
}
printf("===============================================================");
return 0;
A&&B||B!A;
C-Program:
#include <stdio.h>
int main()
{
//declaring variables
int a,b;
int result_log_eval;
//Asking the user to enter 2 numbers
printf("Enter 2 numbers for Logical evaluation operation\n");
//Storing 2 numbers in varaibles a and b
scanf("%d\n%d",&a,&b);
//Logical evaluation operations and its result displaying
//If we get result as 1 means the value is true and if it is 0 then value is false in C
result_log_eval = (a||b&&a);
printf("================LOGICAL EXPRESSION EVALUATION==============\n");
if(result_log_eval==1)
{
printf("Logical expression evaluation of %d and %d is = %d \n",a,b,result_log_eval);
}
else
{
printf("Logical expression evaluation of %d and %d is = %d \n",a,b,result_log_eval);
}
printf("===============================================================");
return 0;
}
(X+2=10)?’true’:’false’;
C-Program:
In a programming language, the expression contains data values of the same data type
or different data types. When the expression contains similar data type values then it is
evaluated without any problem. But if the expression contains two or more different data
type values then they must be converted to the single data type of destination data type.
Here, the destination is the location where the final result of that expression is stored.
For example, the multiplication of an integer data value with the float data value and
storing the result into a float variable. In this case, the integer value must be converted
to float value so that the final result is a float data type value.
1. Type Conversion
2. Type Casting
Type Conversion
The type conversion is the process of converting a data value from one data type to
another data type automatically by the compiler.
The compiler does this automatic conversion at compile time.
Sometimes type conversion is also called implicit type conversion.
The implicit type conversion is automatically performed by the compiler.
For example, in c programming language, when we assign an integer value to a
float variable the integer value automatically gets converted to float value by
adding decimal value 0.
When a float value is assigned to an integer variable the float value automatically
gets converted to an integer value by removing the decimal value.
For type conversion to take place, the following two conditions must be followed:-
o The destination data type and source data type must be compatible. For example,
numeric types (like ‘int’ and ‘float’) are compatible, while numeric to char /
numeric to boolean / char to boolean is not compatible.
o The destination data type must be larger than the source data type. For example,
int can be converted to float, but float cannot be converted to int.
To understand more about type conversion observe the following...
#include<stdio.h>
#include<conio.h>
void main(){
int i = 95 ; Output:
float x = 90.99 ;
char ch = 'A' ; i value is 90
i = x ;
printf("i value is %d\n",i); x value is 90.000000
x = i ;
printf("x value is %f\n",x);
i = ch ;
i value is 65
printf("i value is %d\n",i);
}
In the above program, we assign i = x, i.e., float variable value is assigned to the integer
variable. Here, the compiler automatically converts the float value (90.99) into integer
value (90) by removing the decimal part of the float value (90.99) and then it is assigned
to variable i. Similarly, when we assign x = i, the integer value (90) gets converted to
float value (90.000000) by adding zero as the decimal part.
Typecasting
Typecasting is also called an explicit type conversion.
Compiler converts data from one data type to another data type implicitly.
When compiler converts implicitly, there may be a data loss.
In such a case, we convert the data from one data type to another data type using
explicit type conversion.
To perform this we use the unary cast operator.
To convert data from one type to another type we specify the target data type in
parenthesis as a prefix to the data value that has to be converted.
The general syntax of typecasting is as follows.
(TargetDatatype) DataValue
Example
int totalMarks = 450, maxMarks = 600 ;
float average ;
In the above example code, both totalMarks and maxMarks are integer data values.
When we perform totalMarks / maxMarks the result is a float value, but the destination
(average) datatype is a float. So we use type casting to convert totalMarks and maxMarks
into float data type.
#include<stdio.h>
#include<conio.h> Output:
int main()
Enter the values of a, b
{ and c23
int a,b,c;
float avg; 34
printf("\n Enter the values of a, b and c"); 56
scanf("%d%d%d",&a,&b,&c);
avg = (a+b+c)/3; Average before type
printf("\n Average before type casting is %f", avg); casting is 37.000000
avg = (float)(a + b + c) / 3 ; Average after type casting
printf("\n Average after type casting is %f", avg); is 37.666668
return 0;
}
Type Conversion Vs Type Casting: