programming languages and paradigms the c programming language

15
Programming Languages and Paradigms The C Programming Language

Upload: efren-creighton

Post on 14-Dec-2015

224 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Programming Languages and Paradigms The C Programming Language

Programming Languagesand Paradigms

The C Programming Language

Page 2: Programming Languages and Paradigms The C Programming Language

6/15/2005Copyright 2005, by the authors of these slides, and Ateneo

de Manila University. All rights reserved. L2: C

Slide 2

Components of a C Program

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

Page 3: Programming Languages and Paradigms The C Programming Language

6/15/2005Copyright 2005, by the authors of these slides, and Ateneo

de Manila University. All rights reserved. L2: C

Slide 3

Built-in Data Types in C

int, char, float Short and long versions of these data

types short, long, double, long double, …

No explicit boolean type in C Control structures that require conditions

expect an int value 0 => false, non-zero => true

Page 4: Programming Languages and Paradigms The C Programming Language

6/15/2005Copyright 2005, by the authors of these slides, and Ateneo

de Manila University. All rights reserved. L2: C

Slide 4

Composite Types Arrays

Collection of elements of the same type Declaration: type varname[integer-value];

Structures Collection of elements of different types or with

different names Declaration: struct strname { field

declarations… }; struct strname varname;

dot operator: varname.field

Page 5: Programming Languages and Paradigms The C Programming Language

6/15/2005Copyright 2005, by the authors of these slides, and Ateneo

de Manila University. All rights reserved. L2: C

Slide 5

Pointers Address operator: &

De-referencing operator: * Pointers and arrays

Array names as addresses Pointer arithmetic Array access []

Dynamic allocation malloc, free, NULL

Multi-dimensional arrays Pointers and structures: the -> operator

Page 6: Programming Languages and Paradigms The C Programming Language

6/15/2005Copyright 2005, by the authors of these slides, and Ateneo

de Manila University. All rights reserved. L2: C

Slide 6

Strings String: sequence of characters

(stored in a character array) The null (‘\0’) terminator String literals ( e.g., “hello” ) String functions (<string.h>)

strcpy strlen strcmp strcat …

Page 7: Programming Languages and Paradigms The C Programming Language

6/15/2005Copyright 2005, by the authors of these slides, and Ateneo

de Manila University. All rights reserved. L2: C

Slide 7

Statements expression-statement

Expression followed by a semicolon expr; where expr consists of variables,

constants, operators, parentheses, function calls

Block or compound-statement 0 or more statements enclosed in { } Declarations may precede statements

{ decl decl decl … stmt stmt stmt }

Page 8: Programming Languages and Paradigms The C Programming Language

6/15/2005Copyright 2005, by the authors of these slides, and Ateneo

de Manila University. All rights reserved. L2: C

Slide 8

Statements (decision) if-statement

Condition bounded by parentheses Numeric expression expected in condition Optional else clause

switch-statement 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

Page 9: Programming Languages and Paradigms The C Programming Language

6/15/2005Copyright 2005, by the authors of these slides, and Ateneo

de Manila University. All rights reserved. L2: C

Slide 9

Statements (loops) while-statement

Loop test at the beginning of the loop Loop body may not be executed at all

do-while-statement Loop test at the end of the loop Loop body executed at least once

for-statement for( expr1; expr2; expr3 ) stmt Has an equivalent while-loop formulation Used if there is an explicit loop control variable

Page 10: Programming Languages and Paradigms The C Programming Language

6/15/2005Copyright 2005, by the authors of these slides, and Ateneo

de Manila University. All rights reserved. L2: C

Slide 10

Statements (others) break;

Breaks out of the nearest enclosing loop or switch continue;

Proceeds to the loop test return-statement

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

Page 11: Programming Languages and Paradigms The C Programming Language

6/15/2005Copyright 2005, by the authors of these slides, and Ateneo

de Manila University. All rights reserved. L2: C

Slide 11

Functions

Function definition:

Return type Name Parameters Body (block)

Page 12: Programming Languages and Paradigms The C Programming Language

6/15/2005Copyright 2005, by the authors of these slides, and Ateneo

de Manila University. All rights reserved. L2: C

Slide 12

Parameter Passing

In C, all parameters are pass by value Pointers and the address operator

enable a function to update data outside of the function But the parameter passed (the address) is

still a value

Page 13: Programming Languages and Paradigms The C Programming Language

6/15/2005Copyright 2005, by the authors of these slides, and Ateneo

de Manila University. All rights reserved. L2: C

Slide 13

Module Organization in C Suppose mod.c is a module containing

functions to be used by other C source files A mod.h file contains function prototypes and

#define directives An #include directive is placed in the file using

the module (i.e., #include “mod.h”). (Do you need to include it in mod.c?)

Modules can be separately compiled and then linked with other modules to produce an executable

Page 14: Programming Languages and Paradigms The C Programming Language

6/15/2005Copyright 2005, by the authors of these slides, and Ateneo

de Manila University. All rights reserved. L2: C

Slide 14

Scope Names in a { … } block are limited to that

block Local variables

Formal parameters of a function are local to the function body/block

Declarations outside the functions are accessible from other functions extern declaration: compiler permits use of

variables before they are defined static definition: variable use restricted to

functions within file

Page 15: Programming Languages and Paradigms The C Programming Language

6/15/2005Copyright 2005, by the authors of these slides, and Ateneo

de Manila University. All rights reserved. L2: C

Slide 15

Library Functions The C standard library I/O, math, string functions and others Prototypes are in:

<stdio.h> <math.h> <string.h> <stdlib.h> …