SlideShare a Scribd company logo
Chapter 4: Basic C Operators
  In this chapter, you will learn about:
     Arithmetic operators
        Unary operators
        Binary operators
     Assignment operators
     Equalities and relational operators
     Logical operators
     Conditional operator




Principles of Programming - NI July2005    1
Arithmetic Operators I
  In C, we have the following operators (note
  that all these example are using 9 as the
  value of its first operand)




Principles of Programming - NI July2005   2
Arithmetic Operators II
  There are 2 types of arithmetic operators
  in C:
     unary operators
        operators that require only one operand.
     binary operators.
        operators that require two operands.




Principles of Programming - NI July2005   3
Unary Operator
C Operation          Operator     Example
Positive                +          a = +3
Negative                -          b = -a
Increment              ++           i++
Decrement               --          i--

  The first assigns positive 3 to a
  The second assigns the negative value of a to b.
  i++ is equivalent to i = i + 1
  i-- is equivalent to i = i-1


Principles of Programming - NI July2005   4
PRE- / POST-Increment
  It is also possible to use ++i and --i instead of
  i++ and i--
  However, the two forms have a slightly yet
  important difference.
  Consider this example:
      int a = 9;
      printf(“%dn”, a++);
      printf(“%d”, a);
  The output would be:
   9
   10
Principles of Programming - NI July2005   5
PRE- / POST-Increment cont…
  But if we have:
      int a = 9;
      printf(“%dn”, ++a);
      printf(“%d”, a);
  The output would be:
   10
   10
  a++ would return the current value of a and
  then increment the value of a
  ++a on the other hand increment the value of
  a before returning the value

Principles of Programming - NI July2005   6
The following table illustrates the difference between the prefix and postfix
 modes of the increment and decrement operator.


                          int R = 10, count=10;

   ++ Or --                   Equivalent                      R value            Count
  Statement                   Statements                                         value
R = count++;             R = count;
                                                                  10              11
                         count = count + 1
R = ++count;             count = count + 1;
                                                                  11              11
                         R = count;
R = count --;            R = count;
                                                                  10              9
                         count = count – 1;
R = --count;             Count = count – 1;
                                                                   9              9
                         R = count;


 Principles of Programming - NI July2005                             7
Binary Operators
C Operation          Operator             Example:
Addition                 +                  a+3
Subtraction              -                  a-6
Multiplication           *                  a*b
Division                 /                  a/c
Modulus                 %                   a%x

  The division of variables of type int will always
  produce a variable of type int as the result.
  You could only use modulus (%) operation
  on int variables.

Principles of Programming - NI July2005    8
Assignment Operators
  Assignment operators are used to combine the
  '=' operator with one of the binary arithmetic
  operators
  In the following slide, All operations starting from
      c=9
      Operator    Example     Equivalent   Results
                              Statement
         +=       c += 7     c=c+7         c = 16
         -=        c -= 8    c=c–8          c=1
         *=       c *= 10    c = c * 10    c = 90
         /=        c /= 5     c=c/5         c=1
         %=       c %= 5     c=c%5          c=4
Principles of Programming - NI July2005    9
Precedence Rules
  Precedence rules come into play when there is a mixed
  of arithmetic operators in one statement. For example:
  x = 3 * a - ++b%3;
  The rules specify which of the operators will be
  evaluated first.

  Precedence         Operator             Associativity
  Level
  1 (highest)        ()                     left to right
  2                  unary                  right to left
  3                  * / %                  left to right
  4                  + -                    left to right
  5 (lowest)         = += -= *= /= %=       right to left


Principles of Programming - NI July2005     10
Precedence Rules cont…
  For example: x = 3 * a - ++b % 3;
   how would this statement be evaluated?

  If we intend to have the statement evaluated
  differently from the way specified by the
  precedence rules, we need to specify it using
  parentheses ( )
  Using parenthesis, we will have
          x = 3 * ((a - ++b)%3);
  The expression inside a parentheses will be
  evaluated first.
  The inner parentheses will be evaluated
  earlier compared to the outer parentheses.

