The C Programming Language continued - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

The C Programming Language continued

Description:

Can have complicated expressions on the right side. ... asin(x) Computes the arcsine or inverse sine of x, where x must be in the range [-1, 1] ... – PowerPoint PPT presentation

Number of Views:28
Avg rating:3.0/5.0
Slides: 20
Provided by: ValuedGate899
Category:

less

Transcript and Presenter's Notes

Title: The C Programming Language continued


1
Chapter 3
The C Programming Language (continued)
2
Assignment Statements
3
Assignment Statements
  • Used to assign a value to a variable
  • General Form
  • identifier expression
  • Example 1
  • double sum 0 sum
  • Example 2
  • int x
  • x5 x
  • Example 3
  • char ch
  • ch a a

0
5
a
4
Assignment Statements
  • Can have complicated expressions on the right
    side. They must be evaluated and the resulting
    value is assigned to the target variable
    (identifier) on the left of the assignment
    operator.
  • Ex. simple assignment b x1 i i 1
  • Others compound assignment
  • x 4 same as x x4 If x
    3, after, x 7
  • x y same as x x y
  • x 4 (assume x 7) then
    after, x 3
  • x 3 (assume x 7) then
    after x 1

5
Assignment Statements - continued
  • Example 4
  • int x 0, y 0, z
  • z2
  • x
  • y
  • z
  • Example 5
  • yz y

6
Assignment Statements - continued
  • Mixed-type Assignment Statement
  • When an assignment statement is executed, the
    expression is first evaluated then the result is
    assigned to the variable listed to the left of
    the assignment operator ().
  • Either a type double or a type int expression
    may be assigned to a type double variable.
  • Note in a mixed-mode assignment, the expression
    has a different data type from the target
    variable.
  • Example
  • int m 3, n 2
  • double p 2.0 m
  • double x, y
  • x m / p n
  • y m / n p
  • x

3
2
2.0
1.5
1.0
7
Increment and Decrement Operators
  • Increment Operator
  • post increment x
  • pre increment x
  • Decrement Operator - -
  • post decrement x- -
  • pre decrement - -x

8
Abbreviated Assignment Operator
  • operator example equivalent statement
  • x2 xx2
  • - x-2 xx-2
  • xy xxy
  • / x/y xx/y
  • xy xxy

9
Special Statements
  • Should recognize these and know how they work.
  • How about a b c ? operators
    associate right to left
  • Really is a (b c)
  • What results? int a, b
  • float x, y
  • a x y b 1.5f
  • How about int ioResult scanf (d, x)
  • assigns an integer value to x
  • also provides the number of successful
    conversions to ioResult.
  • This is a side effect of scanf(). (may be
    very useful later!!)

10
Library Functions
11
Library Functions
  • A primary goal of software engineering is to
    write error-free code.
  • Code reuse - reusing, whenever possible, program
    fragments that have already been written and
    tested is one way to accomplish this goal. Why
    reinvent the wheel?
  • C promotes reuse, as do all programming
    languages, by providing numerous library
    functions (such as sqrt) that can be invoked to
    perform complicated mathematical computations.
  • A include must be used in order to have access
    to these functions.
  • Some are positioned within ltstdlib.hgt, others
    are made available by using a include ltmath.hgt
    preprocessor statement in your program.

12
Math Functions that are Available
fabs(x) Absolute value of x. sqrt(x) Square root
of x, where xgt0. pow(x,y) Exponentiation, xy.
Errors occur if x0 and ylt0, or if xlt0 and y
is not an integer. ceil(x) Rounds x to the
nearest integer toward ? (infinity). Example,
ceil(2.01) is equal to 3. floor(x) Rounds x to
the nearest integer toward -? (negative
infinity). Example, floor(2.01) is equal to
2. exp(x) Computes the value of
ex. log(x) Returns ln x, the natural logarithm
of x to the base e. Errors occur if
xlt0. log10(x) Returns log10x, logarithm of x to
the base 10. Errors occur if xlt0.
13
Trigonometric Functions
sin(x) Computes the sine of x, where x is in
radians. cos(x) Computes the cosine of x, where
x is in radians tan(x) Computes the tangent of
x, where x is in radians. asin(x) Computes the
arcsine or inverse sine of x, where x must
be in the range -1, 1. Returns an angle in
radians in the range -?/2,?/2. acos(x) Computes
the arccosine or inverse cosine of x, where
x must be in the range -1, 1. Returns an
angle in radians in the range 0,
?. atan(x) Computes the arctangent or inverse
tangent of x. Returns an angle in radians in
the range -?/2,?/2. atan2(y,x) Computes the
arctangent or inverse tangent of the value
y/x. Returns an angle in radians in the range
-?, ?.
14
Character Functions
toupper(ch) If ch is a lowercase letter, this
function returns the corresponding uppercase
letter otherwise, it returns ch isdigit(ch) Ret
urns a non-zero value(representing true) if ch is
a decimal digit otherwise, it returns a zero
(representing false). islower(ch) Returns a
non-zero value if ch is a lowercase letter
otherwise, it returns a zero. isupper(ch) Re
turns a non-zero value if ch is an uppercase
letter otherwise, it returns a
zero. isalpha(ch) Returns a non-zero value if
ch is an uppercase letter or a lowercase
letter otherwise, it returns a
zero. isalnum(ch) Returns a non-zero value if
ch is an alphabetic character or a numeric
digit otherwise, it returns a zero.
15
Sample C program (using built-in functions)
  • include ltstdio.hgt
  • include ltstdlib.hgt
  • include ltmath.hgt
  • int main(void)
  • double first, second, firstSqrt, secondSqrt,
    sumSqrt
  • printf("Enter the first number ")
  • scanf("lf", first)
  • firstSqrt sqrt(first)
  • printf("The square root is .2f\n", firstSqrt)
  • printf("Enter the second number ")
  • scanf("lf", second)
  • secondSqrt sqrt(second)
  • printf("The square root is .2f\n",
    secondSqrt)
  • sumSqrt sqrt(first second)

16
The char type
  • char stores a character variable
  • We can print char with c
  • A char has a single quote not a double quote.
  • We can use it like so

int main() char a, b a 'x' / Set a
to the character x / printf ("a is c\n",a)
b '\n' / This really is one character/
printf (b c",b) return 0
prints b
17
Another Program Demonstrating char Type
include ltstdio.hgt int main(void) char d1,
d2, d3, d4 int x scanf(dcccc, x,
d1, d2, d3, d4) printf(dcccc, x,
d1, d2, d3, d4) return (0) If the
input is What would be the output?
97 534
97 534
18
Strings
ALWAYS enclosed in double quotes! A string has a
length and a size and they are not the
same. (Previews of coming distractions) But
very important!
19
  • A String (any characters enclosed with double
    quotes) always has
  • a null character at the end of the string (the
    \0).
  • This is considered a single character and marks
    the end of the string.
  • Just in case you are interested (more later)
  • AB123 is a string of length five its size
    however is six.
  • Size always includes the null character at the
    end.
Write a Comment
User Comments (0)
About PowerShow.com