Title: REACH TEST REVIEW
1REACH TEST REVIEW
2Parts of the Program
- / C Programming for the Absolute Beginner /
- // by Michael Vine
- include ltstdio.hgt
- include ltstdlib.hgt
- main()
-
- printf(\nC you later\n)
- system(pause)
3(No Transcript)
4Program Statements
- Serve to control program execution and
functionality. Must end with a semicolon() with
the exception of - Comments / /
- Preprocessor Directives include or define
- Begin and end program identifiers
- Function definition beginnings main()
5The main function
- Functions allow you to group program statements
under one name - C is case-sensitive so main(), MAIN(), and Main()
are all different - The main function is special because the values
it returns are returned to the operating system - Most main functions in this course do not take or
pass information to the operating system
6Escape Sequences
- Definition Escape sequences are specially
sequenced characters used to format output - \
- Ex printf( \ This is quoted text \ )
- \
- Ex printf( \n A single quote looks like \
\n) - \ \ Comment Block
7Directives
- include ltstdio.hgt
- Using a directive to include a header file
- stdio.h standard input output header file
- stdlib.h system commands
8Memory
- A computers long-term memory is called
nonvolatile memory and is generally associated
with mass storage devices, such as hard drives. - A computers short term memory is called volatile
memory. It loses is data when power is removed
from the computer
9Data Types
Data Type Description Declaration Example
Integer Whole numbers, positive or negative int x -3 , 0, 3 , 29
Floating-point number All numbers, positive or negative, decimals and fractions float x -0.35543 , 0.00, 554433.33281
Character Representations of integer values known as character codes char x m, M,
To declare a constant (read only) value const
int x 20 const float PI 3.14
10Variable Types
TYPE SIZE VALUES
bool 1 byte true (1) or false (0)
char 1 byte a toz , A to Z, 0 to 9, space, tab, and so on
int 4 bytes -2,147,483,648 to 2,147,483,647
short 2 bytes -32,768 to 32,767
long 4 bytes -2,147,483,648 to 2,147,483,647
float 4 bytes - (1.2 x 10-38 to 3.4 x 1038)
double 8 bytes - (2.3 x 10-308 to -1.7 x 10308)
11Printf() Scanf()
- Can you explain what the code is doing?
12Conversion Specifiers
- Character - c
- Integer - d
- Float (decimal)- f
- String - s
- Printf Format Tags
- flagswidth.precisionlengthspecifier
- .precisionspecifer -gt .2f
13Can you predict the printout?
- int main()
-
- printf (c c \n", 'a', 65)
- printf (" 10d \n", 1977)
- printf ("010d \n", 1977)
- printf ("floats 4.2f \n", 3.1416)
- printf ("s \n", "A string")
- printf(f \n, 55.55)
- return 0
14Printouts
- printf (c c \n", 'a', 65) aA
- printf ("d ld\n", 1977, 650000L) 1977650000
- printf (" 10d \n", 1977)
1977 - printf ("010d \n", 1977) 0000001977
- printf ("floats 4.2f \n", 3.1416)
3.14 - printf ("s \n", "A string") A string
- Can you create a tabular data using printf?
15Review Printing with Precision
Example Printout
printf(.1f,3.123456) 3.1
printf(\n.2f,3.123456) 3.12
printf(\n.3f,3.123456) 3.123
printf(\n.4f,3.123456) 3.1235
printf(\n.5f,3.123456) 3.12346
printf(\n.6f,3.123456) 3.123456
16Scanf
- Syntax
- scanf(conversion specifier, variable)
Conversion Specifies Description
d Receives integer value
f Receives floating-point number
c Receives character
17Can you predict the printout when the user enter
2 and 4?
- include ltstdio.hgt
- main()
-
- int iOperand1 0
- int iOperand2 0
- printf(\n Enter first operand )
- scanf(d, iOperand1)
- printf(\n Enter second operand )
- scanf(d, iOperand2)
-
- printf(The result is d \n, 24/(iOperand1
iOperand2)6/3)
18Arithmetic and Order of Precedence
Operator Description
Multiplication
/ Division
Modulus (remainder)
Addition
- Subtraction
Order of Precedence Description
( ) Parentheses are evaluated first, from innermost to outermost
, /, Evaluated second, from Left to Right
, - Evaluated last, from Left to Right
19Can you predict the printout?
- include ltstdio.hgt
- main()
-
- int x 4
- int y 9
- int result1, result2
-
- result1 y/x
- result2 yx
- printf(The result is d.d \n, result1,
25result2)
20Conditions and Operators
Operator Description
Equal to
! Not Equal
gt Greater Than
lt Less Than
gt Greater Than or Equal to
lt Less Than or Equal to
Order of Precedence Description
AND condition
OR condition
21Boolean Operators
- Do you know the answers to these?
- A. !( 1 0 )
- B. !( 1 1 0 )
- C. !( ( 1 0 ) 0 )
22Boolean Operators Quiz Answers
- A. !( 1 0 ) ANSWER 0
- B. !( 1 1 0 ) ANSWER 0 (AND is
evaluated before OR) - C. !( ( 1 0 ) 0 ) ANSWER 1 (Parenthesis
are useful)
23If Statements
24Quiz
- Can you write code that will ask a user to enter
a number 1 , 2 , or 3 and print out the
following
User Input Printout
1 1 is the loneliest number
2 2 is better than 1
3 3s a crowd
25Answer
- include ltstdio.hgt
- int main()
-
- int a
- printf (Enter one of the following d, d, or
d\n, 1, 2, 3) - scanf(d, a)
- if(a1 a2 a 3)
-
- if(a1)
- printf(\n d is the loneliest number \n, 1)
-
- if(a2)
- printf(\nd is better than d \n,2,1)
-
- if(a3)
- printf(\nd \ s a crowd \n,3)
-
- else
26Switch-Case Statments
27Example Switch-Case Statement
28Loops
- while ( condition ) Code to execute while the
condition is true - Quiz Can you write a program that prints x
- while x increments from 0 to 10?
29Answer While Quiz
30Operators Cont.
x Tells the function to use the current value
of x and increment it by 1. x Increment the
value of x by 1 and use the new value for
calculations. x-- Tells the function to use
the current value of x and decrease its value by
1. --x Decrease the value of x by 1 and use
the new value for calculations. x0 printf(The
Value of x is d, x) printf(\n The Value of
x is d,x) Would results in The Value of x
is 0 The Value of x is 2
Application of - Equivalent Equation
x y x x y
x - y x x y
31For Loop
- Often used when the of iterations is already
known. - Contains 3 separate expressions
- Variable initialization
- Conditional expression
- Increment/Decrement
- Try writing a program with a for loop that
counts down from 10 seconds.
32For Loop
- include ltstdio.hgt
- main()
-
- int x0
- for(x10 xgt0 x--)
-
- printf("d \n", x)
-
- system("pause")
33Break/Continue Statements
- break
- Used to exit a loop. Once this statement is
executed the program will execute the statement
immediately following the end of the loop. - continue
- Used to manipulate program flow in a loop. When
executed, any remaining statements in the loop
will be skipped and the next iteration of the
loop will begin
34Function Prototypes Definitions
- Function Prototype Syntax
- return-type function_name ( arg_type arg1, ...,
arg_type argN) - Function Prototypes tell you the data type
returned by the function, the data type of
parameters, how many parameters, and the order of
parameters - Function definitions implement the function
prototype - Where are function prototypes located in the
program? - Where do you find function definitions?
35Function Prototypes
- Where are function prototypes located in the
program? - Answer before the Main() Function!
- Function Definitions are self contained outside
of the Main() function
36Calling Functions
include ltstdio.hgt int mult ( int, int ) int
main() int x int
y printf( "Please input two numbers to be
multiplied " ) scanf( "d", x ) scanf(
"d", y ) printf( "The product of your two
numbers is d\n", mult( x, y ) ) getchar()
int mult (int a, int b) return a b
37Whats Wrong with This Code?
include ltstdio.hgt Void printReportHeader() ma
in() printReportHeader void
printReportHeader() printf(\n
Column1\tColumn2\tColumn3\tColumn4 \n)
38Can You Pick Out the Local and Global Variables?
include ltstdio.hgt void printNumbers() int
iNumber main() int x for(x0,
xlt10,x) printf(\n Enter a
number) scanf(d, iNumber) printNumbers
() void printNumbers() printf(\n Your
number is d \n, iNumber)
39Variable Scope
- Variable scope defines the life time of a
variable - Local Scope defined within functions and loses
scope after function is finished. Can reuse in
other functions (ex. p.123) - Global Scope defined outside of functions and
can be accessed by multiple functions
40Questions?