STRUCTURED PROGRAMMING - PowerPoint PPT Presentation

1 / 46
About This Presentation
Title:

STRUCTURED PROGRAMMING

Description:

Defining constants add to program's readability and ease of modification. ... used to define constants for easy readability and maintainability. Types. int ... – PowerPoint PPT presentation

Number of Views:204
Avg rating:3.0/5.0
Slides: 47
Provided by: cbastud
Category:

less

Transcript and Presenter's Notes

Title: STRUCTURED PROGRAMMING


1
STRUCTURED PROGRAMMING
  • Introduction to C

2
The History of C Language
  • Developed by Dennis Ritchie
  • at Bell Laboratories 1972
  • Developed on UNIX Operating System

3
LANGUAGE LEVEL
  • C is an intermediate to high-level language
  • High level language - English like
  • Low level - machine language
  • C also provides control of hardware a facility
    not always available with higher level languages

4
Programming Environment
  • C has been a compiled language (each
    statement is translated into machine language)
  • 5 Steps of Compilation
  • preprocessing
  • translating
  • optimizing
  • assembling
  • link editing

5
Strengths of C
  • Portability across different systems
  • Efficiency of C Code
  • Ease of Maintenance
  • Control over low-level operations

6
Weakness of C
  • Syntax is somewhat irregular
  • Operators have context sensitive meanings
  • Allows programmer to bypass the type system,
    which can be misused.

7
Identifiers or Names in C
  • Letters a, digits 1, and underscores _
  • start with a letter or underscore _
  • case sensitive A a
  • max. 31 characters
  • not identical to a keyword (void)
  • count, first_char,TRUE, char1
  • - 1st_integer, void, last-time

8
The Character Set
  • Uppercase alphabetic characters
  • lowercase alphabetic characters
  • digits
  • punctuation
  • formatting
  • graphic characters
  • ASCII

9
Format of C Programs
  • Free-Format Language
  • start at a column
  • no special place
  • single or multiple lines of code

10
Whitespace Characters
  • Separate identifiers or other elements (tokens)
  • space
  • line feed
  • backspace
  • horizontal tap vertical tap
  • form feed
  • carriage return

11
Token
  • The compiler divides a C program into groups of
    characters that belong together
  • Each group is a token.
  • ( ) -

12
Subprograms or Functions
  • A C program is an collection of subprograms
    called functions.
  • Function heading/parameters/fct block
  • type function_name
  • variable declarations
  • code

13
Shortest C Program
  • Function with the name main required
  • void because no value is returned
  • void main ( void )

14
Calling a Function
  • We will have main() call the function
    does_nothing
  • void main ( void )
  • does_nothing ()

15
Output in C
  • Void main ( void )
  • printf ( C by Discovery\n )

16
Output-Special Meaning
  • \t the tab character
  • \b the backspace character
  • \ the double quote character
  • \ the single quote character
  • \\ the backslash character
  • \0 the null character

17
The Structured Approach
  • main ( ) is the driver of the program
  • Each subfunction must be
  • 1. Be defined
  • 2. Be declared and
  • 3. Be called

18
Input and Output with Variables
  • the keyword int is the name for a built-in data
    type used to represent integers
  • the following declares a variable of type
  • int with the name counter
  • int counter
  • can be declared inside or outside of a function
    block

19
Input with scanf () keyboard
  • Scanf () converts intput from the ASCII
    representation entered at the keyboard to the
    internal representation used by the computer.
  • It is similar to the printf ()

20
Arithmetic Operations
  • addition and unary plus
  • - unary minus and subtraction
  • multiplication
  • / division
  • remainder
  • a (a/b) b (ab)

21
Precedence of Arithmetic Operations
  • High Precedence
  • Unary Operators -
  • Multiplicative Operators /
  • Additive Operators -
  • Assignment Operator

22
Compound Assignment
  • The first syntax shortcut is
    compound assignment
  • The assignment statement
  • a a 4
  • can be shortened to
  • a 4

23
Increment and Decrement by 1 Operations
  • Another shortcut in source code is provided for
    incrementing a variable by one.
  • a a 1
  • a 1
  • a

