Week 2-3 Control flow (review) Conditional statements If, else, else if, switch-case, break Loop constructs for, while, do-while, break, continue, label--go;

Slides:



Advertisements
Similar presentations
Programming III SPRING 2015 School of Computer and Information Sciences Francisco R. Ortega, Ph.D. McKnight Fellow and GAANN Fellow LECTURE #3 Control.
Advertisements

The make Utility Programming Tools and Environments Winter 2006.
Make. A simple make file $ make program Strength of make is its sensitivity to dependency hierarchies. Specify such dependencies in a description file.
Separate compilation Large programs are generally separated into multiple files, e.g. tuples.h, ray.h, ray.c, tuples.c main.c With several files, we can.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Makefiles  Provide a way for separate compilation.  Describe the dependencies among the project files.  The make utility.
The Makefile utility ABC – Chapter 11, Motivation Small programs single file “Not so small” programs : –Many lines of code –Multiple components.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation Namespaces Simple Make Files (Ignore all class references.
The Makefile Utility ABC – Chapter 11, Motivation Small programs single file “Not so small” programs : –Many lines of code –Multiple components.
1 CSSE 332 Standard Library, Storage classes, and Make.
Functions and Program Structure Chapter 4. Introduction Functions break large computing tasks into smaller ones Appropriate functions hide details of.
More about the C Preprocessor CS-2303, C-Term More about the C Preprocessor CS-2303 System Programming Concepts (Slides include materials from The.
Guide To UNIX Using Linux Third Edition
Inline Function. 2 Expanded in a line when it is invoked Ie compiler replace the function call with function code To make a function inline the function.
Lecture 8  make. Overview: Development process  Creation of source files (.c,.h,.cpp)  Compilation (e.g. *.c  *.o) and linking  Running and testing.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
Chapter 4 Functions and Program Structure Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering.
July 29, 2003Serguei Mokhov, 1 Makefile Brief Reference COMP 229, 346, 444, 5201 Revision 1.2 Date: July 18, 2004.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
Macros. There are three basic phases for C programming. preprocessing, compiling, and linking. C input file is first passed to a preprocessing program.
Introduction to C Programming CE Lecture 7 Compiler options and makefiles.
Chapter 6: User-Defined Functions
1 Homework / Exam Finish up K&R Chapters 3 & 4 Starting K&R Chapter 5 Next Class HW4 due next class Go over HW3 solutions.
Chapter 13 Programming in the Large Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. TA: 鄭筱親 陳昱豪.
Homework K&R chapter 4. HW3: ASCII Hex to Integer int axtoi (char s[ ]) { int i, n, flag; n = 0; flag = 1; for ( i = 0; flag; i++) /* for (i = 0; ; i++)
Chapter Ten g++ and make1 System Programming Software Development: g++ and make.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
Functions CIS Feb-06. Summary Slide Using Functions Mathematical Functions Misc. Functions Naming Conventions Writing Functions –Function Prototype.
Makefile M.A Doman. Compiling multiple objects Card.cpp -> Card.o Deck.cpp -> Deck.o main.cpp -> main.o main.o Deck.o Card.o -> Dealer.exe.
Engineering Computing I Chapter 4 Functions and Program Structure.
L function n predefined, programmer-defined l arguments, (formal) parameters l return value l function call, function invocation l function definition.
0 4.1 Basics of Functions + Execution Diagram + make 4.2 Functions Returning Non-integers + Prototype Function 4.3 External Variables 4.4 Scope Rules 4.5.
Makefiles. Multiple Source Files (1) u Obviously, large programs are not going to be contained within single files. u C provides several techniques to.
Separate Compilation make and makefiles
Exam / Homework Exam 1 Starting K&R chapter 4 tonight
(language, compilation and debugging) David 09/16/2011.
FUNCTIONS. Funtions  The heart of effective problem solving is problem decomposition.  breaking a problem into small, manageable pieces  In C, the.
C Programming Day 3. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Storage Class Specifiers Every variable in a C.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
Discussion Week 1 TA: Kyle Dewey. Project 0 Walkthrough.
Makefiles CARYL RAHN. Separate compilation Large programs are generally separated into multiple files, e.g. main.c addmoney.c removemoney.c money.h With.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Lecture 8  make. Using make for compilation  With medium to large software projects containing many files, it’s difficult to: Type commands to compile.
Functions. Flow of Control Review while for do while goto break & continue switch Relational operators short circuits.
Program in Multiple Files. l all C++ statements are divided into executable and non-executable l executable - some corresponding machine code is generated.
Emacs, Compilation, and Makefile C151 Multi-User Operating Systems.
C code organization CSE 2451 Rong Shi. Topics C code organization Linking Header files Makefiles.
Statements / Blocks, Section 3.1 An expression becomes a statement when it is followed by a semicolon x = 0; Braces are used to group declarations and.
Brandon Packard. Why make? So far, you have probably worked on relatively small projects Coding projects can become huge My research consists of 1600.
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
UNIX Development: g++ and make CS 2204 Class meeting 8 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Makefiles Manolis Koubarakis Data Structures and Programming Techniques 1.
Week 3-4 Control flow (review) Function definition Program Structures
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Compilation and Debugging
Compilation and Debugging
Makefiles Caryl Rahn.
Functions.
Register Variables Declaring a variable as a "register" variable is an advisory to the compiler to keep the normal location of the variable in a register,
Functions and Modular Programming
Functions, Part 1 of 3 Topics Using Predefined Functions
Homework Finish up K&R Chapters 3 & 4
CS1100 Computational Engineering
Homework K&R chapter 4.
Compiler vs linker The compiler translates one .c file into a .o file
Functions, Part 1 of 3 Topics Using Predefined Functions
CPS125.
Functions.
Presentation transcript:

Week 2-3 Control flow (review) Conditional statements If, else, else if, switch-case, break Loop constructs for, while, do-while, break, continue, label--go; Function definition Return type, parameter definition, parameter passing Function calls and standard library Program Structures Preprocessing Header files, Application Programming Interfaces (API) Scope rules C library

Conditional statements If, else, else if, state.edu/~caod/cse45921/coding/ifelse_binarysearch.c state.edu/~caod/cse45921/coding/ifelse_binarysearch.c switch-case, break

Control flow Loop constructs for while do-while Other flow control methods break continue label—go Sample programs calculate the sum of 1-10 using different loops state.edu/~caod/cse45921/coding/Sample_sum.c state.edu/~caod/cse45921/coding/Sample_sum.c

Function Function prototype return type argument definition Function calls basic syntax parameter passing Example: find the next prime number

Function Organization A set of functions Defined in the same files as main() Add two more interfaces: my_isuppercase() and my_islowercase() Sample program Use separated file Header files: User-defined: “…”, System: How to modify the above sample multi_check.h check.c multi.c main.c Separated compilation

Recursion A function can call itself Directly Indirectly examples: Calculate the power of x to y Find a matching number in an increasing array of integers int Power(int x, int y) { int pow=1; int i=0; for (i=0;i<y;y++) { pow=pow*x; return pow; } int Power(int x, int y) { if(y==0) return 1; else return x*Power(x,y-1); }

Scope Rules Scope: Where the variable or function definitions can be accessed Local and global variables Local: a variable defined inside a function Global: a variable defined outside of functions external variable Note: global variable and external variables Typically used when a global variable is used in another file

Scope Rules -- continued static variable Value are retained on exit from function Limited scope Only accessible within the same file Or other source files that ‘include’s this file Automatic variables: Typically local variables variables that are automatically created/destroyed Register variable: variables are saved in register for fast access

//multi.c #include “multi_check.h” extern int z; int multiplication(int x, int y) { static int cnt=0; cnt=cnt+1; printf(“%dth invocation”, cnt); z=x*y; return z; } //check.c extern int z; #include “multi_check.h” int check(int x) { if(x>0) return 1; else return 0; } //main.c int z=0; #include #include “multi_check.h” int main() { int x, y; printf(“please input two postive integers); scanf(“%d,%d”,&x,&y); if(check(x)&&check(y)){ z=multiplication(x,y); // 1th invocation z=multiplication(x,y); //2th invocation printf(“%d”,z); } else{ printf(“negative inputs!”); } return 0; } Scope Rules -- Example

Function scope All functions are defined externally Static function, limited scope Inline functions The complier will perform inline expansion: inserting the body of function every place it is invoked rather than generating code to call the function in the one place it is defined. Eliminate the time overhead when a function is called. Typically used for functions that execute frequently.

Preprocessing Preprocessing directives file inclusion Allow a program to use certain interfaces #include code replacement #define Conditional compilation One source for many platforms #ifdef -- #endif; #if expr -- #endif Conditional inclusion #ifdef #include #endif #if SYSTEM == SYSV #define HDR "sysv.h" #elif SYSTEM == BSD #define HDR "bsd.h" #elif SYSTEM == MSDOS #define HDR "msdos.h" #else #define HDR "default.h" #endif #include HDR gcc –DSYSV test.c

Types of lines in Makefiles Dependency or rules lines Commands Macro assignments Comments

Dependency/rules lines Specify a target and a list of prerequisites (optional) for that target target : prereq1 prereq2 prereq3 …

Command lines Follow dependency lines MUST start with a TAB! Any command that can be run in the shell can be placed here target : prereq1 prereq2 command1 command2 Special variables in commands: represents the target $? represents prereqs that are newer than target

Simplified Makefile example program : main.o iodat.o dorun.o g++ -o main.o iodat.o dorun.o main.o : main.cc g++ -c $? iodat.o : iodat.cc g++ -c $? dorun.o : $? g++ -c dorun.cc

Macro (variable) assignments You can use macros to represent text in a Makefile Saves typing Allows you to easily change the action of the Makefile Assignment: MACRONAME = macro value Usage: ${MACRONAME}

Simplified Example Makefile with macros OBJS = main.o iodat.o dorun.o CC = /usr/bin/g++ program : ${OBJS} ${CC} -o ${OBJS} main.o : main.cpp ${CC} -c $? iodat.o : iodat.cpp ${CC} -c $? dorun.o : dorun.cpp ${CC} -c $?

Comments and other Makefile notes Comments begin with a ‘#’ Lines that are too long can be continued on the next line by placing a ‘\’ at the end of the first line

Invoking make Be sure that your description file: is called makefile or Makefile is in the directory with the source files (to simplify) make (builds the first target in the file) make target(s) (builds target(s)) Important options: -n : don’t run the commands, just list them -f file : use file instead of [Mm]akefile

Other useful Makefile tips Include a way to clean up your mess: %make clean No dependencies! clean: /bin/rm -f *.o core

Makefile example b : c d rm echo $? > a : c rm echo $? > target : a b rm echo $? > cat $? >> all : a b c d target start : rm `ls | egrep –v ‘\ ’` ; echo D > d ; echo C > c echo B > b ; echo A > a ; echo Target > target