Conditional expression - PowerPoint PPT Presentation

1 / 39
About This Presentation
Title:

Conditional expression

Description:

If (a b) z=a; else z=b; ... Z=(a b)? a : b; /*z=max (a, b)*/ If a, b, z are of different data type, use type conversion rule mentioned before. ... – PowerPoint PPT presentation

Number of Views:25
Avg rating:3.0/5.0
Slides: 40
Provided by: weiku
Category:

less

Transcript and Presenter's Notes

Title: Conditional expression


1
Conditional expression
  • A conditional statement
  • If (agtb) za
  • else zb
  • There is a conditional expression which is
    equivalent to the conditional statement
  • Z(agtb)? a b /zmax (a, b)/
  • If a, b, z are of different data type, use type
    conversion rule mentioned before.

2
Basic I/O
  • Character based I/O
  • int getchar(void) / read a character/
  • void putchar(int c) /output c as character/
  • int main ()
  • int c
  • printf("Please key in a character")
  • cgetchar()
  • printf("Here is what you have provided")
  • putchar(c)
  • Here use int for c to hold EOF which means the
    end of file.

3
Basic I/O
  • Formatted I/O
  • int printf (char format, arg1,arg2,)
  • int scanf(char format,)

4
Basic I/O
  • Formatted IO
  • int main ( )
  • int i 123,j
  • double f 3.1415926535,d
  • char s8 "ABCDEFG"
  • char mabcdef
  • / formatted output with printf /
  • printf ("i d g g s s \n", i, f, s)
  • / Get input with scanf /
  • scanf ("d g s", j, d, s) / Note the ''
    before the variables /
  • Note if you use scanf( s, m) here, there will
    be a segmentation
  • fault do you know why?

5
Basic I/O
  • Notes
  • includeltstdio.hgt
  • scanf use WHITE SPACE to separate input for
    different arguments
  • used in printf and scanf is to indicate the
    format of I/O, not the same as operator
  • printf can be used for debugging your program

6
Compound Statement/Blocks
  • A compound statement is a set of statements
    enclosed within
  • braces
  • int main()
  • int x1
  • int x0
  • printf("d",x)
  • printf("d\n",x)
  • return 0
  • Output 0 1

7
If-else
  • If (expression)
  • statement1
  • else if (expression)
  • statement2
  • else
  • statement3

8
While
  • / compute 12. n/
  • int sum(int n)
  • int result0,i1
  • while(iltn)
  • resulti
  • return result

9
For
  • / compute 12. n/
  • int sum(int n)
  • int i
  • int result0
  • for(i1iltn i)
  • resulti
  • return result

10
Do-while
  • / compute 12. n/
  • int sum(int n)
  • int result0,i1
  • do
  • resulti
  • while(iltn)
  • return result

11
Switch and Break
  • Switch is a multi-way decision that tests whether
    an expression matches one of a number of constant
    integer values and braches accordingly.
  • Switch( expression)
  • case const-expression statements
  • case const-expression statements
  • default statements

