Programming Languages and Paradigms The C Programming Language
- Slides: 13
Programming Languages and Paradigms The C Programming Language 6/15/2005
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 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 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 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 (‘ ’) 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 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 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 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 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, 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 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 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
- R programming language paradigms
- Binding in programming paradigms
- Ktu programming paradigms notes
- Real-time systems and programming languages
- Cs 421 uiuc
- Advantages of high level language
- Real time programming language
- Cs 421 uiuc
- Multithreading program in java
- Cxc it
- Introduction to programming languages
- Plc programming languages
- Joey paquet
- Imperative programming languages