INC 161 , CPE 100 Computer Programming

Slides:



Advertisements
Similar presentations
Files in C Rohit Khokher.
Advertisements

Programming III SPRING 2015 School of Computer and Information Sciences Francisco R. Ortega, Ph.D. McKnight Fellow and GAANN Fellow LECTURE #3 Control.
Chapter 13 & 14 C Preprocessor and Other C Topics Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education.
Lecture 2 Introduction to C Programming
Introduction to C Programming
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 17 - The Preprocessor Outline 17.1Introduction 17.2The #include Preprocessor Directive 17.3The.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction.
Guide To UNIX Using Linux Third Edition
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
Introduction to C Programming
1 CSE 303 Lecture 13b The C preprocessor reading: Programming in C Ch. 13 slides created by Marty Stepp
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.
CS 161 Introduction to Programming and Problem Solving Chapter 13 C++ Preprocessor Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied verbatim.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Pre-Processor Commands Gabriel Hugh Elkaim Spring 2013.
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)
Macros. There are three basic phases for C programming. preprocessing, compiling, and linking. C input file is first passed to a preprocessing program.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Introduction to C Programming CE Lecture 7 Compiler options and makefiles.
Chapter 13 C Preprocessor C How to Program, 8/e ©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved.
1 Programming in C Hello World! Soon I will control the world! Soon I will control the world!
Engineering Computing I Chapter 4 Functions and Program Structure.
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.
CGS 3460 Preprocessor n All preprocessor directives or commands begin with a #. lE.g. #include C program → Modified C program → Object Code n Can appear.
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.
Program in Multiple Files. l all C++ statements are divided into executable and non-executable l executable - some corresponding machine code is generated.
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.
Pre-Compiler Directives for TTCN-3 Ina Schieferdecker in response to CR5089 and 5090.
COM S 326X Deep C Programming for the 21st Century Prof. Rozier
13 C Preprocessor.
CSCE 206 Structured Programming in C
What Is? function predefined, programmer-defined
The Machine Model Memory
INC 161 , CPE 100 Computer Programming
Chapter 13 - The Preprocessor
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.
14. THE PREPROCESSOR.
Pre-processor Directives
Computer Science 210 Computer Organization
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.
CSc 352 An Introduction to the C Preprocessor
Programming in C Miscellaneous Topics.
C Preprocessor Seema Chandak.
Chapter 11: The Preprocessor
What Is? function predefined, programmer-defined
An Overview of C.
Conditional Compilation
Preprocessor Directives and Macros Chapter 21
Presentation transcript:

INC 161 , CPE 100 Computer Programming Lecture 9 Preprocessing

Introduction Preprocessing Format of preprocessing directives Preprocessing = Do before translating to a machine code Capabilities Inclusion of additional C source files Definition of symbolic constants and macros Conditional preprocessing of a program Format of preprocessing directives A preprocessing directive consists of a sequence of preprocessing tokens that begins with a pound sign #, which must be the first non-space character on the line. Some preprocessing directives are listed below. # is for preprocessing directives (macro)

Note: These commands are not changed to machine code.

Code with preprocessing Compile Procedure Code with preprocessing Substitute code Cut code C Code Change to Machine Language 100010011101 Machine Language

Symbolic Constants and Macros The #define Preprocessing Directive This preprocessing directive is used to create symbolic constants and macros. Form #define identifier replacement-list defines an object-like macro that causes each subsequent instance of the macro names to be replaced by the replacement-list of preprocessing tokens that constitute the remainder of the directive. The new-line is a character that terminates the #define preprocessing directive.

Symbolic constants The simple form of macro is particularly useful for introducing named constants into a program. It allows for easier modification of the constants later on.When programs are processed, all occurrences of symbolic constants indicated by identifier are replaced by the replacement-list. Example: #define BLOCK_SIZE 0x100 we can write int size = BLOCK_SIZE; instead of int size = 0x100; in the program. Note: Cannot redefine symbolic constants with different values by multiple #define statements

Code with preprocessing #define BLOCK 32 main() { int i,j; i = BLOCK; j = BLOCK + 4; } main() { int i,j; i = 32; j = 32 + 4; }

A preprocessing directive of the form #define identifier(identifier-list-opt) replacement-list new-line defines a function-like macro with arguments, similar syntactically to a function call. The parameters are specified by the optional list of identifiers. Example: if a macro mul with two arguments is defined by #define mul(x,y) ((x)*(y)) then the source program line result = mul(5, a+b); is replaced with result = ((5)*(a+b));

