Structured programming
Advertisement
This presentation is the property of its rightful owner.
1 / 46

STRUCTURED PROGRAMMING PowerPoint PPT Presentation

STRUCTURED PROGRAMMING. Introduction to C. The History of C Language. Developed by Dennis Ritchie at Bell Laboratories 1972 Developed on UNIX Operating System. LANGUAGE LEVEL. C is an intermediate to high-level language High level language - English like Low level - machine language

Download Presentation

STRUCTURED PROGRAMMING

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Structured programming

STRUCTURED PROGRAMMING

Introduction to C


The history of c language

The History of C Language

  • Developed by Dennis Ritchie

  • at Bell Laboratories 1972

  • Developed on UNIX Operating System


Language level

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


Programming environment

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


Strengths of c

Strengths of C

  • Portability across different systems

  • Efficiency of C Code

  • Ease of Maintenance

  • Control over low-level operations


Weakness of c

Weakness of C

  • Syntax is somewhat irregular

  • Operators have context sensitive meanings

  • Allows programmer to bypass the type system, which can be misused.


Identifiers or names in c

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


The character set

The Character Set

  • Uppercase alphabetic characters

  • lowercase alphabetic characters

  • digits

  • punctuation

  • formatting

  • graphic characters

  • ASCII


Format of c programs

Format of C Programs

  • Free-Format Language

  • start at a column

  • no special place

  • single or multiple lines of code


Whitespace characters

Whitespace Characters

  • Separate identifiers or other elements (tokens)

  • space

  • line feed

  • backspace

  • horizontal tap vertical tap

  • form feed

  • carriage return


Token

Token

  • The compiler divides a C program into groups of characters that belong together

  • Each group is a token.

  • ( ) [ ] + -


Subprograms or functions

Subprograms or Functions

  • A C program is an collection of subprograms called functions.

  • Function heading/parameters/fct block

  • type function_name

  • {

  • variable declarations

  • code

  • }


Shortest c program

Shortest C Program

  • Function with the name main required

  • void because no value is returned

  • void main ( void )

  • {

  • }


Calling a function

Calling a Function

  • We will have main() call the function does_nothing

  • void main ( void )

  • {

  • does_nothing () ;

  • }


Output in c

Output in C

  • Void main ( void )

  • {

  • printf ( “C” by Discovery\n” ) ;

  • }


Output special meaning

Output-Special Meaning

  • \tthe tab character

  • \b the backspace character

  • \” the double quote character

  • \’ the single quote character

  • \\ the backslash character

  • \0 the null character


The structured approach

The Structured Approach

  • main ( ) is the driver of the program

  • Each subfunction must be:

  • 1. Be defined

  • 2. Be declared and

  • 3. Be called


Input and output with variables

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


Input with scanf keyboard

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 ()


Arithmetic operations

Arithmetic Operations

  • +addition and unary plus

  • - unary minus and subtraction

  • * multiplication

  • / division

  • % remainder

  • a = (a/b) *b + (a%b)


Precedence of arithmetic operations

Precedence of Arithmetic Operations

  • High Precedence

  • Unary Operators - +

  • Multiplicative Operators* / %

  • Additive Operators + -

  • Assignment Operator =


Compound assignment

Compound Assignment

  • The first syntax shortcut is compound assignment

  • The assignment statement

  • a = a + 4

  • can be shortened to

  • a += 4


Increment and decrement by 1 operations

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++;


Introduction to functions and structured programming

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.


Functions subprograms

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


Prepocessor constants

Prepocessor Constants

  • Defining constants add to program’s readability and ease of modification.

  • Preprocessor directives:

  • start with #

  • #include #include <stdio.h>

  • reads external file into source file here

  • #define #define MAXEMPLOYEES 150


Function parameters

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.


Ansi c function definition

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.


Function calls

Function Calls

  • A function call in C consists of the function’s 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


Language elements introduced

Language Elements Introduced

  • Comments

  • Control Statements

  • Conversion Specifications

  • Escape Characters

  • Function Definitions

  • Function Calls

  • Function Declarations


Language elements introduced1

Language Elements Introduced

  • Identifiers

  • Library Functions

  • Operators

  • Preprocessor Directives

  • Types

  • Variable Declarations


Comments

Comments

  • /* This is a comment. */


Control statements

Control Statements

  • return

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


Conversion specifications

Conversion Specifications

  • %d conversion to or from decimal

  • %x conversion to or from hexadecimal

  • %o conversion to or from octal


Escape character

Escape Character

  • ‘\’

  • escapes the usual meaning of the next character


Escape characters examples

Escape CharactersExamples

  • \tthe tab character

  • \ca carriage return

  • \b the backspace character

  • \”the double quote character string

  • \’the single quote character

  • \\the backslash character

  • \0the null character

  • \nthe newline character


Function definitions

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.


Function calls1

Function Calls

  • Consists of the function name,

  • a pair of parentheses

  • and a semicolon.

  • Any actual parameters appear between the parentheses.


Function declarations

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)


Identifiers

Identifiers

  • Consist of letters a, A

  • underscores _

  • numbers 1

  • They must not start with a number and must not conflict with any keyword.


Library functions

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.


Operators

Operators

  • =assignment

  • +addition

  • -unary minus and subtraction

  • *multiplication

  • /division

  • %remainder


Operators1

Operators

  • +=, -=, *=,compound assignment

  • /=, %=

  • ++increment by 1

  • --decrement by 1


Preprocessor directives

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


Types

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.


Variable declarations

Variable Declarations

  • Consists of the type name followed by a comma-separated list of variables and a terminating semicolon.

  • int counter;


  • Login