Principles of Programming - NI July2005     11
Equality and Relational Operators
 Equality Operators:
   Operator        Example       Meaning
    ==              x == y       x is equal to y
    !=              x != y       x is not equal to y

 Relational Operators:
   Operator        Example       Meaning
    >              x>y           x is greater than y
    <              x<y           x is less than y
    >=             x >= y        x is greater than or equal to y
    <=             x <= y        x is less than or equal to y




Principles of Programming - NI July2005      12
Logical Operators
  Logical operators are useful when we want to
  test multiple conditions.
  There are 3 types of logical operators and they
  work the same way as the boolean AND, OR and
  NOT operators.
  && - Logical AND
    All the conditions must be true for the whole
    expression to be true.
    Example: if (a == 10 && b == 9 && d == 1)
    means the if statement is only true when a ==
    10 and b == 9 and d == 1.

Principles of Programming - NI July2005   13
Logical Operators cont…
  || - Logical OR
     The truth of one condition is enough to make
     the whole expression true.
     Example: if (a == 10 || b == 9 || d == 1)
     means the if statement is true when either
     one of a, b or d has the right value.
  ! - Logical NOT (also called logical negation)
     Reverse the meaning of a condition
     Example: if (!(points > 90))
     means if points not bigger than 90.

Principles of Programming - NI July2005   14
Conditional Operator
  The conditional operator (?:) is used to
  simplify an if/else statement.
  Syntax:
  Condition ? Expression1 : Expression2
  The statement above is equivalent to:
       if (Condition)
            Expression1
       else
            Expression2


Principles of Programming - NI July2005   15
Conditional Operator cont…
  Example 1:
  if/else statement:
   if (total > 60)
        grade = ‘P’
  else
        grade = ‘F’;
  conditional statement:
  total > 60 ? grade = ‘P’: grade = ‘F’;
                     OR
   grade = total > 60 ? ‘P’: ‘F’;
Principles of Programming - NI July2005   16
Conditional Operator cont…
Example 2:
if/else statement:
if (total > 60)
     printf(“Passed!!n”);
else
     printf(“Failed!!n”);

Conditional Statement:
printf(“%s!!n”, total > 60? “Passed”: “Failed”);

Principles of Programming - NI July2005   17
SUMMARY
  This chapter exposed you the operators used
  in C
     Arithmetic operators
     Assignment operators
     Equalities and relational operators
     Logical operators
     Conditional operator
  Precedence levels come into play when there
  is a mixed of arithmetic operators in one
  statement.
  Pre/post fix - effects the result of statement


Principles of Programming - NI July2005    18

More Related Content

What's hot (20)

PPTX
Operator in c programming
Manoj Tyagi
 
PPT
Binary operator overloading
BalajiGovindan5
 
PPT
FUNCTIONS IN c++ PPT
03062679929
 
PPTX
Principle source of optimazation
Siva Sathya
 
PPT
Operators in c language
Amit Singh
 
PPTX
Polymorphism
Nochiketa Chakraborty
 
PPTX
Control and conditional statements
rajshreemuthiah
 
PPT
Formatted input and output
Online
 
PPTX
Array and string
prashant chelani
 
PPTX
BIOPOTENTIAL MEASUREMENT-ECG, EEG, EMG, PCG
DJERALDINAUXILLIAECE
 
PPTX
User defined functions
Rokonuzzaman Rony
 
PPTX
Computer architecture control unit
Mazin Alwaaly
 
PDF
Полунатурная модель управляемой ракеты с пассивной ГСН
MATLAB
 
PPTX
Functions in c
sunila tharagaturi
 
PPTX
INSTRUCTION FORMAT.pptx
RaunakKumar33449
 
PPTX
Operators
Krishna Kumar Pankaj
 
PPT
Union In language C
Ravi Singh
 
PPTX
C++ decision making
Zohaib Ahmed
 
PPT
Savitch ch 01
Terry Yoast
 
PPT
Basics of C programming
avikdhupar
 
Operator in c programming
Manoj Tyagi
 
Binary operator overloading
BalajiGovindan5
 
FUNCTIONS IN c++ PPT
03062679929
 
Principle source of optimazation
Siva Sathya
 