NOTE: Parentheses are important in macro definitions. Example: If macro mul is defined as #define mul(x,y) (x*y) The statement result = mul(5, a+b); in the source program becomes result = (5*a+b); The evaluation will be incorrect.

#undef Undefine a symbolic constant or macro, which can later be redefined. Example: #define mul(x,y) ((x)*(y)) /* … */ #undef mul int mul; /* mul can be used after it is undefined */

Source File Inclusion The #include Preprocessing Directive Copy of a specified header file included in place of the directive. It has following two common forms. #include <header.h> Searches standard library for header file and replaces the directive by the entire contents of the file. In Ch, the header is searched according to the paths specified by the the system variable _ipath. C compilers in Unix will typically search the header file in the directory /usr/include. In Visual C++, the header file is searched based on the paths in the environment variable INCLUDE. or cl –I C:/home/assount/include program.c Used for standard library files #include “header.h" C compilers and interpreters will first search the header file in the same directory where the file is being processed, which typically is the current directory. Then search the header file in the paths as if it was included by #include <header.h>.

#include <stdio.h> #include “add.h” main() { …. add(1,2); test.c add.h total #include <stdio.h> #include “add.h” main() { …. add(1,2); printf(“test”) } #include <stdio.h> int add(int x, int y) { …. printf(“add”) } main() add(1,2); printf(“test”) int add(int x, int y) { …. printf(“add”) }

Applications Loading header files #include <stdio.h> Programs with multiple source files to be compiled together Includes user defined header files which have common declarations and definitions (classes, structures, function prototypes, and macros)

Conditional Compilation Enables the user to control the compilation of the program, screen out portions of source code that are not to be compiled. Structure The structure is similar to if and else statement in C. Conditional preprocessing directives #if, #else, #elif, and #endif

Preprocessing directives of the forms #if expr1 /* ... */ #elif expr2 #else #endif check whether the controlling expression evaluates to nonzero. Every #if ends with #endif #define HELP_MODE : #if defined(HELP_MODE) printf(“This is help”); #endif printf(“This is help”); Preprocessed

Preprocessing directives of the forms # ifndef identifier # ifdef identifier # ifndef identifier check whether the identifier is or is not currently defined as a macro name. #ifdef identifier is the short form of #if defined(identifier) #ifndef identifier #if !defined(identifier) Each directive’s condition is checked in order. If it evaluates to false (zero), then the group that it controls is skipped: directives are processed only through the name that determines the directive in order to keep track of the level of nested conditionals. Only the first group whose control condition evaluates to true (nonzero) is processed. If none of the conditions evaluates to true, and there is a #else directive, then the group controlled by the #else is processed; if lacking a #else directive, then all the groups until the #endif are skipped.

Debugging code #define DEBUG /* ... */ double x; x = some_func(); #ifdef DEBUG printf(“The value of x = %f\n”, x); #endif Defining DEBUG to print out the value of x. After debugging, remove #define statement. The debugging statements are ignored.

To include a header file in a program only once, it is typically handled using the combination of the following preprocessing directives #ifndef, #define, and #endif. For example, a header file header.h may consist of the following code fragment. #ifndef HEADER_H #define HEADER_H /* code */ #endif

Multiple placement of include 2 includes Multiple placement of include #include <stdio.h> main() { } #ifndef HEADER_H #define HEADER_H /* code */ #endif main() { } #define HEADER_H /* code */ #ifndef HEADER_H #endif main() { }

/* code */ only paste once #define HEADER_H /* code */ #ifndef HEADER_H #endif main() { } #define HEADER_H /* code */ main() { } /* code */ only paste once

Program 1: Output: Acceleration = 1.364823 (m/s^2) /* File: accelmacro.c */ #include <stdio.h> #define M_G 9.81 #define FORCE(t) (4*(t)-3)+20 #define ACCEL(p, mu, m) (((p)-(mu)*(m)*M_G)/(m)) main() { double a, p, mu, m, t; mu = 0.2; m = 5.0; t = 2.0; p = FORCE(t); a = ACCEL(p, mu, m); // or a = ACCEL(FORCE(t), mu, m); printf("Acceleration a = %f (m/s^2)\n", a); } Output: Acceleration = 1.364823 (m/s^2)