3 Operators and Expressions
3 Operators and Expressions
int main(void) {
int b=10, d=20, c=1;
int a = b + (c = d / b) - 1;
printf("a: %d",a);
return 0;
}
Find the output of the Program
#include <stdio.h>
int main()
{
int a = 10, b = 20, c =30;
if (c > b > a)
printf("TRUE");
else
printf("FALSE");
return 0;
}
Evaluate the expressions
Side-effect
• a=b++ + ++b (b=3)
• a=b*=c++ + ++d+3 (a=3,b=3,c=2,d=2)
• a=b&&++c (b=0, c=5) a=b||++c (b=1,c=5)
int main()
int main()
{
{
double x = 1.2;
int x = 10;
char y = 'a'; int sum = (int)x + 1;
x = x + y;
float z = x + 1.0f; printf("sum = %d", sum);
printf("x = %d, z = %f", x, z);
return 0;
return 0;
}
}
Type Conversions
• Narrowing Conversion
• Widening Conversion
• Implicit Conversion
• Coercion
• Explicit Conversion
(int) sum
Escape Sequences
Examples
• Write a program to print
“Hello World”
• Write a program to print
\Hello World\
• Write a program to print
The format specifiers: float:%f
integer:%d
Symbolic Constants
• Occurrence of a number or constant can be replaced by a meaningful
name
Syntax:
#define name replacement text
Eg.
#define PI 3.14159
Comments
• Single line comments
eg. //This is a comment
• Multi-line comments
eg. /* This is a
Multi-line comment */