
Operators, Expressions, and Type Casting
Explore the world of operators and expressions in programming, from arithmetic to logical operations. Learn about unary, binary, and bitwise operators, as well as the essentials of expression evaluation and type casting. Dive into the foundations of coding with this insightful guide.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
Operators, Expression Evaluation & Type Casting
Agenda Operators Expression Evaluation Type Casting
Operators and Expression An operator is a symbol that tells the computer to perform certain mathematical and logical manipulations. Example: +, -, *,/ An expression is a sequence of operands and operators that reduces to a single value. Example: 10+15 is an expression whose value is 25.
Operators Unary Operators (++, --) Binary Operators Arithmetic operators Relational Operators Logical Operators Assignment Operators Bitwise Operators Ternary Operators (? :)
Binary Operators Arithmatic Operator These operators are used to perform arithmetic/mathematical operations on operands. Examples: (+, -, *, /, %).
Binary Operators Relational Operator
Binary Operators Logical Operator
Binary Operators Assignement Operator Assignment operator is used to assign a value in a variable. The usual assignment operator is =. For example- int a = 10; \\Here 10 is assigned to variable a.
Binary Operators Bitwise Operator The Bitwise operators are used to perform bit-level operations on the operands. The operators are first converted to bit-level and then the calculation is performed on the operands.
Bitwise Operators vs. Logical Operators Logical Operators && || Bitwise Operators & | A = 10 , B = 20 A = 15 , B = 15 # A > 10 && B <= 20 # A & B = F && T = 15 = F = 0
Unary Operators (Increment and Decrement Operator) The increment ( ++ ) and decrement (--) operators in C are unary operators for incrementing and values by 1 respectively. decrementing the numeric
Unary Operators (Increment and Decrement Operator) // C Program to illustrate the increment of both type #include <stdio.h> int main() { int a = 5; int b = 5; // PREFIX int prefix = ++a; printf("Prefix Increment: %d\n", prefix); // POSTFIX int postfix = b++; printf("Postfix Increment: %d", postfix); }
Unary Operators (Increment and Decrement Operator) // C program to illustrate the decrement operator of both // types #include <stdio.h> int main() { int a = 5; int b = 5; // PREFIX int prefix = --a; printf("Prefix = %d\n", prefix); // POSTFIX int postfix = b--; printf("Postfix = %d", postfix); }
Ternary Operators (Conditional Operator)
Expression Evaluation Let, a = 5
Type Casting Converting one data type into another is known as type casting or, type-conversion. For example, if you want to store a 'long' value into a simple integer then you can type cast 'long' to 'int'. You can convert the values from one type to another explicitly using the cast operator as follows (type_name)expression Consider the following example where the cast operator causes the division of one integer variable by another to be performed as a floating-point operation #include <stdio.h> Int main() { int sum = 17, count = 5; double mean; mean = (double) sum / count; printf("Value of mean : %f\n", mean ); return 0; } When the above code is compiled and executed, it produces the following result Value of mean : 3.400000 It should be noted here that the cast operator has precedence over division, so the value of sum is first converted to type double and finally it gets divided by count yielding a double value.
Implicit Type Casting When the type conversion is performed automatically by the compiler without programmers intervention, such type of conversion is known as implicit type conversion or type promotion. int x; for(x=97; x<=122; x++) { printf("%c", x); /*Implicit casting from int to char thanks to %c*/ }
Explicit Type Casting The type conversion performed by the programmer by posing the data type of the expression of specific type is known as explicit type conversion. The explicit type conversion is also known as type casting. Type casting in c is done in the following form: (data_type)expression; where, data_type is any valid c data type, and expression may be constant, variable or expression. For example, int x; for(x=97; x<=122; x++) { printf("%c", (char)x); /*Explicit casting from int to char*/ }
Type Casting Implicit Example: char val = a ; int num = val; Explicit Example: char val = a ; int num =(int) val;
Example Consider the following code: If we want to get the exact value of 7/5 then we need explicit casting from int to float: