Macro A fragment of code which has been given a name.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

THE PREPROCESSOR. Preprocessing is (apparently) done before actual compilation begins. The preprocessor doesnt know (very much) C. Major kinds of preprocessor.
CSCI 171 Presentation 11 Pointers. Pointer Basics.
1 CS 201 Introduction to C (1) Debzani Deb. 2 Outline Overview of C General form of a C program C Language Elements.
1 Key Concepts:  Why C?  Life Cycle Of a C program,  What is a computer program?  A program statement?  Basic parts of a C program,  Printf() function?
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 10: Appendices.
1 ICS103 Programming in C Lecture 2: Introduction to C (1)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction.
Exercise 7 Strings. An array of characters Used to store text Another way to initialize: char A[ ]=“blabla”;
1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3.
Programming C/C++ on Eclipe Trình bày: Ths HungNM C/C++ Training.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
1 CSE 303 Lecture 13b The C preprocessor reading: Programming in C Ch. 13 slides created by Marty Stepp
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 CMPE-013/L Functions Gabriel Hugh Elkaim Spring 2012.
 2007 Pearson Education, Inc. All rights reserved C Preprocessor.
Windows Programming Lecture 05. Preprocessor Preprocessor Directives Preprocessor directives are instructions for compiler.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 12 - The Preprocessor Directives (Macros)
Chapter 2 : Overview of C By Suraya Alias. /*The classic HelloWorld */ #include int main(void) { printf(“Hello World!!"); return 0; }
Macros. There are three basic phases for C programming. preprocessing, compiling, and linking. C input file is first passed to a preprocessing program.
Semantics CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
The Structure of a C++ Program. Outline 1. Separate Compilation 2. The # Preprocessor 3. Declarations and Definitions 4. Organizing Decls & Defs into.
CPS120: Introduction to Computer Science Functions.
CSCI 171 Presentation 8 Built-in Functions, Preprocessor Directives, and Macros.
Chapter 7 Templates. Objectives Introduction Function Templates Class Templates Standard Template Library.
ELE118 Introduction to Programming
מערכים (arrays) 02 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 15 1 Department of Computer Science-BGU.
Data Structures & Algorithms
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
Semantics CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessor Midterm Review Lecture 7 Feb 17, 2004.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Overloading operators C++ incorporates the option to use standard operators to perform operations with.
Compiler Directives. The C Preprocessor u The C preprocessor (cpp) changes your source code based on instructions, or preprocessor directives, embedded.
Chapter 6 Methods Chapter 6 - Methods.
THE PREPROCESSOR
The Preprocessor Directives Introduction Preprocessing – Occurs before program compiled Inclusion of external files Definition of symbolic constants.
© Oxford University Press All rights reserved. CHAPTER 10 THE PREPROCESSOR DIRECTIVE.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
Adv. UNIX:pre/111 Advanced UNIX v Objectives of these slides: –look at the features of the C preprocessor Special Topics in Comp. Eng.
C++ Functions A bit of review (things we’ve covered so far)
Unit 10 Code Reuse. Key Concepts Abstraction Header files Implementation files Storage classes Exit function Conditional compilation Command-line arguments.
מערכים (arrays) 02 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 16 Department.
13 C Preprocessor.
Executable program (p1)
INC 161 , CPE 100 Computer Programming
Chapter 13 - The Preprocessor
ICS103 Programming in C Lecture 3: Introduction to C (2)
Functions Separate Compilation
14. THE PREPROCESSOR.
The Preprocessor Based on Chapter 1 in C++ for Java Programmers by Weiss When a C compiler is invoked, the first thing that happens is that the code is.
Programmazione I a.a. 2017/2018.
Pre-processor Directives
Semantics CSE 340 – Principles of Programming Languages Spring 2016
Preprocessor C program → Modified C program → Object Code
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,
C Preprocessor(CPP).
Programming in C Miscellaneous Topics.
Programming in C Miscellaneous Topics.
PROGRAMMING IN HASKELL
CSCE 314: Programming Languages Dr. Dylan Shell
C++ Compilation Model C++ is a compiled language
C Preprocessor Seema Chandak.
POINTER CONCEPT 4/15/2019.
Exercise Arrays.
Lexical Elements & Operators
Variables in C Topics Naming Variables Declaring Variables
CS1100 Computational Engineering
Executable program (p1)
POINTER CONCEPT 8/3/2019.
Computer Architecture and System Programming Laboratory
Preprocessor Directives and Macros Chapter 21
Presentation transcript:

