1 CS 201 Exam 1 Review More on Makefiles Debzani Deb.

Slides:



Advertisements
Similar presentations
C Language.
Advertisements

Computer Programming w/ Eng. Applications
1 CS 201 Pointers (2) Debzani Deb. 2 Overview Pointers Functions: pass by reference Quiz 2 : Review Q & A.
Kernighan/Ritchie: Kelley/Pohl:
Introduction: C Pointers Day 2 These Slides NOT From Text.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Announcements The first graded lab will be posted Sunday 2/15 and due Friday 2/27 at midnight It is brand new, so please don’t hand in last semester’s.
1 CS 201 Repetition Debzani Deb. 2 Overview Loops  Endfile-Controlled loop  Nested loop  Do-While loop  Flag-Controlled loop Hand Tracing the code.
Modular Programming From Chapter 6. Output Parameters Pass by reference. In the calling program: int frog, lily; int * frog_ptr=&frog; function_name(frog_ptr,
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
CS 201 Functions Debzani Deb.
CS100A, Fall 1997, Lectures 221 CS100A, Fall 1997 Lecture 22, Tuesday 18 November Introduction To C Goal: Acquire a reading knowledge of basic C. Concepts:
Guide To UNIX Using Linux Third Edition
CS201 – C Functions Procedural Abstraction Code Reuse C Library.
12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
Computer Science 210 Computer Organization Introduction to C.
CS140: Intro to CS An Overview of Programming in C by Erin Chambers.
C programming: Variables, Expressions part II. Data Types of Arithmetic Expressions Relational Expressions Logical Expressions Multiple Assignments Compound.
CECS 121 EXAM 1. /* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
16&17-2 Grasp the concept of top-down programming Identify Function Headers and Prototypes Understand when and where prototypes used Understand how arguments.
Introduction to C Programming CE Lecture 7 Compiler options and makefiles.
C Tutorial Session #2 Type conversions More on looping Common errors Control statements Pointers and Arrays C Pre-processor Makefile Debugging.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
IPC144 - LOOPS while - do while - for.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
C Hints and Tips The preprocessor and other fun toys.
C Tutorial - Program Organization CS Introduction to Operating Systems.
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
1 C Programming Week 2 Variables, flow control and the Debugger.
CS Class 05 Topics  Selection: switch statement Announcements  Read pages 74-83, ,
Chapter 5: Structured Programming
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
Pointers *, &, array similarities, functions, sizeof.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
1 ICS103 Programming in C Lecture 8: Functions I.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Introduction Chapter 1 1/22/16. Check zyBooks Completion Click on the boxes for each section.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CSE 1320 Basics Dr. Sajib Datta
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Functions Dr. Sajib Datta Functions A function is a self-contained unit of program code designed to accomplish a particular task. Some functions.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Windows Programming Lecture 03. Pointers and Arrays.
User-Written Functions
Computer Science 210 Computer Organization
Lesson #6 Modular Programming and Functions.
A bit of C programming Lecture 3 Uli Raich.
Lesson #6 Modular Programming and Functions.
Functions and Structured Programming
Functions Dr. Sajib Datta
Functions Separate Compilation
CS1010 Discussion Group 11 Week 7 – Two dimensional arrays.
Computer Science 210 Computer Organization
Lesson #6 Modular Programming and Functions.
Looping.
C Preprocessor(CPP).
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Introduction to C Topics Compilation Using the gcc Compiler
Lesson #6 Modular Programming and Functions.
Introduction to C Topics Compilation Using the gcc Compiler
Variables in C Topics Naming Variables Declaring Variables
Presentation transcript:

1 CS 201 Exam 1 Review More on Makefiles Debzani Deb

2 Announcements MSU Book Store: The official last day to buy books is March 20, ACM Meeting: This Thursday at 5pm in the CS conference room.

3 Exam 1 review of critical Qs

4 Answer to Q: 1(c) #include double Root_product(double x, double y); int main(void) { double first, second; double result; printf("Please enter two positive real numbers\n"); scanf("%lf %lf", &first, &second); result = Root_product(first, second); printf("The square root of their product is %f\n", result); return 0; } double Root_product (double x, double y) { double result; result = sqrt(x*y); return result; }

5 Question 2 Q2(c): Challenging  If you execute the code int i = 7; printf("%d\n", i++ * i++);  prints 49. Shouldn't it print 56?  The behavior of code which contains side effects like this has always been undefined. So avoid them.  You can learn more about this in

6 Question 3 Q 3(a): What is the output if the input is 3? scanf("%d", &n); if (n = 5) printf("Equal\n"); else if (n < 5) printf("Less\n"); else printf("Greater\n"); Condition (n=5) is always true. See Lecture 7 for more on this.

7 Question 4 (a): Tricky What is printed out by the following C code fragment? int i; for (i = 1; i <= 100; i++) if ((i % 3) && (i % 5)) printf("%d\n", i); i.All integers between 1 to 100 inclusive. ii.All integers between 1 to 100 inclusive that are divisible by 3 or 5. iii.All integers between 1 to 100 inclusive that are divisible by 15. iv.All integers between 1 to 100 inclusive that are not divisible by 3 or 5. v.All integers between 1 to 100 inclusive that are not divisible by 15. The if condition becomes false when  input i is divisible by 3, 5 or both. If you are not sure about the output, then try hand tracing for few inputs and apply process of elimination.

8 Question 4 (b): More Tricky What is printed out by the following C code fragment? int i; for (i = 1; i <= 100; i++) if (!((i % 3) || (i % 5))) printf("%d\n", i); i.All integers between 1 to 100 inclusive. ii.All integers between 1 to 100 inclusive that are divisible by 3 or 5. iii.All integers between 1 to 100 inclusive that are divisible by 15. iv.All integers between 1 to 100 inclusive that are not divisible by 3 or 5. v.All integers between 1 to 100 inclusive that are not divisible by 15. !((i % 3) || (i % 5)) = !(i%3) && !(i%5) So if becomes true when input i is divisible by both 3 and 5.

9 Question 4(c) st = scanf("%d%d%d", &n, &m, &p); printf("n = %d m = %d st = %d\n", n, m, st); Input data: hello Answer: n = 10 m = 12 st = 2 Lecture 8 and p. 242 of HK If scanf faces invalid/insufficient data, the function returns as its value the number of successful scans before encountering the problem.

10 Question 4(d) Write a do-while loop that repeatedly scans integer values until a positive even number is input. We want the while (condition) to be false when user enters a positive even number. while (negative || odd), the loop should continue. Answer: do scanf("%d", &num); while ((num <= 0) || (num % 2 != 0));

11 Question 4(e): Tricky What is printed out by the following C code fragment int a = 1.5; if (a == 1.5) printf("1\n"); else printf("2\n"); i.1 ii.2 iii.System dependent. iv.A compilation error occurs. v.A run-time error occurs.

12 Question 5(a) Complete function incr so its effect mimics the prefix increment of an integer variable: that is, both ++i and incr(&i) would add 1 to i. int incr(int *argp) { ++(*argp); return(*argp); }

13 Question 5(c) What's wrong with the following code? int *p = 20; This is actually declaring a pointer variable p and initializing it with memory address 20 at the same time. The point of this question was to test whether you remember the usage of operator * in pointer declaration and in regular use such as *p = 20. If you execute this code like this, there is a warning: Initialization makes pointer from integer without a cast. If you do proper casting such as int *p = (int *) 20; the address of p becomes 0x14(20 in decimal).

14 Question 5(d)-(g) void main() { char bird[10]; /* 0810 */ char c1,c2; int i; char *ptr1; i=2; strcpy(bird, “robin”); ptr1=&bird[1]; ptr1=ptr1+i; c1=*ptr1; c2=bird[0]; } What is the memory location of c1? What is the value of ptr1 after the program has run? What is the value of c1 after the program has run? What is the value of c2 after the program has run? bird[0]= r bird[1]= o bird[2]= b bird[3]= i bird[4]= n bird[5] bird[6] bird[7] bird[8] bird[9] c1 ptr

