Programming Paradigms The C Programming Language 6152005 Components

  • Slides: 13
Download presentation
Programming Paradigms The C Programming Language 6/15/2005

Programming Paradigms The C Programming Language 6/15/2005

Components of a C Program n n n A C program is a collection

Components of a C Program n n n A C program is a collection of function definitions, (global) variable definitions, declarations and compiler directives Functions ( e. g. , void push( char c ) { … } ) Variables ( e. g. , int top; char Store[MAX]; ) Declarations ( e. g. , void push( char c ); ) Compiler directives ( e. g. , #define MAX 100 #include <stdio. h> ) 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 2

Built-in Data Types in C n n int, char, float Short and long versions

Built-in Data Types in C n n int, char, float Short and long versions of these data types n n short, long, double, long double, … No explicit boolean type in C n n 6/15/2005 Control structures that require conditions expect an int value 0 => false, non-zero => true Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 3

Composite Types n Arrays n n n Collection of elements of the same type

Composite Types n Arrays n n n Collection of elements of the same type Declaration: type varname[integer-value]; Structures n n n 6/15/2005 Collection of elements of different types or with different names Declaration: struct strname { field declarations… }; struct strname varname; dot operator: varname. field Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 4

Pointers n n Address operator: & De-referencing operator: * Pointers and arrays n n

Pointers n n Address operator: & De-referencing operator: * Pointers and arrays n n Dynamic allocation n Array names as addresses Pointer arithmetic Array access [] malloc, free, NULL Multi-dimensional arrays Pointers and structures: the -> operator 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 5

Strings n n String: sequence of characters (stored in a character array) The null

Strings n n String: sequence of characters (stored in a character array) The null (‘’) terminator String literals ( e. g. , “hello” ) String functions (<string. h>) n n n 6/15/2005 strcpy strlen strcmp strcat … Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 6

Statements n expression-statement n n n Expression followed by a semicolon expr; where expr

Statements n expression-statement n n n Expression followed by a semicolon expr; where expr consists of variables, constants, operators, parentheses, function calls Block or compound-statement n n 6/15/2005 0 or more statements enclosed in { } Declarations may precede statements { decl … stmt } Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 7

Statements (decision) n if-statement n n Condition bounded by parentheses Numeric expression expected in

Statements (decision) n if-statement n n Condition bounded by parentheses Numeric expression expected in condition Optional else clause switch-statement n n n 6/15/2005 Can be viewed as a special kind of block Has entry points (through the case labels) and exit points (through break; statements ) switch expression should be of ordinal type Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 8

Statements (loops) n while-statement n n n do-while-statement n n n Loop test at

Statements (loops) n while-statement n n n do-while-statement n n n Loop test at the beginning of the loop Loop body may not be executed at all Loop test at the end of the loop Loop body executed at least once for-statement n n n 6/15/2005 for( expr 1; expr 2; expr 3 ) stmt Has an equivalent while-loop formulation Used if there is an explicit loop control variable Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 9

Statements (others) n break; n n continue; n n Breaks out of the nearest

Statements (others) n break; n n continue; n n Breaks out of the nearest enclosing loop or switch Proceeds to the loop test return-statement n n n 6/15/2005 return expr; Provides value return by a function Without expr (if return type is void), the statement simply causes control to be returned to the caller Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 10

Functions Function definition: n n Return type Name Parameters Body (block) 6/15/2005 Copyright 2005,

Functions Function definition: n n Return type Name Parameters Body (block) 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 11

Parameter Passing n n In C, all parameters are pass by value Pointers and

Parameter Passing n n In C, all parameters are pass by value Pointers and the address operator enable a function to update data outside of the function n 6/15/2005 But the parameter passed (the address) is still a value Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 12

Library Functions n n n The C standard library I/O, math, string functions and

Library Functions n n n The C standard library I/O, math, string functions and others Prototypes are in: n n n 6/15/2005 <stdio. h> <math. h> <string. h> <stdlib. h> … Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 13