1 CS 3090: Safety Critical Programming in C
1 CS 3090: Safety Critical Programming in C
Output: Number is 4
Example 2
#include <stdio.h>
// program reads and prints the same thing
int main() {
int number ;
printf (“ Enter a Number: ”);
scanf (“%d”, &number);
printf (“Number is %d\n”, number);
return 0;
}
int main() {
/* this program adds
all marks and prints the average */
Get int mark1; //first subject marks
Get int mark2; //second subject marks
Get int mark3; //third subject marks
calculate total ; //result
calculate average; //average
}
C increment operator
C has two special unary operators called
increment (++)
decrement (--)
++x is same as x = x + 1 or x += 1
--x is same as x = x - 1 or x -= 1
Increment/Decrement operators are of two types:
The postfix increment/decrement operator causes the current value of the variable
to be used in the expression, then the value is incremented or decremented.
y = x++;
The increment and decrement operators have higher precedence than the
operators we have discussed so far (with the only exception being the
parentheses).
=, +=, -=, *=, /=, %= Assignment Operator and Compound right to left
assignment operator
Example 1
int x, y, z;
x = 5;
y = 8;
z = ++x + y++;
Example 2
int a, b, c;
a = 10;
b = 20;
c = 1;
c += a++ * 5 - --b;
Relational Operators
• Relational operators are used to compare values of two
expressions.
• Relational operators are binary operators because they require two
operands to operate.
• If the relation is true then the result of the relational expression is 1,
if the relation is false then the result of the relational expression is
0.
Logical Operators
• Logical operators are used to evaluate two or more conditions.
Hands On