Simple C Programs - PowerPoint PPT Presentation

1 / 54
About This Presentation
Title:

Simple C Programs

Description:

Comments begin with the characters /* and end with the characters ... comment. last line of main program. required closing brace. comment. 6. Keywords. auto. break ... – PowerPoint PPT presentation

Number of Views:42
Avg rating:3.0/5.0
Slides: 55
Provided by: jeanine4
Category:

less

Transcript and Presenter's Notes

Title: Simple C Programs


1
Chapter 2
  • Simple C Programs

2
Program Structure - General Form
preprocessing directives int main(void) declara
tions statements
3
Program Structure
  • Comments begin with the characters / and end
    with the characters /
  • Preprocessor directives give instructions to the
    compiler
  • Every C program contains one function named main
  • The body of the main function is enclosed by
    braces,

4
Program Structure - continued
  • The main function contains two types of commands
    declarations and statements
  • Declarations and statements are required to end
    with a semicolon ()
  • Preprocessor directives do not end with a
    semicolon
  • To exit the program, use a return 0 statement

5
Program Structure - First Program
// /
Program chapter1 / /
/ / This
program computes the sum two numbers / include
ltstdio.hgt int main(void) / Declare and
initialize variables. / double number1
473.91, number2 45.7, sum / Calculate
sum. / sum number1 number2 /
Print the sum. / printf(The sum is 5.2f
\n, sum) / Exit program. / return
0 //
comment comment comment comment preprocessor
directive ltgt means file included in
standard C library required name of
routine required opening brace comment declaration
comment assignment comment output
statement comment last line of main
program required closing brace comment
6
Keywords
  • auto
  • break
  • case
  • char
  • const
  • continue
  • default
  • do

double else enum extern float for goto if
int long register return short signed sizeof stati
c
truct switch typedef union unsigned void volatile
while
7
Constants and Variables
8
Constants and Variables
  • A constant is a specific value
  • A variable is a memory location that is assigned
    a name or an identifier
  • An identifier is used to reference a memory
    location.
  • Rules for selecting a valid identifier
  • must begin with an alphabetic character or
    underscore
  • may contain only letters, digits and underscore
    (no special characters)
  • case sensitive
  • can not use keywords as identifiers
  • only FIRST 31 characters significant

9
C Data Types
  • Integers
  • short
  • int
  • long
  • Floating-Point Values
  • float
  • double
  • long double
  • Characters
  • char

10
Data Type Limits (System Dependent)
  • Integers
  • short Maximum 32,767
  • int Maximum 32,767
  • long Mqaximum 2,147,483,647
  • Floating point
  • float 6 digits of precision
  • Max exponent 138
  • Max value 3.402823e138
  • double 15 digits of precision
  • Max exponent 1308
  • Max value 1.797693e1308
  • long double 19 digits of precision
  • Max exponent 14932
  • Max value 1.189731e14932

11
Symbolic Constants
  • Defined with a preprocessor directive
  • Compiler replaces each occurrence of the
    directive identifier with the constant value in
    all statements that follow the directive
  • Example
  • define PI 3.141593

12
Examples Identifier Names
  • Which of the following names are valid
    identifiers if not valid why not
  • 1. density 2. area 3. Time
  • 4. xsum 5. x_sum 6. tax-rate
  • 7. perimeter 8. sec2 9. degrees_C
  • 10. break 11. 123 12. xy
  • 13. count 14. void 15. f(x)
  • 16. f2 17. Final_Value 18. w1.1
  • 19. reference1 20. reference_1 21. m/s
  • 22. void2 23. 1side 24. Auto

13
Assignment Statements
14
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
15
Assignment Statements - continued
  • Example 3
  • int x, y, z
  • xy0
  • z2 x
  • y
  • z
  • Example 4
  • yz y

0
0
2
2
16
Arithmetic Operators
  • Addition
  • Subtraction -
  • Multiplication
  • Division /
  • Modulus
  • Modulus returns remainder of division between two
    integers
  • Example
  • 52 returns a value of 1

17
Integer Division
  • Division between two integers results in an
    integer.
  • The result is truncated, not rounded
  • Example
  • 5/3 is equal to 1
  • 3/6 is equal to 0

18
Practice
  • Give value computed by each of the following
  • 1. int a 27, b 6, c
  • c ba
  • 2. int a
  • float b 6, c 18.6
  • a c/b
  • 3. int a 27, b 6
  • float c
  • c a/(float)b
  • 4. int b 6
  • float a, c 18.6
  • a (int) c/b

5. int sum 18, count 5 float average
average sum/count
19
Priority of Operators
  • Parentheses Inner most first
  • Unary operators Right to left
  • ( -)
  • Binary operators Left to right
  • ( / )
  • Binary operators Left to right
  • ( -)

20
Increment and Decrement Operators
  • Increment Operator
  • post increment x
  • pre increment x
  • Decrement Operator - -
  • post decrement x- -
  • pre decrement - -x

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

22
(No Transcript)
23
Standard Input and Output
24
Standard Output
  • printf Function
  • prints information to the screen
  • requires two arguments
  • control string
  • conversion specifier
  • Example
  • double angle 45.5
  • printf(Angle .2f degrees \n, angle)
  • Output
  • Angle 45.50 degrees

25
Standard Input
  • scanf Function
  • inputs values from the keyboard
  • required arguments
  • control string
  • memory locations that correspond to the
    specifiers in the control string
  • Example
  • double distance
  • char unit_length
  • scanf("1f c", distance, unit_length)
  • It is very important to use a specifier that is
    appropriate for the data type of the variable

26
(No Transcript)
27
Practice!
Assume that the integer variable sum contains the
value 65, the double variable average contains
the value 12.368 and that the char variable ch
contains the value 'b'. Show the output line
(or lines) generated by the following statements.
  • printf("Sum 5i Average 7.1f \n", sum,
    average)
  • printf("Sum 4i \n Average 8.4f \n", sum,
    average)
  • printf("Sum and Average \n\n d .1f \n", sum,
    average)
  • printf("Character is c Sum is c \n", ch, sum)
  • printf("Character is i Sum is i \n", ch, sum)

28
Library Functions
29
Math Functions
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.
30
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. The
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 -?, ?.
31
Character Functions
toupper(ch) If ch is a lowercase letter, this
function returns the corresponding uppercase
letter otherwise, it returns ch isdigit(ch) Retu
rns a nonzero value if ch is a decimal digit
otherwise, it returns a zero. islower(ch) Retur
ns a nonzero value if ch is a lowercase letter
otherwise, it returns a zero. isupper(ch) Retur
ns a nonzero value if ch is an uppercase letter
otherwise, it returns a zero. isalpha(ch) Retu
rns a nonzero value if ch is an uppercase letter
or a lowercase letter otherwise, it returns a
zero. isalnum(ch) Returns a nonzero value if ch
is an alphabetic character or a numeric digit
otherwise, it returns a zero.
32
Review True/False
  • A CPU consists of an ALU, memory, and a
    processor.
  • Linking/Loading is the step that prepares the
    object program for execution.
  • An algorithm describes the problem solution step
    by step, while a computer program solves the
    problem in one step.
  • A computer program is the implementation of an
    algorithm.

33
Review Multiple Choice
  • 1. Instructions and data are stored in
  • the arithmetic logic unit (ALU)
  • the control unit (processor)
  • the central processing unit (CPU)
  • the memory
  • the keyboard

34
Review Multiple Choice
  • 2. An operating system is
  • the software that is designed by the users.
  • a convenient and efficient interface between the
    user and the hardware.
  • the set of utilities that allow us to perform
    common operations.
  • a set of software tools.

35
Review Multiple Choice
  • 3. Source code is
  • the result of compiler operations
  • the process of getting information from the
    processor
  • the set of instructions in a computer language
    that solve a specific problem.
  • the data stored in the computer memory.
  • the values entered through the keyboard.

36
Review Multiple Choice
  • 4. Object code is
  • the result of compiler operations on the source
    code.
  • the process of obtaining information from the
    processor.
  • a computer program.
  • a process involving the listing of commands
    required to solve a specific problem.
  • the result of the linking/loading process.

37
Review Multiple Choice
  • An algorithm refers to
  • a step-by-step solution to solve a specific
    problem.
  • a collection of instructions that the computer
    can understand.
  • a code that allows us to type in text materials
  • stepwise refinement
  • a set of math equations to derive the problem
    solution.

38
Constants and Variables
  • A constant is a specific value
  • A variable is a memory location that is assigned
    a name or an identifier
  • An identifier is used to reference a memory
    location.
  • Rules for selecting a valid identifier
  • must begin with an alphabetic character or
    underscore
  • may contain only letters, digits and underscore
    (no special characters)
  • case sensitive
  • can not use keywords as identifiers
  • only FIRST 31 characters significant

39
Examples Identifier Names
  • Which of the following names are valid
    identifiers if not valid why not
  • 1. density 2. area 3. Time
  • 4. xsum 5. x_sum 6. tax-rate
  • 7. perimeter 8. sec2 9. degrees_C
  • 10. break 11. 123 12. xy
  • 13. count 14. void 15. f(x)
  • 16. f2 17. Final_Value 18. w1.1
  • 19. reference1 20. reference_1 21. m/s
  • 22. void2 23. 1side 24. Auto

40
Declaring Things
  • ALL variables MUST be declared
  • char a_character
  • int an_integer
  • float a_float
  • double a_double
  • int number_1, number_2, number_3
  • char a a
  • float num 1.23

41
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
42
Assignment Statements - continued
  • Example 3
  • int x, y, z
  • xy0
  • z2 x
  • y
  • z
  • Example 4
  • yz y

0
0
2
2
43
Arithmetic Operators
  • Addition
  • Subtraction -
  • Multiplication
  • Division /
  • Modulus
  • Modulus returns remainder of division between two
    integers
  • Example
  • 52 returns a value of 1

44
Integer Division
  • Division between two integers results in an
    integer.
  • The result is truncated, not rounded
  • Example
  • 5/3 is equal to 1
  • 3/6 is equal to 0

45
Practice
  • Give value computed by each of the following
  • 1. int a 27, b 6, c
  • c ba
  • 2. int a
  • float b 6, c 18.6
  • a c/b
  • 3. int a 27, b 6
  • float c
  • c a/(float)b
  • 4. int b 6
  • float a, c 18.6
  • a (int) c/b

5. int sum 18, count 5 float average
average sum/count
46
Priority of Operators
  • Parentheses Inner most first
  • Unary operators Right to left
  • ( -)
  • Binary operators Left to right
  • ( / )
  • Binary operators Left to right
  • ( -)

47
Increment and Decrement Operators
  • Increment Operator
  • post increment x
  • pre increment x
  • Decrement Operator - -
  • post decrement x- -
  • pre decrement - -x

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

49
(No Transcript)
50
Standard Input
  • scanf Function
  • inputs values from the keyboard
  • required arguments
  • control string
  • memory locations that correspond to the
    specifiers in the control string
  • Example
  • double distance
  • char unit_length
  • scanf("1f c", distance, unit_length)
  • It is very important to use a specifier that is
    appropriate for the data type of the variable

51
Example ProgramFinding Leap Years
  • A leap year is defined as one that is evenly
    divisible by 4.
  • If the year is a century year (100, 200, 300,
    etc.) then it must be evenly divisible by 400 to
    be a leap year

52
You Try It
  • Purpose of the program
  • Required input (source)
  • Process on input
  • Required output

53
You Try It
  • Purpose of the program
  • To determine if an imput year is a leap year
  • Required input (source)
  • User inputs a year
  • Process on input
  • Determine if year is evenly divisible by 4 or 400
  • Required output
  • Leap year or Not Leap year

54
Program
/ Program to determine if an input year is a
leap year or not / include ltstdio.hgt main() in
t year / user input year / puts() puts(Th
is program determines if an input year is a leap
year or not) printf(Enter year gt
) scanf(i,year) if (year400
0) puts(Year is a century leap
year) else if(year4 0) puts(Year is a
leap year) else puts(Year is NOT a leap
year) getchar() getchar()
Write a Comment
User Comments (0)
About PowerShow.com