1 Advanced C Programming from Expert C Programming: Deep C Secrets by Peter van der Linden CIS*2450 Advanced Programming Concepts.

Slides:



Advertisements
Similar presentations
You have been given a mission and a code. Use the code to complete the mission and you will save the world from obliteration…
Advertisements

Chapter 5: Control Structures II (Repetition)
Advanced Piloting Cruise Plot.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2003 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Learning Objectives Structures Structure types Structures.
Chapter 4 Parameters and Overloading. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 4-2 Learning Objectives Parameters Call-by-value Call-by-reference.
Chapter 1 C++ Basics. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 1-2 Learning Objectives Introduction to C++ Origins, Object-Oriented.
Introduction to C Programming
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Title Subtitle.
My Alphabet Book abcdefghijklm nopqrstuvwxyz.
Multiplying binomials You will have 20 seconds to answer each of the following multiplication problems. If you get hung up, go to the next problem when.
DIVIDING INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
Addition Facts
Programming Language Concepts
Storage class in C Topics Automatic variables External variables
1 Storage Duration and Scope –Local and global variables Storage classes –automatic, static, external, register Todays Material.
Solve Multi-step Equations
Chapter 7: Arrays In this chapter, you will learn about
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 5.
ABC Technology Project
Semantic Analysis and Symbol Tables
1 Structures. 2 Structure Basics A structure is a collection of data values, called data members, that form a single unit. Unlike arrays, the data members.
1 Structures. 2 Structure Basics A structure is a collection of data values, called data members, that form a single unit. Unlike arrays, the data members.
Procedures. 2 Procedure Definition A procedure is a mechanism for abstracting a group of related operations into a single operation that can be used repeatedly.
1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010.
Chapter 5 Test Review Sections 5-1 through 5-4.
More Two-Step Equations
Properties of Equality
 2000 Prentice Hall, Inc. All rights reserved. Chapter 14 - Advanced C Topics Outline 14.1Introduction 14.2Redirecting Input/Output on UNIX and DOS Systems.
Addition 1’s to 20.
25 seconds left…...
An introduction to pointers in c
Arithmetic of random variables: adding constants to random variables, multiplying random variables by constants, and adding two random variables together.
Chapter Three Arithmetic Expressions and Assignment Statements
Week 1.
We will resume in: 25 Minutes.
Pointers and Arrays Chapter 12
1 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 1 Operators and Expressions.
Copyright 2013 – Noah Mendelsohn Compiling C Programs Noah Mendelsohn Tufts University Web:
Order of Operations And Real Number Operations
Data Structures Using C++ 2E
Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method.
1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
Intermediate Code Generation
Programming Languages and Paradigms The C Programming Language.
Chapter 7: User-Defined Functions II
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.
Storage & Linkage: Effects on Scope Rudra Dutta CSC Spring 2007, Section 001.
Chapter 4:Functions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 1 Functions Lecture 4 by Jumail Bin.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
Functions Kernighan/Ritchie: Kelley/Pohl: Chapter 4 Chapter 5.
C Functions Three major differences between C and Java functions: –Functions are stand-alone entities, not part of objects they can be defined in a file.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
Department of Electronic & Electrical Engineering Functions Parameters Arguments Pointers/dereference & * Scope Global/Local Storage Static/Automatic.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
1 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
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.
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
Scope Rules and Storage Types
Presentation transcript:

1 Advanced C Programming from Expert C Programming: Deep C Secrets by Peter van der Linden CIS*2450 Advanced Programming Concepts

2 Topics Scope of symbol names –4 flavours Precedence of operators –associativity –syntax of declarations with multiple operators

3 Scope Definition –Region over which you can access a variable by name. There are 4 types of scope: –Program scope… widest –File scope –Function scope –Block scope… narrowest

4 Scoping Principle Always define a symbol in the narrowest scope that works Reasons?

5 Program Scope The variable is active among all source files that make up the executable. –In C, all functions –Global (extern) variables How does it work?

6 External Symbols Program scope symbols passed to linker in.o file –External definition, “extdef” –External reference, “extref” In linked executable, each external symbol: –Exactly 1 extdef, or else… “undefined external” “multiply defined external” –Any number of extrefs substituted with final memory address of symbol

