0% found this document useful (0 votes)
20 views24 pages

Lecture 4

C programming fundamentals

Uploaded by

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

Lecture 4

C programming fundamentals

Uploaded by

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

CS1001E

Computer Programming
Lecture #4
Arithmetic Operators, Expressions, Comments

Monsoon 2024
Amit Praseed, Saleena N, Santosh Kumar Behera
CSED NITC

1
Arithmetic Operators
• Binary arithmetic operators

+ addition
- subtraction
* multiplication
/ division
% modulus

Modulus operator (%)


• returns the remainder after integer division
• 5%2 returns 1 (remainder after dividing 5 by 2)
• only defined for integer operands

2
Example Program
#include <stdio.h>

int main(){
int a=10, b=20, sum;
sum = a + b;
printf("a=%d b=%d sum=%d\n", a, b, sum);
}

output: a=10 b=20 sum=30

Note: a+b is an expression


3
Expression: examples
10+2*3
a+1
x-y
total/n
a*x+b
(sum+val)/num
a*x*x+b*x+c
num % 10

Note: assuming all variables are declared as int type


4
Assignment statement

sum = a + b ;

• compute the value of the expression a+b


• assign the computed value to variable sum

5
Assignment Statement
<variable> = <expression>;

Meaning: evaluate the expression on the RHS, assign its value to the
variable on the LHS

sum=a+b;

compute the sum of the values of variables a and b, store the


resulting value in sum

6
Assignment Statement: examples

a=b;

z=x+y*10;

a=a+1;

7
Exercise
...
int a=10, b=20;
a=a+1;
b=a+b;
...

What are the values of a and b after the last assignment statement?

8
Program to read two numbers, calculate and print their sum
#include <stdio.h>
int main( )
{
int a, b, sum;
printf(“Enter the first number\n”);
scanf(“%d”,&a);
printf(“Enter the second number\n”);
scanf(“%d”,&b);
sum = a + b;
printf("a=%d b=%d sum=%d\n", a, b, sum);
}

9
Write a program to read two
integer values and print their
mean

10
A Wrong Program
#include <stdio.h>

int main( )
{
int a, b, avg; • Input: a=2, b=4
printf(“Enter the two numbers\n”); • Output: Average=4
scanf(“%d%d”,&a,&b);
avg=a+b/2;
printf(“Average = %d\n”, avg);
return 0;
}

11
Operator Precedence
• When there are multiple operators in a single expression, operators
are executed according to a precedence order
• Similar to BODMAS rule in maths

Operator(s)
Decreasing
precedence
* / %
+ -

12
Operator Precedence

a+b*c
b*c is evaluated first
operator * has higher precedence than +

13
Associativity of Operators
• Consider this expression:
a*b/c Operator(s) Associativity

• Both operators have same * / % Left to right


precedence, so which operation
+ - Left to right
is performed first?
• When multiple operators with
same precedence occur in an
expression, they are executed in
the order of their associativity

14
Associativity of Operators
Operator(s) Associativity
• Consider this expression: * / % Left to right
a*b/c
+ - Left to right
• Both operators have same
precedence, so which operation
is performed first? a*b/c
• When multiple operators with Associativity is left to right
same precedence occur in an a*b gets executed first and
expression, they are executed in then the result is divided by c
the order of their associativity

15
Parentheses for grouping subexpressions

• Highest level of precedence for parenthesized expressions


in a*(b+c)
b+c is evaluated first

• Nested parentheses - operators in the innermost pair of


parentheses are applied first.
in a*((x+y)/z)
x+y is evaluated first

16
What went wrong here?
#include <stdio.h>
/ has higher precedence than +
b/2 gets executed first, and
int main( ) the result is added to a
{
int a, b, avg;
• How to solve?
printf(“Enter the two numbers\n”); • either separate out the
scanf(“%d%d”,&a,&b); operations
avg=a+b/2; sum=a+b;
printf(“Average = %d\n”, avg); avg=sum/2;
return 0; • or use proper parentheses to
} group expressions
avg=(a+b)/2;

17
A “Correct” Program
#include <stdio.h>

int main( )
{
int a, b, avg; • Input: a=2, b=4
printf(“Enter the two numbers\n”);
• Output: Average=3
scanf(“%d%d”,&a,&b);
avg=(a+b)/2;
printf(“Average = %d\n”, avg);
return 0;
}

18
Exercise: indicate the order of evaluation of
operations in each of the following expressions

m*x+b
a*b%c
a*(b+c)
a*x*x+b*x+c
a*b+(c*(d+e))

19
Exercise: Predict the output
#include <stdio.h>

int main( )
{
int a, b, avg; • Input: a=2, b=3
printf(“Enter the two numbers\n”);
• Output: Average=?
scanf(“%d%d”,&a,&b);
avg=(a+b)/2;
printf(“Average = %d\n”, avg);
return 0;
}

20
Comments

• Comments are used in programs to provide documentation and


enhance understandability

• Comments are ignored by the compiler


• Do not affect program behavior

21
Comments
• Two types (in C)
• Single Line Comment:
• Starts with //
• Everything coming after the // in that line is treated as a comment
• Multi Line Comment
• Starts with /* and ends with */
• Anything coming between /* and */ is treated as a comment
• Can span multiple lines

22
A program with comments
/* This is a program to add two integer numbers.
Input: 2 integer numbers
Output: Sum of the two integers */

#include <stdio.h>
int main( )
{
int a, b, sum; //variable declarations
printf(“Enter two numbers\n”);
scanf(“%d%d”,&a,&b); //read the two numbers
sum=a+b;
printf(“Sum = %d\n”, sum); //print the output
}
23
Major References
1. B. W. Kernighan and D. M. Ritchie, The C Programming Language,
2nd ed., Pearson Education India, 2015.
2. P. J. Deitel and H. M. Deitel and, C: How to program, 9th ed.
Pearson Education, 2023.

24

You might also like