24
Introduction to Functions and Structured
Programming
  • The goals of structured programming include
    writing source code that is modular in nature,
    easily modifiable, robust (handles errors
    gracefully) and readable.
  • A modular program is composed of many independent
    subprograms or functions in C.

25
Functions (Subprograms)
  • Each subprogram or function in C should be
    designed to do one task
  • and should not be too long to be understood
    easily.
  • Another programming goal is to write subprograms
    that are tools and can be used with little or no
    modifications in many programs
  • Variables and constants declared in it

26
Prepocessor Constants
  • Defining constants add to programs readability
    and ease of modification.
  • Preprocessor directives
  • start with
  • include
    include ltstdio.hgt
  • reads external file into source file here
  • define
    define MAXEMPLOYEES 150

27
Function Parameters
  • Using parameters is one method of letting a
    function communicate with the rest of the program
    without depending directly on program variables.
  • A function parameter is used to carry information
    from one function to another.

28
ANSI C Function Definition
  • int f ( int x )
  • The heading will appear before the opening brace
    for the function.
  • This informs the compiler that the function f
    takes a single parameter that will be referred to
    by the name x in the body of the function and
    that the parameter type is int.

29
Function Calls
  • A function call in C consists of the functions
    name, and a pair of parentheses containing the
    actual parameters.
  • A function call can appear anywhere that an
    expression of the corresponding type is allowed.
  • main ( ) calls f several times
  • f ( ) mathematical function

30
Language Elements Introduced
  • Comments
  • Control Statements
  • Conversion Specifications
  • Escape Characters
  • Function Definitions
  • Function Calls
  • Function Declarations

31
Language Elements Introduced
  • Identifiers
  • Library Functions
  • Operators
  • Preprocessor Directives
  • Types
  • Variable Declarations

32
Comments
  • / This is a comment. /

33
Control Statements
  • return
  • Used to impart value to a function and to return
    control from a function to the calling
    environment.

34
Conversion Specifications
  • d conversion to or from decimal
  • x conversion to or from hexadecimal
  • o conversion to or from octal

35
Escape Character
  • \
  • escapes the usual meaning of the next character

36
Escape CharactersExamples
  • \t the tab character
  • \c a carriage return
  • \b the backspace character
  • \ the double quote character string
  • \ the single quote character
  • \\ the backslash character
  • \0 the null character
  • \n the newline character

37
Function Definitions
  • Functions must have a heading and function block.
  • The heading contains the function type, a name
    (identifier), a pair of parentheses, and the
    formal parameters
  • The function block would contain the declaration
    of the local variables and the executable code.

38
Function Calls
  • Consists of the function name,
  • a pair of parentheses
  • and a semicolon.
  • Any actual parameters appear between the
    parentheses.

39
Function Declarations
  • Consist of the function type, the function name,
    and a pair of parentheses containing the number
    and type of each parameter.
  • int f( int x ) (ANSI Prototype)

40
Identifiers
  • Consist of letters a, A
  • underscores _
  • numbers 1
  • They must not start with a number and must not
    conflict with any keyword.

41
Library Functions
  • printf ( control string, parameters)
  • Does format output. Takes one additional
    parameter for each conversion specification in
    the control string.
  • scanf ( control string, parameters)
  • Does formatted input. Takes one additional
    paramter for each conversion specification in the
    the control string.

42
Operators
  • assignment
  • addition
  • - unary minus and subtraction
  • multiplication
  • / division
  • remainder

43
Operators
  • , -, , compound assignment
  • /,
  • increment by 1
  • -- decrement by 1

44
Preprocessor Directives
  • include
  • includes a source file or header file at that
    point in the code
  • define
  • used to define constants for easy readability and
    maintainability

45
Types
  • int
  • used to represent an integer value
  • void
  • indicates that a function will not return a value
    or that a function does not take parameters.

46
Variable Declarations
  • Consists of the type name followed by a
    comma-separated list of variables and a
    terminating semicolon.
  • int counter
Write a Comment
User Comments (0)
About PowerShow.com