ECE 103 Engineering Programming Chapter 37 C Macro Parameters

Slides:



Advertisements
Similar presentations
Spring Semester 2013 Lecture 5
Advertisements

ECE 103 Engineering Programming Chapter 54 Recursion Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material developed.
1 CS 161 Introduction to Programming and Problem Solving Chapter 9 C++ Program Components Herbert G. Mayer, PSU Status 10/20/2014.
 2006 Pearson Education, Inc. All rights reserved Templates.
Miscellaneous topicsCS-2301 B-term Miscellaneous Topics CS-2301, System Programming for Non-majors (Slides include materials from The C Programming.
Introduction to C Programming
Macro & Function. Function consumes more time When a function is called, the copy of the arguments are passed to the parameters in the function. After.
CS 161 Introduction to Programming and Problem Solving Chapter 13 C++ Preprocessor Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied verbatim.
ECE 103 Engineering Programming Chapter 10 Variables, AKA Objects Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103.
Macros. There are three basic phases for C programming. preprocessing, compiling, and linking. C input file is first passed to a preprocessing program.
CS212: Object Oriented Analysis and Design Lecture 12: Operator Overloading-II.
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
ECE 103 Engineering Programming Chapter 48 Typedef and Enum Type Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
ECE 103 Engineering Programming Chapter 47 Dynamic Memory Alocation Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
ECE 103 Engineering Programming Chapter 24 Sorting Herbert G. Mayer, PSU CS Status 6/2/2015 Initial content copied verbatim from ECE 103 material developed.
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
ECE 103 Engineering Programming Chapter 9 gcc Compiler Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material.
CS 161 Introduction to Programming and Problem Solving Chapter 19 Single-Dimensional Arrays Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied.
ECE 103 Engineering Programming Chapter 18 Iteration Herbert G. Mayer, PSU CS Status 7/19/2015 Initial content copied verbatim from ECE 103 material developed.
ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material.
ECE 103 Engineering Programming Chapter 49 Structures Unions, Part 1 Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE.
ECE 103 Engineering Programming Chapter 53 Generic Algorithms Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material.
ECE 103 Engineering Programming Chapter 50 Structures Unions, Part 2 Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE.
CS 161 Introduction to Programming and Problem Solving Chapter 18 Control Flow Through C++ Program Herbert G. Mayer, PSU Status 10/8/2014 Initial content.
ECE 103 Engineering Programming Chapter 31 C Scopes Herbert G. Mayer, PSU CS Status 8/1/2015 Initial content copied verbatim from ECE 103 material developed.
ECE 103 Engineering Programming Chapter 7 Compiling C Programs Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
ECE 103 Engineering Programming Chapter 15 C Standard Library Herbert G. Mayer, PSU CS Status 7/19/2015 Initial content copied verbatim from ECE 103 material.
CS 161 Introduction to Programming and Problem Solving Chapter 17 Nested Loops Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim from.
ECE 103 Engineering Programming Chapter 41 C Pointers, Part 3 Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material.
ECE 103 Engineering Programming Chapter 3 Numbers Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
CS 161 Introduction to Programming and Problem Solving Chapter 12 C++ Statements Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied verbatim.
ECE 103 Engineering Programming Chapter 30 C Functions Herbert G. Mayer, PSU CS Status 8/9/2014 Initial content copied verbatim from ECE 103 material developed.
1 CS 106 Computing Fundamentals II Chapter 42 “Sub Procedures And Functions” Herbert G. Mayer, PSU CS Status 8/5/2013 Initial content copied verbatim from.
ECE 103 Engineering Programming Chapter 45 Pointers to Functions Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
1 CS 106 Computing Fundamentals II Chapter 85 “Excel Tables” Herbert G. Mayer, PSU CS status 6/14/2013 Initial content copied verbatim from CS 106 material.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed.
Functions Chapter 5 CS12 - Computer Programming 1 Chapter 5.
CS 106 Computing Fundamentals II Chapter 35 “Controls For Choices”
Functions Chapter 6-Part 2.
ECE 103 Engineering Programming Chapter 3 Numbers
Functions Separate Compilation
CS 161 Introduction to Programming
CS 106 Computing Fundamentals II Chapter 73 “Ranges”
Exam 4 review Copyright © 2008 W. W. Norton & Company.
Functions, Part 2 of 3 Topics Functions That Return a Value
CS 106 Computing Fundamentals II Chapter 66 “Working With Strings”
Functions Chapter 9 Copyright © 2008 W. W. Norton & Company.
Parameter Passing in Java
ECE 103 Engineering Programming Chapter 32 Array Parameters
Herbert G. Mayer, PSU CS Status 8/2/2013
ECE 103 Engineering Programming Chapter 56 Runtime Errors
ECE 103 Engineering Programming Chapter 19 Nested Loops
ECE 103 Engineering Programming Chapter 12 More C Statements
ECE 103 Engineering Programming Chapter 51 Random Numbers
ECE 103 Engineering Programming Chapter 25 C Strings, Part 1
ECE 103 Engineering Programming Chapter 46 argc, argv, envp
ECE 103 Engineering Programming Chapter 8 Data Types and Constants
ECE 103 Engineering Programming Chapter 62 Stack Implementation
CS 106 Computing Fundamentals II Chapter 69 “Event Loop”
ECE 103 Engineering Programming Chapter 64 Tree Implementation
ECE 103 Engineering Programming Chapter 20 Change in Flow of Control
ECE 103 Engineering Programming Chapter 63 Queue Implementation
ECE 103 Engineering Programming Chapter 35 C Pointers, Part 1
Herbert G. Mayer, PSU CS Status 7/19/2015
ECE 103 Engineering Programming Chapter 38 C Pointers, Part 2
ECE 103 Engineering Programming Chapter 18 Iteration
ECE 103 Engineering Programming Chapter 22 Selection
Functions, Part 2 of 3 Topics Functions That Return a Value
Preprocessor Directives and Macros Chapter 21
Presentation transcript:

ECE 103 Engineering Programming Chapter 37 C Macro Parameters Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material developed by Professor Phillip Wong @ PSU ECE

Syllabus Defining a Parameterized Macro Parameterized Macros vs. C Functions

Defining a Parameterized Macro Similar to a C function, preprocessor macros can be defined with a parameter list. The macro’s parameters do not require data types to be specified. Syntax: #define MACRONAME(parameter_list) text No white space is allowed between the name of the macro and the left parenthesis. 2

Parameters in the parameter_list are separated by commas. Example: #define MAXVAL(A,B) ((A) > (B)) ? (A) : (B) #define PRINT(e1,e2) printf(”%c\t%d\n”,(e1),(e2)); #define putchar(x) putc(x, stdout) Arguments passed to the parameterized macro are substituted verbatim into the corresponding parameters. 3

Care must be taken to avoid side-effects Care must be taken to avoid side-effects. Use parentheses to force a particular interpretation. Example: /* This has potential side-effects */ #define PROD1(A,B) A * B PROD1(1+3,2) → 1+3 * 2 /* Use parentheses as needed */ #define PROD2(A,B) (A) * (B) PROD2(1+3,2) → (1+3) * (2) 4

Example: #include <stdio.h> #include <math.h> #define EVERYFIFTH(n) ((n) % 5 == 0) int main (void) { int k; for (k = 0; k < 100; k++) if (EVERYFIFTH(k)) /* Silly, but it does work */ printf("%3d %.3f\n", k, sqrt(k)); return 0; } 5

Parameterized Macros vs. C Functions Since macro expansion is done at compile-time, there is no function call overhead at runtime. There is no type checking performed for macros, which is useful if generic data types are desired for arguments. Unlike a C function, parameterized macros have no concept of local variables. 6