Operators in c language
Amit Singh
 
Polymorphism
Nochiketa Chakraborty
 
Control and conditional statements
rajshreemuthiah
 
Formatted input and output
Online
 
Array and string
prashant chelani
 
BIOPOTENTIAL MEASUREMENT-ECG, EEG, EMG, PCG
DJERALDINAUXILLIAECE
 
User defined functions
Rokonuzzaman Rony
 
Computer architecture control unit
Mazin Alwaaly
 
Полунатурная модель управляемой ракеты с пассивной ГСН
MATLAB
 
Functions in c
sunila tharagaturi
 
INSTRUCTION FORMAT.pptx
RaunakKumar33449
 
Union In language C
Ravi Singh
 
C++ decision making
Zohaib Ahmed
 
Savitch ch 01
Terry Yoast
 
Basics of C programming
avikdhupar
 

Viewers also liked (7)

PPT
Slides 2-basic sql
Anuja Lad
 
PPT
Final exam review answer(networking)
welcometofacebook
 
PPT
Basic networking hardware pre final 1
Anuja Lad
 
PDF
Top 100 SQL Interview Questions and Answers
iimjobs and hirist
 
DOC
Best sql plsql material
pitchaiah yechuri
 
DOC
Sql queries with answers
vijaybusu
 
PPT
Sql ppt
Anuja Lad
 
Slides 2-basic sql
Anuja Lad
 
Final exam review answer(networking)
welcometofacebook
 
Basic networking hardware pre final 1
Anuja Lad
 
Top 100 SQL Interview Questions and Answers
iimjobs and hirist
 
Best sql plsql material
pitchaiah yechuri
 
Sql queries with answers
vijaybusu
 
Sql ppt
Anuja Lad
 
Ad

Similar to Basic c operators (20)

PPT
Operation and expression in c++
Online
 
PDF
Arithmetic instructions
Learn By Watch
 
PPTX
c programme
MitikaAnjel
 
PPT
Operator & Expression in c++
bajiajugal
 
PPS
C programming session 02
Dushmanta Nath
 
PPS
02 iec t1_s1_oo_ps_session_02
Niit Care
 
PDF
Unit ii chapter 1 operator and expressions in c
Sowmya Jyothi
 
PPT
FP 201 Unit 2 - Part 3
rohassanie
 
PPTX
Operators and expressions
vishaljot_kaur
 
PPT
Mesics lecture 4 c operators and experssions
eShikshak
 
DOC
Unit 4
rohassanie
 
PPS
C programming session 02
AjayBahoriya
 
PPTX
Working with IDE
Nurul Zakiah Zamri Tan
 
PPTX
Operators-computer programming and utilzation
Kaushal Patel
 
PPTX
Operators and Expression
shubham_jangid
 
PPT
Java 2
Preethi Nambiar
 
PPT
Java - Operators
Preethi Nambiar
 
PDF
Lecture03(c expressions & operators)
Dhaka University of Engineering & Technology(DUET)
 
DOC
Report on c
jasmeen kr
 
PDF
C sharp chap3
Mukesh Tekwani
 
Operation and expression in c++
Online
 
Arithmetic instructions
Learn By Watch
 
c programme
MitikaAnjel
 
Operator & Expression in c++
bajiajugal
 
C programming session 02
Dushmanta Nath
 
02 iec t1_s1_oo_ps_session_02
Niit Care
 
Unit ii chapter 1 operator and expressions in c
Sowmya Jyothi
 
FP 201 Unit 2 - Part 3
rohassanie
 
Operators and expressions
vishaljot_kaur
 
Mesics lecture 4 c operators and experssions
eShikshak
 
Unit 4
rohassanie
 
C programming session 02
AjayBahoriya
 
Working with IDE
Nurul Zakiah Zamri Tan
 
Operators-computer programming and utilzation
Kaushal Patel
 
Operators and Expression
shubham_jangid
 
Java - Operators
Preethi Nambiar
 
Lecture03(c expressions & operators)
Dhaka University of Engineering & Technology(DUET)
 
Report on c
jasmeen kr
 