12
Switch example
  • include ltstdio.hgt / show the use of switch /
  • int main ()
  • char encryption
  • printf("Please choose an encryption level ")
  • scanf("c", encryption)
  • switch (encryption) / Must be a integral number
    /
  • case 's' printf("You have chosen the
    simple
  • encryption.\n")
  • break
  • case 'm'printf("You have chosen the
    medium
  • encryption.\n")
  • break
  • default printf("You have chosen the ultra
    encryption.\n")
  • break

13
Break and Continue
  • Break and continue interrupt the normal flow of
    control.
  • while(1)
  • sumsumi
  • if(ix) break
  • i
  • for(i0ilt10i)
  • if((i2)0) continue
  • sumsumi

14
Function prototype
  • Functions break large computing tasks into
    smaller ones, functions in C separates the data
    and what will be done on those data.
  • Function prototype (function declaration)
  • return type function-name (argument
    declarations)
  • int sum( int n)
  • If return type is omitted, int is assumed
  • If the function you used is defined after where
    it is used, prototype should be placed before
    where it is used. Otherwise there may be
    complains from compiler.

15
Function definition
  • Function definition only once in the whole
    program
  • return type function-name (argument declarations)
  • Declarations and statements
  • int swap(int x, int y)
  • int k
  • kx
  • xy
  • yk
  • return 0

16
Function call
  • Function calls
  • parameter passing-call by value.
  • Example
  • get returned value
  • Recursion
  • A function can call itself
  • Directly
  • Indirectly
  • examples
  • Calculate the power of x to y
  • double power (int x, int y)
  • if(y1) return x
  • else return xpower (x,y-1)

17
Example
  • include ltstdio.hgt
  • int swap(int x, int y)
  • int k
  • kx
  • xy
  • yk
  • return 0
  • int main( )
  • int x1
  • int y2
  • swap(x,y)
  • printf(d\td\n, x, y)

18
Program Organization
  • Separated files, separated Compilation
  • Header files
  • Header files normally are made up with external
    declaration of global variables and prototype of
    functions
  • can be included by .c files
  • Put functions into different c files according to
    their functionalities
  • function definition can not be put into header
    files
  • Global variable can only be declared (not
    external declaration) once
  • Function can only be defined once
  • Function can not be defined in a function

19
Example
  • c.h
  • / c.h /
  • int x 0 / Wrong/
  • extern int x/ Right/
  • void inc_value(void)
  • void dec_value(void)

20
Example (continued)
  • / a.c /
  • include ltstdio.hgt
  • include "c.h
  • int x / declare x as global variable /
  • void inc_value()
  • x
  • int main()
  • x 0
  • inc_value()
  • dec_value()
  • fprintf(stdout, "The value of x is d\n", x) /
    expecting 0 /

21
Example (continued)
  • b.c
  • / b.c /
  • include "c.h"
  • void dec_value()
  • x --

22
Preprocessing
  • Preprocessing directives
  • file inclusion
  • include ltstdio.hgt
  • include sum.h
  • code replacement (macro substitution)
  • define name replacement text
  • Example
  • conditional inclusion
  • if !defined (HDR)
  • define HDR
  • endif

23
Example
  • include ltstdio.hgt
  • define square(x) xx
  • int main( )
  • int y8
  • int zsquare(y1)
  • / y1y1 17 /
  • printf(d\n,z)
  • return 0
  • What do you think this program will output? Will
    it output 81? If not, explain why.

24
Makefile Getting Started
  • What is Make?
  • Make is a program that looks for a file called
    "makefile" or "Makefile", within the makefile are
    variables and things called dependencies.

25
Makefile
  • Use makefile
  • manage your project (several separated files)
  • automate and optimize the construction of
    programs/files

26
Format of Makefiles -- Environment Variables
  • Variable definitions are in the following format
  • VARNAME Value
  • such as
  • CC gcc
  • this assigns the string "gcc to the variable
    CC.
  • To expand variables, use the following form
  • VARNAME
  • such as
  • CC

27
Format of Makefiles -- Dependencies
  • Dependencies rules are the heart of makefiles.
  • Dependencies rules have the following form
  • Target dependencyA dependencyB ... dependencyN
  • command for target
  • Very Important
  • The commands underneath the dependencies must
    have a tab before them. This lets make know it is
    a command and not some kind of crazy dependency.
  • myprogram main.c function.c
  • gcc o myprogram main.c function.c

28
A simple makefile
  • CC gcc
  • lab main.o function.o
  • CC o lab main.o function.o
  • main.o main.c
  • CC -c main.c
  • function.o function.c function.h
  • CC -c function.c
  • clean
  • rm -rf lab .o
  • Type make on prompt to generate executable
    programType make clean
  • on prompt to remove the executable Program and
    object files.

29
Pointers and Arrays
  • Pointers and Arrays
  • Pointers and Addresses
  • Pointers and Function Arguments
  • Pointers and Arrays
  • Address arithmetic
  • Pointer Arrays Pointers to Pointers

30
Pointers and Addresses
  • Pointer is a special data type, which specify a
    special kind of variables holding the address of
    a piece of memory space.
  • char a / hold the address of character /
  • int b / hold the address of integer /
  • double c / hold the address of double /
  • char a100 / hold 100 addresses of character
    /
  • char b100 / the array name b holds the
    beginning address of
  • consecutive 100
    characters, i.e. it holds the address of
  • a character array/

31
Pointers and Addresses
  • A byte-addressable machine has consecutively
    numbered or addressed memory cells (bytes) which
    may be manipulated individually or in contiguous
    groups.

char ip1
char
ip1
short
int/long
ip2
double
double ip2
32
Pointers and Addresses
  • a pointer array holding addresses of characters
  • char a2
  • A pointer array holding addresses of integers
  • int b2

33
Pointers and Addresses
  • pointers and references vs
  • is used on a variable (can be char, int,
    double, array element) to get the address
  • int i, ip1, ip2, a8
  • char c, cp
  • ip1 i
  • ip2 a0
  • cp c

34
Pointers and Addresses
  • pointers and references vs
  • can not be used on a constant, array name and
    expressions
  • char ip1
  • char b10
  • int ip2
  • ip1y / WRONG /
  • ip1abcdef /WRONG/
  • ip1abcedf /RIGHT/
  • ip27 / WRONG /
  • ip2b /WRONG/
  • ip1b /RIGHT /
  • ip2(ip12)/WRONG/

35
Pointers and Addresses
  • pointers and references vs
  • is used on pointer, to access the object the
    pointer points to.
  • int x 1,y22,3
  • int ip1,ip2,ip3
  • ip1x
  • ip10 /x0/
  • printf(d\td\n,x,y0)
  • ip2y0
  • ip21 /y01/
  • printf(d\n,y0)
  • ip3y /ip3y0/
  • ip30 /y00/
  • printf(d\n,y0)

36
Pointers and Addresses
  • pointers and references vs
  • pointer has a data type which is the same as
    that of the
  • object the pointer points to.
  • int a
  • every pointer points to a specific data type
    except void
  • void is used to hold the address of any data
    type.
  • pointer can be used where the object the
    pointer points to can be used.
  • int x
  • int y0
  • xy
  • xx1 / yy1 /
  • x /y /
  • (x) / y /
  • y(x) / add 4 to x /
  • y(x) / add 4 to x /
  • Incrementing (decrementing) a pointer
    increases (decreases) it by the size of the data
    type it points to.

37
Pointers and Function Arguments
  • C passes arguments to functions by value
  • swap(int x, int y) / change local copies of x,
    y/
  • swap(int x, int y) /use pointer to change
    the content of two variables /

38
Parameter Passing -- Example
void swap (int x, int y) int temp temp
x x y y temp
void swap (int x, int y) int temp
temp x x y y temp
39
Pointers and Function Arguments
  • Still Pass-by-value with pointers
  • However the memory content can change
  • Attention
  • Be aware of the argument type when passing
    parameters
  • swap (int, int )
  • swap(x,y) /pass addresses of two integer
    variables/
  • When you pass an array name to a function, what
    is passed is the address of the first element of
    this array.
  • You can pass part of an array to a function
  • f(a2)
Write a Comment
User Comments (0)
About PowerShow.com