Preprocessor C program → Modified C program → Object Code

Slides:



Advertisements
Similar presentations
Compilation and Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)
Advertisements

Programming III SPRING 2015 School of Computer and Information Sciences Francisco R. Ortega, Ph.D. McKnight Fellow and GAANN Fellow LECTURE #3 Control.
 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.
CS 202 Computer Science II Lab Fall 2009 October 1.
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
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.
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.
Introduction to C Programming CE Lecture 7 Compiler options and makefiles.
1 Homework / Exam Finish up K&R Chapters 3 & 4 Starting K&R Chapter 5 Next Class HW4 due next class Go over HW3 solutions.
Chapter 13 C Preprocessor C How to Program, 8/e ©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved.
Computational Methods of Scientific Programming Lecture 8 Today’s lecture Start C/C++ Basic language features Web page
C Hints and Tips The preprocessor and other fun toys.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
Engineering Computing I Chapter 4 Functions and Program Structure.
CSCI 171 Presentation 8 Built-in Functions, Preprocessor Directives, and Macros.
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.
Lecture 13: Working with Multiple Programmers. Headers Header files: Each standard library has a corresponding header. The function prototype for all.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 5.3Math Library Functions Math library functions –perform.
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.
C Programming Day 3. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Storage Class Specifiers Every variable in a C.
 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.
Pre-Compiler Directives for TTCN-3 Ina Schieferdecker in response to CR5089 and 5090.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
COM S 326X Deep C Programming for the 21st Century Prof. Rozier
13 C Preprocessor.
What Is? function predefined, programmer-defined
INC 161 , CPE 100 Computer Programming
Chapter 13 - The Preprocessor
Functions Separate Compilation
14. THE PREPROCESSOR.
CSC113: Computer Programming (Theory = 03, Lab = 01)
Deitel- C:How to Program (5ed)
Pre-processor Directives
Chapter 5 - Functions Outline 5.1 Introduction
Introduction to Programming
Scope, Parameter Passing, Storage Specifiers
Separate Compilation.
Functions Chapter 3 of the text Motivation:
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).
Comments, Prototypes, Headers & Multiple Source Files
Homework Finish up K&R Chapters 3 & 4
C Preprocessor Seema Chandak.
Chapter 11: The Preprocessor
What Is? function predefined, programmer-defined
Programming Fundamental
Conditional Compilation
Week 2 - Friday CS222.
Preprocessor Directives and Macros Chapter 21
Presentation transcript:

Preprocessor C program → Modified C program → Object Code All preprocessor directives or commands begin with a #. E.g. #include <stdio.h> C program → Modified C program → Object Code Can appear anywhere in the program No “;” in the end preprocessor compiler

Preprocessor Directives Macro definition #define, #undef File inclusion #include Conditional Compilation #if, #ifdef, #ifndef, #elseif, #else Others

#define To define constants or any macro substitution.     #define <macro> <replacement name>   E.g.   #define FALSE 0 #define TRUE !FALSE To undefined a macro. E.g. #undef FALSE #define D 2 printf("%d",D); #undef D int D=6;

Define Functions E.g. To get the maximum of two variables: #define max(A,B) A > B ? A:B In the C code:    x = max(q+r,s+t);

File inclusion #include directive Why need file inclusion Include the contents of another file at the point where the directive appears. Why need file inclusion Use built-in functions E.g., printf(), rand(); Reuse code double mean(int x,int y) { return (x+y)/2; } double mean(int ,int ); #include “test.h” printf("%f",mean(3,5)); test.cpp test.h Your code

File inclusion formats #include <file> Used for system header files File contain function prototypes for library functions <stdlib.h> , <math.h> , etc #include "file" Used for header files of your own program looks for a file in the current directory first then system directories if not found

Custom header files Steps for creating custom header files Advantage Create a file with function prototypes Save as a .h file. E.g.: filename.h Load in other files with #include "filename.h" Advantage Reuse functions and data structure declaration

Conditional compilation Useful when machine-dependencies setting certain options at compile-time. Expressions #if expression #ifdef expression (same as #if defined ) #ifudef #elif and #else #endif

Examples Example: write programs that are portable to several machines or operating systems #if defined(WINDOWS) #elif defined(LINUX) #elif defined(SOLARIS) #endif Example: Providing a default definition for a macro #ifndef BUFFER_SIZE #define BUFFER_SIZE 256

int inline func(void); Inline functions Ret_Type inline func_name(params); e.g. int inline func(void); Inline hints to the compiler that this function should be unrolled an inlined into wherever it is called. This can avoid function call overhead. Problems with inline: The compiler may happily ignore this. It’s kind of like the register keyword in this manner. It is merely a hint; it is not a command. You may (and probably will) make your program size larger. Read the previous bullet as “cache miss” or “page fault”.