C sharp chap3
Mukesh Tekwani
 
Ad

More from Anuja Lad (20)

DOCX
Important topic in board exams
Anuja Lad
 
PDF
Data communication
Anuja Lad
 
PPT
Data communication intro
Anuja Lad
 
DOCX
Questions from chapter 1 data communication and networking
Anuja Lad
 
DOC
T y b com question paper of mumbai university
Anuja Lad
 
DOCX
Questions from chapter 1 data communication and networking
Anuja Lad
 
DOC
T y b com question paper of mumbai university
Anuja Lad
 
PPT
Basic networking hardware pre final 1
Anuja Lad
 
PDF
Data communication
Anuja Lad
 
PPT
Data communication intro
Anuja Lad
 
PPT
Intro net 91407
Anuja Lad
 
PPT
Itmg360 chapter one_v05
Anuja Lad
 
PPT
Mysqlppt3510
Anuja Lad
 
PPT
Lab 4 excel basics
Anuja Lad
 
PPT
Mysql2
Anuja Lad
 
PDF
Introductionto excel2007
Anuja Lad
 
PPT
C tutorial
Anuja Lad
 
PPT
C
Anuja Lad
 
PPT
5 intro to networking
Anuja Lad
 
PPT
1 introduction-to-computer-networking
Anuja Lad
 
Important topic in board exams
Anuja Lad
 
Data communication
Anuja Lad
 
Data communication intro
Anuja Lad
 
Questions from chapter 1 data communication and networking
Anuja Lad
 
T y b com question paper of mumbai university
Anuja Lad
 
Questions from chapter 1 data communication and networking
Anuja Lad
 
T y b com question paper of mumbai university
Anuja Lad
 
Basic networking hardware pre final 1
Anuja Lad
 
Data communication
Anuja Lad
 
Data communication intro
Anuja Lad
 
Intro net 91407
Anuja Lad
 
Itmg360 chapter one_v05
Anuja Lad
 
Mysqlppt3510
Anuja Lad
 
Lab 4 excel basics
Anuja Lad
 
Mysql2
Anuja Lad
 
Introductionto excel2007
Anuja Lad
 
C tutorial
Anuja Lad
 
5 intro to networking
Anuja Lad
 
1 introduction-to-computer-networking
Anuja Lad
 

Recently uploaded (20)

PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PPTX
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
PDF
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
PDF
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
PDF
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
PDF
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PDF
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PDF
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
PDF
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
PPTX
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
PDF
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 

