Programming III SPRING 2015 School of Computer and Information Sciences Francisco R. Ortega, Ph.D. McKnight Fellow and GAANN Fellow LECTURE #3 Control Flows and Functions
Programming III Remember to Check – – Any problems with basic compilation? Have you check the resource folders? – Do you know where they are?
Programming III This lecture covers Chapter 3 and 4 from K&R – You can find Chapter 5,6,9,10 (5 + 6, ) in Modern Approach in C with much more detail
Statement and Blocks X = 5; //statement { } // inside of a block or compound statement if (exp) s1; else s2; if (exp) { s1; s2; } else s3; if (exp) s1; else if (exp) s2; else if (exp) s3; else s4;
Wrong! Why?
Example (Binary Search)
For and While Loops while (expression) s1; while (true) ; while (expr) { s1; s2; } for (e1; e2; e3) s1; for ( ; ; ) ; for (e1; e2; e3) { s1; s2; }
Example
Shellsort
Reverse String Comma Operator
DO While
Break and Continue
Goto and Labels
Note about Goto It will help to exit nested loops But you can always write code without goto – I have never needed in C, C++, C#, or Java. I assume that I will not needed in most other languages – My only code that has goto in C++, is a C library for the WiiMote controller that came with it. However, the issue with goto is that makes code look messy. – But there is nothing wrong with having a jump – We do it in Assembly
Another example of Goto
But we can do it without GOTO
Functions and Program Structure Break large computation task into smaller task Enable you (or other people) to reuse it Ideally, you always want to have small functions You may want to break the C files into many, depending on your project – This is true for any other language Compile multiple files : gcc –Wall main.c getline.c strindex.c -o pattern.out
Example -- ould
main
int getline(char s[], int lim)
int strindex(char s[], char t[])
Functions return-type function-name (argument declarations) { declarations; statements; return expression; // if void, just return }
Functions returning non-integer
main calling atof
atof (note) double atof(char []); – says that atof returns a double and takes a character. It is better to used either function declaration – Use extern if needed.
Function declaration #include //already has a declaration extern void myGetLine(); float pow(float x, float y); int main() { … pow(5,2); } float pow(float x, float y) { … }
atoi
extern External variables are global variables Functions are always external If in separate file – use extern keyword
Push and Pop
Use of Extern
Header Files Allows to keep ideas functions together It is easier to reuse.
Static variables It has two uses – In private variables, it allows to keep its value – In external (global) variables It allows to hide variables from other source files – static int value; A note about register value – It is unlikely that modern compilers will use this. But if they do, it is unclear that the developer will gain an advantage – register int x;
Block Structure (scope) if (n > 0) { int x = 10; for (int x = 0; x < n; x++) … }
Block Structure (scope) int x; int y; f (double x) { double y; … }
Initialization external and static variables are initialized to zero – However, I like to be safe, so always initialized. Automatic (function vars) and Register variables – Undefined (garbage) External and Static variables – Initialization value must be constant Automatic and Register variables – Initialization does not have to be constant
Initialization Arrays can be initialized with braces or by individual members of the arrays Characters arrays can use string or {}.
We can also do recursion in C
The C Pre-Processor #include “filename” #include Macro Substitution – #define name replacementText – #define foreverloop for ( ; ; ) – #define max(A,B) ( (A) > (B) ? (A) : (B)) x = max(p+q,r+s) Which it will be replaced by x = (( p+ q) > ….. ) It has pitfalls max(i++,j++) // wrong – #define square(x) x * x square (z + 1)
Avoid macro Functions Avoid macro functions as much as possible It can brings lots of problems – Undesired side effects – Makes it harder for debugging
#define #define has a #undef If you use #param in a definition it will replace it with quotes If use ##, then you can get concatenation
“ “ #param If you use #param in a definition it will replace it with quotes
Conditional Inclusions if !defined(HDR) #define HDR #if #elif #else #endif #ifdef #ifndef
Conditional Inclusion (2)