15 Question 5(h) Calling program: c = funct(&a, &b); In the called program int funct(int *fa, int *fb) { scanf(“…”, fa, fb); } Since formal parameters fa and fb are addresses already, if you pass it on to a scanf, you don’t need the & operator.

16 Question 5(i) We have seen that all pointer variables (i.e. int *, double *) are of same size (in our case all of them occupy 4 bytes of memory and contain plain and simple addresses). Considering this, can we live without declaring the type of a pointer variable? Whether your answer is ‘yes’/’no’, please explain. My Answer: No What will happen in case of pointer arithmetic operations such as p++ or p+2 ? If we do not know the type p is pointing to, how do we know which memory location to go to?

17 Makefiles

18 #include “proto.h” I like to put all my prototypes in a separate file and then include this file in the main program. Why? It makes my modules more convenient for re- using. When you do it this way, you need some additional pre-processor directives to avoid duplication.

19 Sample proto.h /* proto.h – Function Prototypes */ #ifndef _PROTO_H #define _PROTO_H int fun1(int,int,int); double fun2(double,int); float funn(int); #endif

20 Avoids Duplication First it checks to see if the variable _PROTO_H is defined. If it IS defined, the entire file is skipped. If it ISN’T defined, the first thing done is to define it. Then the rest of the file is processed. If this file is included twice, the compiler will only see one copy of it!