Basic c operators

  • 1. Chapter 4: Basic C Operators In this chapter, you will learn about: Arithmetic operators Unary operators Binary operators Assignment operators Equalities and relational operators Logical operators Conditional operator Principles of Programming - NI July2005 1
  • 2. Arithmetic Operators I In C, we have the following operators (note that all these example are using 9 as the value of its first operand) Principles of Programming - NI July2005 2
  • 3. Arithmetic Operators II There are 2 types of arithmetic operators in C: unary operators operators that require only one operand. binary operators. operators that require two operands. Principles of Programming - NI July2005 3
  • 4. Unary Operator C Operation Operator Example Positive + a = +3 Negative - b = -a Increment ++ i++ Decrement -- i-- The first assigns positive 3 to a The second assigns the negative value of a to b. i++ is equivalent to i = i + 1 i-- is equivalent to i = i-1 Principles of Programming - NI July2005 4
  • 5. PRE- / POST-Increment It is also possible to use ++i and --i instead of i++ and i-- However, the two forms have a slightly yet important difference. Consider this example: int a = 9; printf(“%dn”, a++); printf(“%d”, a); The output would be: 9 10 Principles of Programming - NI July2005 5
  • 6. PRE- / POST-Increment cont… But if we have: int a = 9; printf(“%dn”, ++a); printf(“%d”, a); The output would be: 10 10 a++ would return the current value of a and then increment the value of a ++a on the other hand increment the value of a before returning the value Principles of Programming - NI July2005 6
  • 7. The following table illustrates the difference between the prefix and postfix modes of the increment and decrement operator. int R = 10, count=10; ++ Or -- Equivalent R value Count Statement Statements value R = count++; R = count; 10 11 count = count + 1 R = ++count; count = count + 1; 11 11 R = count; R = count --; R = count; 10 9 count = count – 1; R = --count; Count = count – 1; 9 9 R = count; Principles of Programming - NI July2005 7
  • 8. Binary Operators C Operation Operator Example: Addition + a+3 Subtraction - a-6 Multiplication * a*b Division / a/c Modulus % a%x The division of variables of type int will always produce a variable of type int as the result. You could only use modulus (%) operation on int variables. Principles of Programming - NI July2005 8
  • 9. Assignment Operators Assignment operators are used to combine the '=' operator with one of the binary arithmetic operators In the following slide, All operations starting from c=9 Operator Example Equivalent Results Statement += c += 7 c=c+7 c = 16 -= c -= 8 c=c–8 c=1 *= c *= 10 c = c * 10 c = 90 /= c /= 5 c=c/5 c=1 %= c %= 5 c=c%5 c=4 Principles of Programming - NI July2005 9
  • 10. Precedence Rules Precedence rules come into play when there is a mixed of arithmetic operators in one statement. For example: x = 3 * a - ++b%3; The rules specify which of the operators will be evaluated first. Precedence Operator Associativity Level 1 (highest) () left to right 2 unary right to left 3 * / % left to right 4 + - left to right 5 (lowest) = += -= *= /= %= right to left Principles of Programming - NI July2005 10
  • 11. Precedence Rules cont… For example: x = 3 * a - ++b % 3; how would this statement be evaluated? If we intend to have the statement evaluated differently from the way specified by the precedence rules, we need to specify it using parentheses ( ) Using parenthesis, we will have x = 3 * ((a - ++b)%3); The expression inside a parentheses will be evaluated first. The inner parentheses will be evaluated earlier compared to the outer parentheses. Principles of Programming - NI July2005 11
  • 12. Equality and Relational Operators Equality Operators: Operator Example Meaning == x == y x is equal to y != x != y x is not equal to y Relational Operators: Operator Example Meaning > x>y x is greater than y < x<y x is less than y >= x >= y x is greater than or equal to y <= x <= y x is less than or equal to y Principles of Programming - NI July2005 12
  • 13. Logical Operators Logical operators are useful when we want to test multiple conditions. There are 3 types of logical operators and they work the same way as the boolean AND, OR and NOT operators. && - Logical AND All the conditions must be true for the whole expression to be true. Example: if (a == 10 && b == 9 && d == 1) means the if statement is only true when a == 10 and b == 9 and d == 1. Principles of Programming - NI July2005 13
  • 14. Logical Operators cont… || - Logical OR The truth of one condition is enough to make the whole expression true. Example: if (a == 10 || b == 9 || d == 1) means the if statement is true when either one of a, b or d has the right value. ! - Logical NOT (also called logical negation) Reverse the meaning of a condition Example: if (!(points > 90)) means if points not bigger than 90. Principles of Programming - NI July2005 14
  • 15. Conditional Operator The conditional operator (?:) is used to simplify an if/else statement. Syntax: Condition ? Expression1 : Expression2 The statement above is equivalent to: if (Condition) Expression1 else Expression2 Principles of Programming - NI July2005 15
  • 16. Conditional Operator cont… Example 1: if/else statement: if (total > 60) grade = ‘P’ else grade = ‘F’; conditional statement: total > 60 ? grade = ‘P’: grade = ‘F’; OR grade = total > 60 ? ‘P’: ‘F’; Principles of Programming - NI July2005 16
  • 17. Conditional Operator cont… Example 2: if/else statement: if (total > 60) printf(“Passed!!n”); else printf(“Failed!!n”); Conditional Statement: printf(“%s!!n”, total > 60? “Passed”: “Failed”); Principles of Programming - NI July2005 17
  • 18. SUMMARY This chapter exposed you the operators used in C Arithmetic operators Assignment operators Equalities and relational operators Logical operators Conditional operator Precedence levels come into play when there is a mixed of arithmetic operators in one statement. Pre/post fix - effects the result of statement Principles of Programming - NI July2005 18