7 “Externals” Having “program scope” (external symbols) is a common requirement –assembly language –all kinds of programming languages –allows big program to be linked together out of small modules Each language has own convention for designating extdef & extref

8 Using Program Scope in C Function –extdef: MARCFILE * useMarcFile (FILE *filep){…} definition only appears in one.c (marcutil.c) –extref: MARCFILE * useMarcFile (FILE *filep); prototype declaration (.h) included in many.c files Variable (don’t have any in A1) –extdef: MARCFILE *libfile; definition only appears in one.c, outside any function can initialize: type varname = initial_value; –extref: extern MARCFILE *libfile; declaration appears anywhere, in/outside functions

9 File Scope A variable is active from its declaration point to the end of the file. –In C, static variables. If variable defined outside any function… –would normally be “program scope” (global) –“static” keyword keeps definition from being passed to linker → doesn’t become external

10 Scope vs. Storage Class “Static” storage –One copy of variable in executable program –Applies to both program scope (global variables) & file scope (“static” variables) Issue confused in C/C++ (real “deep secret” ) –Program scope (globals) are static in nature, but without “static” keyword –If you add “static” keyword, not global anymore!

11 Contrast Automatic Storage Associated with functions –Arguments –Local variables inside function Fresh temporary copy created on stack every time function called –Copy can be initialized (same value each time) –Copy goes away when function returns to caller –Allows recursion to work! “static” keyword changes local variable from automatic to static storage class –Initialization effective once, when program started

12 Function Scope Active from the beginning to the end of a function. –In C, only goto labels have function scope; therefore you will never see them

13 Block Scope The variable is active from its declaration point to the end of the block in which it was declared. –In C, these are local variables. –Includes function’s parameters

14 Example int i; /* Program scope */ static int j; /* File scope */ func ( int k ) { /* func is program scope, k is block scope */ int m; /* Block scope */ …. } /* which are static storage, auto? */

15 What Happens? func() { int a = 11; { int b = 10; } printf ( “%d %d\n”,a,b); } Won’t work! The variable b is inside a block and therefore is not visible to the rest of the function.

16 What Happens? newfunc() { int a = 11; { int b = 10; printf ( “%d\n”,b); } printf ( “%d\n”,a); } WORKS!

17 Precedence of Operators Definition –Determines the order in which operators are evaluated. –Operators are used to calculate values for both numeric and pointer expressions Operators also have an associativity which is used to determine the order in which operands are grouped with operators.

18 Associativity Applies with 2 or more operators of same precedence: A op 1 B op 2 C op 3 D –Answers question: Which op is done first? Associativity can be either Left-to-Right or Right-to-Left

19 Associativity Left-to-Right is most common a + b – c; –The + and – operators are both evaluated left- to-right so the expression is “a plus b, then subtract c” –Equivalent to: (a + b) – c;

20 Associativity Right-to-Left is rare a = b = c; –This expression is read “assign c to b, then to a” –Equivalent to:a = (b = c); –Meaningful because in C assignment operator is an expression with a value

21 Problems with Precedence The precedence of some operators produces problems when they create behaviours which are unexpected.

22 Problems with Precedence Pointer to structure: *p.f –Expectation: the field of what p points to (*p).f –Actually: p.f gives a compile error *(p.f) –Why:. is higher than * –Note: The -> operator was made to correct this. p->f

23 Problems with Precedence int *ap[] –Expectation: ap is a ptr to an array of ints int (*ap)[] –Actually: ap is an array of pointers-to-int int *(ap[]) –Why: [] is higher than * –Note: usually found in declarations.

24 Problems with Precedence int *fp() –Expectation: fp is a ptr to a function returning an int int (*fp)() –Actually: fp is a function returning a ptr-to-int int *(fp()) –Why: () is higher than * –Note: usually found in declarations.

25 Problems with Precedence c = getchar() != EOF –Expectation: ( c = getchar() ) != EOF –Actually: c = (getchar() != EOF) c is set equal to the true/false value –Why: == and != have higher precedence than assignment

26 Solution to Precedence Problems When in doubt… Use parentheses!!! Better still, use parentheses anyway –You may not be in doubt, but the next reader may be