Lect05 Pre2 PDF
Lect05 Pre2 PDF
Topics
• Expressions
• Precedence
• Function calls
• Comments printf(“Hello World”);
Expressions
• Combine values using operators and
function calls
• Return a value of a known type
Arithmetic Expressions
• take arithmetic (numerical) values and
• return an arithmetic (numerical) value
• Are composed using the following operators:
+ (unary plus)
- (unary minus)
+ (addition)
- (subtraction)
* (multiplication)
/ (division or quotient)
% (modulus or remainder)
Precedence in Expressions
• Defines the order in which an expression is
evaluated
Precedence in Expressions -- Example
1 + 2 * 3 - 4 / 5 =
1 + (2 * 3) - (4 / 5)
B.O.D.M.A.S.
2 * 3 / 4 = (2 * 3) / 4 = 6 / 4
2 / 3 * 4 = (2 / 3) * 4 = 0 * 4
Precedence in Expressions –
Example (cont)
1 + 2 * 3 - 4 / 5 =
1 + (2 * 3) - (4 / 5)
6.2
Precedence in Expressions –
Example (cont)
1 + 2 * 3 - 4 / 5 =
1 + (2 * 3) - (4 / 5)
6.2
Precedence in Expressions –
Example (cont)
1 + 2 * 3 - 4 / 5 =
1 + (2 * 3) - (4 / 5)
Integer division
results in integer
quotient
Precedence in Expressions –
Example (cont)
1 + 2 * 3 - 4 / 5 =
1 + (2 * 3) - (4 / 5)
oh
= 0
Precedence in Expressions –
Example (cont)
1 + 2 * 3 - 4 / 5 =
1 + (2 * 3) - (4 / 5)
7
int-s and float-s
• float is a “communicable” type
• Example:
1 + 2 * 3 - 4.0 / 5
= 1 + (2 * 3) - (4.0 / 5)
= 1 + 6 - 0.8
= 6.2
int-s and float-s – Example 2
(1 + 2) * (3 - 4) / 5
= ((1 + 2) * (3 - 4)) / 5
= (3 * -1) / 5
= -3 / 5
= 0
int-s and float-s – Example 2
(cont)
(1 + 2.0) * (3 - 4) / 5
= ((1 + 2.0) * (3 - 4)) / 5
= (3.0 * -1) / 5
= -3.0 / 5
= -0.6
int-s and float-s – Example 3
(1 + 2.0) * ((3 - 4) / 5)
= (1 + 2.0) * (-1 / 5)
= 3.0 * 0
= 0.0
Unary operators
• Called unary because they require one operand.
• Example
i = +1; /* + used as a unary operator */
j = -i; /* - used as a unary operator */
set result to
1+2*3-4/5
output result
Example -- Simple Expressions (cont)
Evaluate an expression #include <stdio.h>
/* Evaluate an expression */
set result to
1+2*3-4/5
output result
Example -- Simple Expressions (cont)
Evaluate an #include <stdio.h>
expression
/* Evaluate an expression */
int main()
{
set result to
1+2*3-4/5
output result
return 0;
}
Example -- Simple Expressions (cont)
Evaluate an #include <stdio.h>
expression
/* Evaluate an expression */
int main()
{
float result;
set result to
1+2*3-4/5
output result
return 0;
}
Example -- Simple Expressions (cont)
Evaluate an #include <stdio.h>
expression
/* Evaluate an expression */
int main()
{
float result;
set result to
1+2*3-4/5 result = 1 + 2 * 3 - 4 / 5;
output result
return 0;
}
Example -- Simple Expressions (cont)
Evaluate an #include <stdio.h>
expression
/* Evaluate an expression */
int main()
{
float result;
set result to
1+2*3-4/5 result = 1 + 2 * 3 - 4 / 5;
output result printf(“%f\n”, result);
return 0;
}
Example -- Simple Expressions (cont)
Evaluate an #include <stdio.h>
expression
/* Evaluate an expression */
int main()
{
float result;
set result to
1+2*3-4/5 result = 1 + 2 * 3 - 4 / 5;
output result printf(“%f\n”, result);
Output: return 0;
7.000000 }
Topics
Expressions
Precedence
• Function calls
• Comments
Function Calls
• Tell the computer to execute a series of C
commands and (maybe) return a value
– In algorithm-speak: An invocation of a named
sequence of instructions
• Example: printf, scanf, sqrt
Example --Find the square root
output myResult
Example -- Find the square root (cont)
#include <stdio.h>
#include <math.h>
/* Find square root */
int main()
{
/* declare variables */
float x,myResult;
/* output myResult */
printf(“Result is %f\n”,myResult);
return 0;
}
Topics
History of C
Structure of a C program
Values and variables
Expressions
Function calls
• Comments
Comments
• Essential for documenting programs
• Run from a /* to the next */
• Examples:
/* THIS IS A COMMENT */
/* So is
this */
/*
** ...and this.
**
*/
Comments (cont)
• Comments do not “nest”