Fundamentals of Computing And C Programming
Unit - II - Basics of C Language
Seminar 3
By Karthik Srini
Presentation By, Karthik Srini
Synopsis
• Formatted Input
• Formatted Output
• Expressions
• Precedence of Operators
Formatted Input
C Language
Presentation By, Karthik Srini
Formatted Input
While accepting values from the user, the compiler must be instructed
with the memory location in which the input values must be stored.
This must be in a specified format.


For example,
If two integers, say n1 and n2 are to be got from the user, then,
scanf(“%d%d”,&n1,&n2);
Presentation By, Karthik Srini
Formatted Input - Points to remember
•Format specifications contained in the control string should match the
arguments in order
•Input data items must be separated by space and must match the
variable receiving the input in same order.
•The reading will be terminated, when scanf() encounters a mismatch
of data or a character that is not valid for the value being read.
Formatted Output
C Language
Presentation By, Karthik Srini
Formatted Output
In a similar manner, output also must be given in such a way that, it is
easy to understand.
For example,
If one integer and one float value are to be printed, then,
printf(“%d%f”,n,m);
Presentation By, Karthik Srini
Expressions
Presentation By, Karthik Srini
Expressions
An expression is defined as the collection of variables, operators and
constants.
For example,
x = 5 + num2 - 15 ;
Presentation By, Karthik Srini
Operator Precedence
Presentation By, Karthik Srini
Operator Precedence
The hierarchy that is followed while an expression is evaluated is
called operator precedence.
Presentation By, Karthik Srini
Rules for Evaluation of expression
➢First , parenthesised sub expression from left to right are
evaluated.
➢If parentheses are nested, the evaluation begins with the
innermost sub-expression.
➢The precedence rule is applied in determining the order
of application of operators in evaluating sub-expressions.
➢The associativity rule is applied when two or more
operators of the same precedence level appear in a sub-
expression.
➢Arithmetic expressions are evaluated from left to right
using rules of precedence.
➢When parentheses are used, the expression within
parentheses assume highest priority
Presentation By, Karthik Srini
Presentation By, Karthik Srini
Example
Presentation By, Karthik Srini
Example
If x=10 and y=5 ;
What is ( x * y ) - ( x / y ) ; ?
Answer :
( 50 ) - ( 2 ) = 48
Presentation By, Karthik Srini
Exercise
If a = 9 , b = 12 , c = 3 . Then evaluate the following :
( i ) x = a - b / 3 + c * 2 - 1 ;
( ii ) y = a - b / ( 3 + c ) * ( 2 - 1 ) ;
( iii ) z = - a + ( ( b / 3 + c ) * 2 ) - 1;
Presentation By, Karthik Srini
Answers
( i ) x = 10
( ii ) y = 7
( iii ) z = 4
Presentation By, Karthik Srini
Queries???
Presentation By, Karthik Srini
Thank You

Fundamentals of Computing and C Programming - Part 3

  • 1.
    Fundamentals of ComputingAnd C Programming Unit - II - Basics of C Language
  • 2.
  • 3.
    Presentation By, KarthikSrini Synopsis • Formatted Input • Formatted Output • Expressions • Precedence of Operators
  • 4.
  • 5.
    Presentation By, KarthikSrini Formatted Input While accepting values from the user, the compiler must be instructed with the memory location in which the input values must be stored. This must be in a specified format. 
 For example, If two integers, say n1 and n2 are to be got from the user, then, scanf(“%d%d”,&n1,&n2);
  • 6.
    Presentation By, KarthikSrini Formatted Input - Points to remember •Format specifications contained in the control string should match the arguments in order •Input data items must be separated by space and must match the variable receiving the input in same order. •The reading will be terminated, when scanf() encounters a mismatch of data or a character that is not valid for the value being read.
  • 7.
  • 8.
    Presentation By, KarthikSrini Formatted Output In a similar manner, output also must be given in such a way that, it is easy to understand. For example, If one integer and one float value are to be printed, then, printf(“%d%f”,n,m);
  • 9.
    Presentation By, KarthikSrini Expressions
  • 10.
    Presentation By, KarthikSrini Expressions An expression is defined as the collection of variables, operators and constants. For example, x = 5 + num2 - 15 ;
  • 11.
    Presentation By, KarthikSrini Operator Precedence
  • 12.
    Presentation By, KarthikSrini Operator Precedence The hierarchy that is followed while an expression is evaluated is called operator precedence.
  • 13.
    Presentation By, KarthikSrini Rules for Evaluation of expression ➢First , parenthesised sub expression from left to right are evaluated. ➢If parentheses are nested, the evaluation begins with the innermost sub-expression. ➢The precedence rule is applied in determining the order of application of operators in evaluating sub-expressions. ➢The associativity rule is applied when two or more operators of the same precedence level appear in a sub- expression. ➢Arithmetic expressions are evaluated from left to right using rules of precedence. ➢When parentheses are used, the expression within parentheses assume highest priority
  • 14.
  • 15.
  • 16.
    Presentation By, KarthikSrini Example If x=10 and y=5 ; What is ( x * y ) - ( x / y ) ; ? Answer : ( 50 ) - ( 2 ) = 48
  • 17.
    Presentation By, KarthikSrini Exercise If a = 9 , b = 12 , c = 3 . Then evaluate the following : ( i ) x = a - b / 3 + c * 2 - 1 ; ( ii ) y = a - b / ( 3 + c ) * ( 2 - 1 ) ; ( iii ) z = - a + ( ( b / 3 + c ) * 2 ) - 1;
  • 18.
    Presentation By, KarthikSrini Answers ( i ) x = 10 ( ii ) y = 7 ( iii ) z = 4
  • 19.
    Presentation By, KarthikSrini Queries???
  • 20.
    Presentation By, KarthikSrini Thank You