Macro A fragment of code which has been given a name. Whenever the name is used, it is replaced by the contents of the macro. Two kinds of macros Object-like Functon-like You may define any valid identifier as a macro, even if it is a C keyword. The preprocessor does not know anything about keywords.

Object-like Macros A simple identifier which will be replaced by a code fragment. Most commonly used to give symbolic names to numeric constants. #define BUFFER_SIZE 1024 … foo = (char *) malloc (BUFFER_SIZE); #define BUFFER_SIZE 1024 … foo = (char *) malloc (1024); C preprocessor

Object-like Macros By convention, macro names are written in uppercase. The macro’s body ends at the end of the ‘#define’ line. You may continue the definition onto multiple lines, if necessary. #define NUMBERS 1, \ 2, \ 3 int x[] = { NUMBERS }; int x[] = { 1, 2, 3 };

Object-like Macros The C preprocessor scans your program sequentially. Macro definitions take effect at the place you write them. foo = X; #define X 4 bar = X; foo = X; bar = 4; #define TABLESIZE BUFFSIZE #define BUFFSIZE 1024 TABLESIZE BUFFSIZE 1024

Function-like Macros Use the same ‘#define’ directive, but put a pair of parentheses immediately after the macro name. #define lang_init() c_init() … lang_init() c_init() If you put spaces between the macro name and the parentheses in the macro definition, that does not define a function-like macro. It defines an object-like macro. #define lang_init () c_init() … lang_init() () c_init()()

Macro Arguments To define a macro with arguments, insert parameters between the pair of parentheses. #define min(X, Y) ((X)<(Y) ? (X):(Y)) x = min(a, b); -> x = ((a)<(b) ? (a):(b)); y = min(1, 2); -> y = ((1)<(2) ? (1):(2)); z = min(a + 28, *p) -> z = ((a + 28)<(*p) ? (a + 28):(*p)); You can leave macro arguments empty, but cannot leave out arguments entirely. min(, b); -> (( )<(b) ? ( ):(b)); min(a, ); -> ((a)<( ) ? (a):()); min(,); -> (( )<( ) ? ( ):()); min() -> error macro “min” requires 2 arguments, but only 1 given

Undefining and Redefining Macros If a macro ceases to be useful, it may be undefined with the “#undef” directive, which takes a single argument, the name of the macro to undefine. Use the bare macro name, even if the macro is function-like. #define FOO 4 x = FOO; -> x = 4; #undef FOO x = FOO -> x = FOO; Once a macro has been undefined, that identifier may be redefined as a macro by a subsequent “#define” directive.

The following is strcpy() function using pointers. strcpy(s, t) /* copy t to s; pointer version */ { char *s, *t; while (*s ++ = *t ++) ; } Write a macro for strcpy.

Solution 1 (segmentation fault) #include <stdio.h> #define strcpy(x,y) while (*x ++ = *y ++); int main(void) { char *s, *t; t = "abc"; strcpy(s,t); printf("%s\n", s); return 0; }

Solution 2 (segmentation fault) #include <stdio.h> #define strcpy(x,y) {char *tempA = x; while (*tempA ++ = *y ++);} int main(void) { char *s, *t; t = "abc"; strcpy(s,t); printf("%s\n", s); return 0; }

Solution 3 (Finally working!) #include <stdio.h> #define strcpy(x,y) {char *tempA = x; while (*tempA ++ = *y ++);} int main(void) { char s[10], *t; t = "abc"; strcpy(s,t); printf("%s\n", s); return 0; }