21 Why did I use a strange variable name? _PROTO_H is used so that it never conflicts with any normal variables in any programs. I usually use the file name since that is pretty unique. You’ll see this done in all sorts of ways.

22 Using Multiple Source Files proto.h prog.cfun2.cfun1.cfunn.c prog.ofun1.ofun2.ofunn.c gcc -c library prog gcc -o

23 Sample Makefile # Makefile for multiple file example prog: prog.o fun1.o fun2.o funn.o gcc -o prog prog.o fun1.o fun2.o funn.o prog.o: prog.c proto.h gcc -c prog.c fun1.o: fun1.c gcc -c fun1.c fun2.o: fun2.c proto.h gcc -c fun2.c fun3.o: fun3.c gcc -c fun3.c clean: rm *.o prog

24 What does it do? make is a program that looks for a file called “Makefile”. Makefile contains variables, dependency rules and comments. When a Makefile is executed, a specific target is executed. A target is just some name that appears at the beginning of a dependency rule.

25 Dependency rules (1) Dependencies are the heart of makefiles; without them nothing would work. They have the following form: dependency1: depA, depB …..depN command for dependency1 dependency1 is dependent upon depA, and depB, upto however many dependencies you have. The program make, looks at dependency1 and sees it is dependent upon depA, so then it looks later in the makefile for how depA is defined.

26 Dependency rules (2) A rule does not have to be used in every invocation of make. Example: the “clean” rule is not normally used. A rule does not need to have any dependencies. This means if we tell make to handle its target, it will always execute its command list, as in the “clean” rule below. A rule does not need to have any commands. For example, the “all” rule is just used to invoke other rules, but does not need any commands of its own. # top-level rule to create program all: prog … # Cleaning everything that could be automatically # recreated by “make” clean: rm –f prog *.o

27 Compiler Options Depend on the compiler, but many similar ones. gcc {…. Options….} file_names; You have seen  -c : compile only  -o : define executable file name. Here are few others  -g : inserts debugging statements  -Wall : Print all warnings. Allows to write cleaner executing code.  -E : preprocess only and produce the results of all preprocessor directives. If you have problem linking your math library: gcc –lm –o file_names;

28 Have a nice Spring Break!!!