Macros. There are three basic phases for C programming. preprocessing, compiling, and linking. C input file is first passed to a preprocessing program.

Slides:



Advertisements
Similar presentations
THE PREPROCESSOR. Preprocessing is (apparently) done before actual compilation begins. The preprocessor doesnt know (very much) C. Major kinds of preprocessor.
Advertisements

Compilation and Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)
Lecture 2 Introduction to C Programming
CSc 352 An Introduction to the C Preprocessor Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 17 - The Preprocessor Outline 17.1Introduction 17.2The #include Preprocessor Directive 17.3The.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction.
 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
CMSC 104, Version 9/011 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program 104 C Programming Standards and Indentation.
The Preprocessor #include #define N 10 C program Preprocessor Modified C program Preprocessor Object codes.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
 2007 Pearson Education, Inc. All rights reserved C Preprocessor.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 19 - The Preprocessor Outline 19.1 Introduction 19.2 The #include Preprocessor Directive 19.3.
The Pre-processor A unique feature of c language is the pre-processor. A program can use the tools provided by pre-processor to make his program easy to.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Pre-Processor Commands Gabriel Hugh Elkaim Spring 2013.
1 CSC103: Introduction to Computer and Programming Lecture No 26.
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)
The Structure of a C++ Program. Outline 1. Separate Compilation 2. The # Preprocessor 3. Declarations and Definitions 4. Organizing Decls & Defs into.
Programming With C.
Introduction to C Programming CE Lecture 7 Compiler options and makefiles.
CMSC 104, Version 9/011 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program 104 C Programming Standards and Indentation.
Programming Fundamentals. Today’s Lecture Why do we need Object Oriented Language C++ and C Basics of a typical C++ Environment Basic Program Construction.
COMPUTER PROGRAMMING. A Typical C++ Environment Phases of C++ Programs: 1- Edit 2- Preprocess 3- Compile 4- Link 5- Load 6- Execute Loader Primary Memory.
Chapter 13 C Preprocessor C How to Program, 8/e ©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved.
C Hints and Tips The preprocessor and other fun toys.
Engineering Computing I Chapter 4 Functions and Program Structure.
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
L function n predefined, programmer-defined l arguments, (formal) parameters l return value l function call, function invocation l function definition.
1 Preprocessor –How it works File Inclusion: #include Macro Definition: #define Predefined macros –__LINE__, __FILE__, __DATE__, __TIME__ Conditional Compilation.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessing Lecture 12 April 7, 2005.
Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Chapter 7: Preprocessing.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessor Midterm Review Lecture 7 Feb 17, 2004.
Compiler Directives. The C Preprocessor u The C preprocessor (cpp) changes your source code based on instructions, or preprocessor directives, embedded.
THE PREPROCESSOR
The Preprocessor Directives Introduction Preprocessing – Occurs before program compiled Inclusion of external files Definition of symbolic constants.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
© 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 PREPROCESSOR. Introduction  It is a program that processes our source program before it is passed to the compiler.  Preprocessor commands (often known.
C language + The Preprocessor. + Introduction The preprocessor is a program that processes that source code before it passes through the compiler. It.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
Pre-Compiler Directives for TTCN-3 Ina Schieferdecker in response to CR5089 and 5090.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
COM S 326X Deep C Programming for the 21st Century Prof. Rozier
Introduction to C Topics Compilation Using the gcc Compiler
13 C Preprocessor.
What Is? function predefined, programmer-defined
CSC201: Computer Programming
INC 161 , CPE 100 Computer Programming
Chapter 13 - The Preprocessor
Functions Separate Compilation
14. THE PREPROCESSOR.
Pre-processor Directives
Introduction to Programming
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).
Preprocessor.
CSc 352 An Introduction to the C Preprocessor
C Preprocessor Seema Chandak.
C Programming Language
Chapter 11: The Preprocessor
What Is? function predefined, programmer-defined
Introduction to Programming - 1
Introduction to C Topics Compilation Using the gcc Compiler
Introduction to C Programming
Conditional Compilation
Preprocessor Directives and Macros Chapter 21
Presentation transcript:

Macros

There are three basic phases for C programming. preprocessing, compiling, and linking. C input file is first passed to a preprocessing program called the preprocessor. The output of the preprocessor is passed to the C compiler. The output of the C compiler is then fed into a linker program. The linker finally produces an executable form of your program.

Macros C Preprocessor is just a text substitution tool and they instruct compiler to do required pre- processing before actual compilation. Preprocessing is done before actual compilation begins. All preprocessor commands begin with a pound symbol (#).

Macros There are essentially three uses of the preprocessor--directives, constants, and macros. Directives are commands that tell the preprocessor to skip part of a file, include another file, or define a constant or macro. All other uses of the preprocessor involve processing #define'd constants or macros. Typically, constants and macros are written in ALL CAPS to indicate they are special.

Macros Line that begin with # are called preprocessing directives. Major kinds of preprocessor directives: File inclusion Macro definition Conditional compilation

Macros File inclusion Used to include a file Syntax # include # include “filename” The difference between <> and “”  looks firstly in the header path for the header file  whereas "" looks in the current directory of the file for the header.

Macros Macro Definition A macro is a fragment of code which has been given a name. Whenever the name is used, it is replaced by the contents of the macro. The #define directive specifies a macro identifier and a replacement list, and terminates with a new-line character. The #undef directive is used to cancel a definition for a macro.

Macros Macro Definition syntax #define identifier replacement-list Eg #include #define MACRO 25 Void main() { printf("\nMACRO Defined with value %d", MACRO); } Output MACRO Defined with value 25 (MACRO will be replaced to 25)

Macros Macro Definition Examples of simple macros: #define N 100 #define PI #define WARNING "Warning: nonstandard feature" #define BEGIN { #define END } void main() BEGIN // replace { int a[N];// replace int a[100] printf(“\nvalue of Pie %f”,PI);// print printf(“%s”,WARNING); END // replace }

Macros Parameterized Macro Function-like macros can take arguments, just like functions. Insert parameters between the pair of parentheses in the macro definition. To invoke a macro that takes arguments, ie the name of the macro followed by a list of actual arguments in parentheses, separated by commas. The number of arguments give must match the number of parameters in the macro definition. When the macro is expanded, each use of a parameter in its body is replaced by the corresponding argument.

Macros Eg #define MIN(X, Y) ((X) < (Y) ? (X) : (Y)) printf(“minimum %d”,MIN(a, b)); ==> x = ((a) < (b) ? (a) : (b)); # define EVEN(N) (N%2==0) if(EVEN(2)==1) { printf(“even”); }

Macros Conditional Compilation The conditional directives are: – #if - Test if a compile time condition is true – #else - The alternative for #if – #ifdef - If this macro is defined – #ifndef - If this macro is not defined – #elif - #else an #if in one statement – #endif - End preprocessor conditional

Macros Conditional Compilation #if defined(CREDIT) credit(); #elif defined(DEBIT) debit(); #else printerror(); #endif The function call to credit is compiled if the identifier CREDIT is defined. If the identifier DEBIT is defined, the function call to debit is compiled. If neither identifier is defined, the call to printerror is compiled. Note thatCREDIT and credit are distinct identifiers in C and C++ because their cases are different.