0% found this document useful (0 votes)
4 views21 pages

Operators and Expressions in C

This document provides an overview of C programming concepts, focusing on operands, expressions, and various types of operators including arithmetic, relational, logical, and bitwise operators. It also covers operator precedence, types of statements, data input/output functions, and includes a simple C program example for adding two numbers. The document serves as a foundational guide for understanding problem-solving in C programming.

Uploaded by

Sahil Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views21 pages

Operators and Expressions in C

This document provides an overview of C programming concepts, focusing on operands, expressions, and various types of operators including arithmetic, relational, logical, and bitwise operators. It also covers operator precedence, types of statements, data input/output functions, and includes a simple C program example for adding two numbers. The document serves as a foundational guide for understanding problem-solving in C programming.

Uploaded by

Sahil Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

C

(Programming for Problem Solving BTPS-


101-18

Prepared By
Dr. Dinesh Gupta
Assistant Professor

Department of Computer Science & Engineering


IKG Punjab Technical University, Kapurthala
Operands and Expression
Consider the following statement:
a = b + 5;
The statement above consists of 3 operands and 2 operators.
Operands: a, b, 5
Operators: =, +
Each operand within the statement above is an expression, which can then
be clubbed together operation by operation to form single expression.
Exp1 = b
Exp2 = 5
Exp3 = a
Exp4 = exp1 + exp2
Exp5 = exp3 = exp4
Operators
• In C, an operator is a symbol that is part of the character set of C language
programming construct and is used to perform operations like making
calculations, assignments, comparisons etc.
• Unary operators: These operators operate on single operand and the
example is unary minus.
For example, -a, &a, !a etc.
• Binary operators: These operators operate on two operands and the
example is addition or plus operator.
For example, a + b, a * b, a&&b etc.
• Ternary operator: These operators operate on three operands and the only
example of this type of operator in C language is conditional operator that is
explained later in this chapter.
For example, a < b ? x++ : y++; (Note that it uses three operands)
Types of Operators
• Arithmetic operators
• Assignment operator
• Relational operators
• Logical operators
• Increment/decrement operators
• Conditional operator
• Bitwise operators
• Left over operators
Arithmetic Operators
Operator Name Expression Result
* Multiplication 2*3 6
/ Division 4/2 2
- Subtraction 4-2 2
+ Addition 4+2 6
% Modulus 5%2 1
(to find remainder)
Assignment & Shorthand Assignment
Operator
The assignment operator in C is used to assign a value of the expression on
right side of the operator to the expression on the left side of the operator. For
example, int a = 5;

SHORTHAND OPERATOR
Short hand operator is used when the arithmetic expression of the right side of
assignment operator starts with the expression on the left side.
For example, a = a + b;
Expression on right side starts with ‘a’, which the expression on left side.We can
use Shorthand assignment operator that is equivalent to the simple assignment
operator.
a += b; a = a + b; //both the statements are same.
Increment and Decrement Operator
These two operators are used to increase or decrease the value of the
variable by one. Increment and decrement operators are unary
operators. They can be applied on a variable in two different ways
namely prefix and postfix.
Syntax:
For prefix: For postfix:
++Variable_name; Variable_name++;
Example of Increment & Decrement
Operator
For postfix increment example For prefix increment example consider:
consider: int a= 10;
int a= 10; ++a;
a++;

int a=10, b; int a=10, b;


b= a++; //value of ‘b’ will be 10 and ‘a’ 11. b= ++a; //value of ‘b’ will be 11 and ‘a’ 11.
Relational Operators
These operators are used to find the relation between two operands or
expression. The outcome is either 0 or 1.
Operator Operator Name Expression Result
Symbol

< Less than 5<6 1


> Greater than 5>6 0
<= Less than or equal to 6<=7 1
>= Greater than or equal 7>=7 1
to

== Equal to 6==7 0
!= Not equal too 7!=8 1
Logical Operator
This category of operators is used to produce a decision based on
combination or multiple expressions or relational conditions.
Operator symbol Operator Name Meaning
&& AND Final result is true if an only if all the conditions
separated by AND operator are true
|| OR Final result is true if any one of the conditions
separated by OR operator is true
! NOT NOT operator is used to produce the negation of the
result of the condition.
Conditional Operator
The conditional operator (? :) is a ternary operator as it takes three
operands. The conditional expression is sort of a decision control
statement called if/else statement in which the execution of a
statement depends upon the outcome of a condition.

Syntax:
e1 ? e2 : e3;
For example,
c = b>a ? b : a ;
Bitwise Operators
The bitwise operators work on individual bits of an integer value.
Operator Operator Name Meaning
symbol
& Bitwise AND Applying AND logic to all bit
positions between two numbers.
| Bitwise OR Applying OR logic to all bit
positions between two numbers.
^ Bitwise exclusive OR (XOR) Applying XOR logic to all bit
positions between two numbers.
~ Bitwise compliment Applying logical compliment to a
number.
<< Bitwise left shift Shifting one bit position left
>> Bitwise right shift Shifting one bit position right
Left Over Operators
• Sizeof() Used to find the amount of memory needed
For Example,
int a;
sizeof(a);

• Reference Operator (&) Used to return the address of a variable or create reference variable.
For example,
Printf(“%x”,&a);
&a = b;

• Dereference Operator (*) Used to create a pointer variable or access value at the address pointed to by
pointer
For Example
int a=10, *ptr ;
Ptr = &a ;
printf(“%d”,*ptr);
Operator Precedence and
Associativity
Operator Type Operator Associativity
Primary Operators ( ) [ ] . -> expr++ expr-- left-to-right
Unary Operators right-to-left
Binary Operators * / % left-to-right
+ -
>> <<
< > <= >=
== !=
&
^
|
&&
||
Ternary Operator ?: right-to-left
Assignment Operator = += -= *= /= %= >>= <<= &= right-to-left
^= |=
Comma , left-to-right
Types of Statements In C
• Type Declaration statement
int a;
int a, b;
int a = 10;

• Arithmetic statement
num1 = num2 + num3;
a = b *c / d;

Control statement
Control statements are used to control the order of execution of statements in a C program. In
other words the control statements determine the ‘flow of control’ in a program. There are 3
categories of control statements in C.
Decision statements
Loop statements
Comments
Comments are non executable statement in C. you can write a single
line and multiline comments.

// Single line comment

/* Line1
…………..
Multiline comment */
Data Input
C Programming allows user to enter the values of variable through the
keyboard at run time. scanf( ) is the most basic function in C to accept
input from user. You can use other functions as well to accept string
input.
To accept integer input:
scanf(“%d”,&num1);
To accept multiple integer input
scanf(“%d %d”,&num1, &num2);
Various Format Specifiers
%c single character
%d or %i integer type
%u unsigned integer
%ld or %lu long signed or unsigned integer
%o octal integer unsigned
%x,%X hex unsigned integer
%e, %E, %f, %g, %G floating type
%lf double
%s strings that is sequence of characters
Data Output
printf() function is used to display the output on the screen to the user.
Format of printf( ) function:
printf ( "<format specifiers> or <constant literals> ", <list of variables> ) ;
Following are some of the constant literals:
• \n (newline)
• \t (tab 8 spaces
• \v (vertical tab)
• \f (new page)
• \b (backspace)
• \r (carriage return)
Some of the examples of printf( ):
printf ( "%d", fact ) ;
printf ( "%d %f",p,intr ) ;
printf ( "factorial = %d", fact ) ;
printf ( "Principle = %d \nRate = %f", p, r ) ;
Simple C Program
// program to add two numbers
#include<conio.h>
#include<stdio.h>
main ( )
{
int num1, num2, sum=0;
clrscr( );
printf(“enter number 1”);
scanf(“%d”,&num1);
printf(“enter number 2”);
scanf(“%d”,&num2);
sum = num1 + num2;
printf ( "sum of two numbers is :- %d" , sum ) ;
getch ( )
Thank You

You might also like