490 likes | 1.23k Views
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
E N D
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 • C also provides control of hardware a facility not always available with higher level languages
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 • Portability across different systems • Efficiency of C Code • Ease of Maintenance • Control over low-level operations
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 • 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 • Uppercase alphabetic characters • lowercase alphabetic characters • digits • punctuation • formatting • graphic characters • ASCII
Format of C Programs • Free-Format Language • start at a column • no special place • single or multiple lines of code
Whitespace Characters • Separate identifiers or other elements (tokens) • space • line feed • backspace • horizontal tap vertical tap • form feed • carriage return
Token • The compiler divides a C program into groups of characters that belong together • Each group is a token. • ( ) [ ] + -
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 • Function with the name main required • void because no value is returned • void main ( void ) • { • }
Calling a Function • We will have main() call the function does_nothing • void main ( void ) • { • does_nothing () ; • }
Output in C • Void main ( void ) • { • printf ( “C” by Discovery\n” ) ; • }
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
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 • 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 • 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 • +addition and unary plus • - unary minus and subtraction • * multiplication • / division • % remainder • a = (a/b) *b + (a%b)
Precedence of Arithmetic Operations • High Precedence • Unary Operators - + • Multiplicative Operators * / % • Additive Operators + - • Assignment Operator =
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 • 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 • 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) • 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 • 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 • 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 • 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 • 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 • Comments • Control Statements • Conversion Specifications • Escape Characters • Function Definitions • Function Calls • Function Declarations
Language Elements Introduced • Identifiers • Library Functions • Operators • Preprocessor Directives • Types • Variable Declarations
Comments • /* This is a comment. */
Control Statements • return • Used to impart value to a function and to return control from a function to the calling environment.
Conversion Specifications • %d conversion to or from decimal • %x conversion to or from hexadecimal • %o conversion to or from octal
Escape Character • ‘\’ • escapes the usual meaning of the next character
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
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 Calls • Consists of the function name, • a pair of parentheses • and a semicolon. • Any actual parameters appear between the parentheses.
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 • Consist of letters a, A • underscores _ • numbers 1 • They must not start with a number and must not conflict with any keyword.
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 • = assignment • + addition • - unary minus and subtraction • * multiplication • / division • % remainder
Operators • +=, -=, *=, compound assignment • /=, %= • ++ increment by 1 • -- decrement by 1
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 • 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 • Consists of the type name followed by a comma-separated list of variables and a terminating semicolon. • int counter;