Control flow and string example. C and C++ Functions. Function type-system nasties. 2. Functions Preprocessor. Alastair R. Beresford.

Similar documents
C and C++ 2. Functions Preprocessor. Alan Mycroft

Structure of this course. C and C++ Past Exam Questions. Text books

Appendix A. The Preprocessor

fpp: Fortran preprocessor March 9, 2009

(heavily based on last year s notes (Andrew Moore) with thanks to Alastair R. Beresford. 1. Types Variables Expressions & Statements 2/23

Have examined process Creating program Have developed program Written in C Source code

CS240: Programming in C

Programming in C and C++

OBJECT ORIENTED PROGRAMMING USING C++

Programming in C and C++

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

C Programming. The C Preprocessor and Some Advanced Topics. Learn More about #define. Define a macro name Create function-like macros.

COMP322 - Introduction to C++

Appendix. Grammar. A.1 Introduction. A.2 Keywords. There is no worse danger for a teacher than to teach words instead of things.

EDIABAS BEST/2 LANGUAGE DESCRIPTION. VERSION 6b. Electronic Diagnostic Basic System EDIABAS - BEST/2 LANGUAGE DESCRIPTION

Unit 4 Preprocessor Directives

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

G52CPP C++ Programming Lecture 6. Dr Jason Atkin

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

EL6483: Brief Overview of C Programming Language

Programming for Engineers C Preprocessor

A Fast Review of C Essentials Part II

SISTEMI EMBEDDED. The C Pre-processor Fixed-size integer types Bit Manipulation. Federico Baronti Last version:

COSC 2P91. Bringing it all together... Week 4b. Brock University. Brock University (Week 4b) Bringing it all together... 1 / 22

Slide Set 5. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

Problem Solving and 'C' Programming

3 PREPROCESSOR. Overview. Listing 3-0. Table 3-0.

Topic 6: A Quick Intro To C

Chapter 7: Preprocessing Directives

Conditional Compilation

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

Full file at C How to Program, 6/e Multiple Choice Test Bank

SISTEMI EMBEDDED. The C Pre-processor Fixed-size integer types Bit Manipulation. Federico Baronti Last version:

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

INDEX. Figure I-0. Listing I-0. Table I-0. Symbols.DIRECTIVE (see Assembler directives)? preprocessor operator 3-34

Preface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS

SISTEMI EMBEDDED. The C Pre-processor Fixed-size integer types Bit Manipulation. Federico Baronti Last version:

Macros and Preprocessor. CGS 3460, Lecture 39 Apr 17, 2006 Hen-I Yang

CE221 Programming in C++ Part 1 Introduction

Absolute C++ Walter Savitch

Rule 1-3: Use white space to break a function into paragraphs. Rule 1-5: Avoid very long statements. Use multiple shorter statements instead.

C: Program Structure. Department of Computer Science College of Engineering Boise State University. September 11, /13

Compiler Theory. (GCC the GNU Compiler Collection) Sandro Spina 2009

Practical C Issues:! Preprocessor Directives, Multi-file Development, Makefiles. CS449 Fall 2017

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

C and C++ 1. Types Variables Expressions & Statements. Andrew W. Moore

C and C++ 1. Types Variables Expressions & Statements. Andrew W. Moore

Programming for Engineers Introduction to C

Short Notes of CS201

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

C: How to Program. Week /Mar/05

CS201 - Introduction to Programming Glossary By

Basic C Programming. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Chapter 2, Part I Introduction to C Programming

Errors During Compilation and Execution Background Information

CSCI 2132 Software Development. Lecture 8: Introduction to C

CSE 333 Autumn 2013 Midterm

2 nd Week Lecture Notes

The SPL Programming Language Reference Manual

Chapter 1 Introduction to Computers and C++ Programming

Chapter 2 - Introduction to C Programming

Not Quite C Compiler version 1.0 b1

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

A Fast Review of C Essentials Part I

IPCoreL. Phillip Duane Douglas, Jr. 11/3/2010

Computer Programming


Fundamentals of Programming Session 4

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Macros in C/C++ Computer Science and Engineering College of Engineering The Ohio State University. Lecture 33

Preprocessor Directives

Programming, numerics and optimization

Model Viva Questions for Programming in C lab

CSCI 171 Chapter Outlines

Lecture 10. Command line arguments Character handling library void* String manipulation (copying, searching, etc.)

Chapter 1 & 2 Introduction to C Language

Compiler, Assembler, and Linker

Assignment #5 Answers

Lesson 5: Functions and Libraries. EE3490E: Programming S1 2018/2019 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

Object Oriented Design

Lecture 2: C Programming Basic


UEE1302 (1102) F10: Introduction to Computers and Programming

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #54. Organizing Code in multiple files

Fundamentals of Programming. Lecture 3: Introduction to C Programming

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Course organization. Course introduction ( Week 1)

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

C for Engineers and Scientists: An Interpretive Approach. Chapter 7: Preprocessing Directives

Getting started with C++ (Part 2)

Programming Languages & Translators. XML Document Manipulation Language (XDML) Language Reference Manual

A A B U n i v e r s i t y

ME 461 C review Session Fall 2009 S. Keres

The C Programming Language Guide for the Robot Course work Module

#include. Practical C Issues: #define. #define Macros. Example. #if

Modules:Context-Sensitive Keyword

Computers Programming Course 5. Iulian Năstac

CS113: Lecture 7. Topics: The C Preprocessor. I/O, Streams, Files

Transcription:

Control flow and string example C and C++ 2. Functions Preprocessor Alastair R. Beresford University of Cambridge Lent Term 2007 #include <string.h> char s[]="university of Cambridge Computer Laboratory"; char c; int i, j; for (i=0,j=strlen(s)-1;i<j;i++,j--) /* strlen(s)-1? */ c=s[i], s[i]=s[j], s[j]=c; printf("%s\n",s); 1 / 19 2 / 19 Functions C does not have objects, but does have function support A function definition has a return type, parameter specification, and a body or statement; for example: int power(int base, int n) stmt A function declaration has a return type and parameter specification followed by a semicolon; for example: int power(int base, int n); The use of the extern keyword for function declarations is optional All arguments to a function are copied or passed-by-value; modification of the local value does not affect the original Just as for variables, a function must have exactly one definition and can have multiple declarations A function which is used but only has a declaration, and no definition, results in a link error Functions cannot be nested 3 / 19 Function type-system nasties A function definition with no values (e.g. power()) is not an empty parameter specification, rather it means that its arguments should not be type-checked! (this is not the case in C++) Instead, a function with no arguments is declared using void An ellipsis (...) can be used for partial parameter specification, for example: int printf(char* fmt,...) stmt The ellipsis is useful for defining functions with variable length arguments, but leaves a hole in the type system (stdarg.h) In comparison, C++ uses operator overloading to provide better I/O type safety (more on this later) 4 / 19

Recursion Functions can call themselves recursively On each call, a new set of local variables are created Therefore, a function recursion of depth n has n sets of variables Recursion can be useful when dealing with recursively defined data structures, like trees (more on such data structures later) Recursion can also be used as you would in ML: unsigned int fact(unsigned int n) { return n? n*fact(n-1) : 1; Compilation The compiler transforms each C source file or execution unit into an object file An object file consists of machine code, and a list of: defined or exported symbols, together with undefined or imported symbols For each object file, the compiler generates: defined symbols for defined function names and global variables undefined symbols for functions and global variables which are declared but not defined A linker combines several object files into an executable by: combining all object code into a single file adjusting the absolute addresses from each object file resolving all undefined symbols The Part 1b Compiler Course describes how to build a compiler and linker in more detail 5 / 19 6 / 19 Handling code in multiple files in C Multi-source file example C separates declaration from definition for both variables and functions This allows portions of code to be split across multiple files Code in different files can then be compiled at different times This allows libraries to be compiled once, but used many times It also allows companies to sell binary-only libraries In order to use code written in another file we still need a declaration A header file can be used to: supply the declarations of function and variable definitions in another file provide preprocessor macros (more on this later) avoid duplication (and errors) that would otherwise occur Header File example4.h /*reverse a string in place */ void reverse(char str[]); Source File example4a.c #include <string.h> #include "example4.h" /*reverse a string in place */ void reverse(char s[]) { int c, i, j; for (i=0,j=strlen(s)-1;i<j;i++,j--) c=s[i], s[i]=s[j], s[j]=c; Source File example4b.c #include "example4.h" char s[] = "Reverse me"; reverse(s); printf("%s\n",s); 7 / 19 8 / 19

Variable and function scope with static C Preprocessor The static keyword limits the scope of a variable or function In the global scope, static does not export the function or variable symbol This prevents the variable or function from being called externally In the local scope, a static variable retains its value between function calls A single static variable exists even if a function call is recursive The preprocessor is executed before any compilation takes place It manipulates the textual content of the source file in a single pass Amongst other things, the preprocessor: deletes each occurrence of a backslash followed by a newline; replaces comments by a single space; replaces definitions, obeys conditional preprocessing directives and expands macros; and it replaces escaped sequences in character constants and string literals and concatenates adjacent string literals 9 / 19 10 / 19 Controlling the preprocessor programmatically The #include directive The preprocessor can be used by the programmer to rewrite source code This is a powerful (and, at times, useful) feature, but can be hard to debug The preprocessor interprets lines starting with # with a special meaning Two text substitution directives: #include and #define Conditional directives: #if, #elif, #else and #endif The #include directive performs text substitution It is written in one of two forms: #include "filename" #include <filename> Both forms replace the #include... line in the source file with the contents of filename The quote (") form searches for the file in the same location as the source file, then searches a predefined set of directories The angle (<) form searches a predefined set of directories When a #included file is changed, all source files which depend on it should be recompiled 11 / 19 12 / 19

The #define directive The #define directive has the form: #define name replacement text The directive performs a direct text substitution of all future examples of name with the replacement text for the remainder of the source file The name has the same constraints as a standard C variable name Replacement does not take place if name is found inside a quoted string By convention, name tends to be written in upper case to distinguish it from a normal variable name Defining macros The #define directive can be used to define macros as well; for example: #define MAX(A,B) ((A)>(B)?(A):(B)) In the body of the macro: prefixing a parameter in the replacement text with # places the parameter value inside string quotes (") placing ## between two parameters in the replacement text removes any whitespace between the variables in generated output Remember: the preprocessor only performs text substitution This means that syntax analysis and type checking doesn t occur until the compilation stage This can, initially at least, generate some confusing compiler warnings on line numbers where the macro is used, rather than when it is defined; for example: #define JOIN(A,B) (A ## B)) 13 / 19 14 / 19 Example #define PI 3.141592654 #define MAX(A,B) ((A)>(B)?(A):(B)) #define PERCENT(D) (100*D) /* Wrong? */ #define DPRINT(D) printf(#d " = %g\n",d) #define JOIN(A,B) (A ## B) const unsigned int a1=3; const unsigned int i = JOIN(a,1); printf("%u %g\n",i, MAX(PI,3.14)); DPRINT(MAX(PERCENT(0.32+0.16),PERCENT(0.15+0.48))); Conditional preprocessor directives Conditional directives: #if, #ifdef, #ifndef, #elif and #endif The preprocessor can use conditional statements to include or exclude code in later phases of compilation #if accepts a (somewhat limited) integer expression as an argument and only retains the code between #if and #endif (or #elif) if the expression evaluates to a non-zero value; for example: #if SOME DEF > 8 && OTHER DEF!= THIRD DEF The built-in preprocessor function defined accepts a name as it s sole argument and returns 1L if the name has been #defined; 0L otherwise #ifdef N and #ifndef N are equivalent to #if defined(n) and #if!defined(n) respectively #undef can be used to remove a #defined name from the preprocessor macro and variable namespace. 15 / 19 16 / 19

Example Error control Conditional directives have several uses, including preventing double definitions in header files and enabling code to function on several different architectures; for example: #if SYSTEM SYSV #define HDR "sysv.h" #elif SYSTEM BSD #define HDR "bsd.h" #else #define HDR "default.h" #endif #include HDR #ifndef MYHEADER H #define MYHEADER H 1... declarations & definitions... #endif /*!MYHEADER H */ To help other compilers which generate C code (rather than machine code) as output, compiler line and filename warnings can be overridden with: #line constant "filename" The compiler then adjusts its internal value for the next line in the source file as constant and the current name of the file being processed as filename ("filename" may be omitted) The statement #error some text causes the preprocessor to write a diagnostic message containing some text There are several predefined identifiers that produce special information: LINE, FILE, DATE, and TIME. 17 / 19 18 / 19 Exercises 1. Write a function definition which matches the declaration int cntlower(char str[]);. The implementation should return the number of lower-case letters in a string 2. Use function recursion to write an implementation of merge sort for a fixed array of integers; how much memory does your program use for a list of length n? 3. Define a macro SWAP(t,x,y) that exchanges two arguments of type t (K&R, Exercise 4-14) 4. Define a macro SWAP(x,y) that exchanges two arguments of the same type (int or char) without using a temporary 19 / 19