C Preprocessor. Prabhat Kumar Padhy

Similar documents
Unit 4 Preprocessor Directives

Chapter 7: Preprocessing Directives

C for Engineers and Scientists: An Interpretive Approach. Chapter 7: Preprocessing Directives

Have examined process Creating program Have developed program Written in C Source code

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

Conditional Compilation

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

A Fast Review of C Essentials Part II

OBJECT ORIENTED PROGRAMMING USING C++

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

Topic 6: A Quick Intro To C

Errors During Compilation and Execution Background Information

fpp: Fortran preprocessor March 9, 2009

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

CS 107 Lecture 18: GCC and Make

COMsW Introduction to Computer Programming in C

M2 Instruction Set Architecture

Programming for Engineers C Preprocessor

ENCE Computer Organization and Architecture. Chapter 1. Software Perspective

The Compilation Process

Come and join us at WebLyceum


The C Pre Processor ECE2893. Lecture 18. ECE2893 The C Pre Processor Spring / 10

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

PROGRAMMAZIONE I A.A. 2017/2018

C: Program Structure. Department of Computer Science College of Engineering Boise State University. September 11, /13

Compiler Theory. (GCC the GNU Compiler Collection) Sandro Spina 2009

CMPSC 311- Introduction to Systems Programming Module: Build Processing

Macros in C/C++ Computer Science and Engineering College of Engineering The Ohio State University. Lecture 33

Practical C Issues:! Preprocessor Directives, Multi-file Development, Makefiles. CS449 Fall 2017

SISTEMI EMBEDDED. The C Pre-processor Fixed-size integer types Bit Manipulation. Federico Baronti Last version:

Intermediate Programming, Spring 2017*

Lectures 5-6: Introduction to C

Lectures 5-6: Introduction to C

Control flow and string example. C and C++ Functions. Function type-system nasties. 2. Functions Preprocessor. Alastair R. Beresford.

MODULE 5: Pointers, Preprocessor Directives and Data Structures

INDEX. Figure I-0. Listing I-0. Table I-0. Symbols.DIRECTIVE (see Assembler directives)? preprocessor operator 3-34

Introduction to C Final Review Chapters 1-6 & 13

Macros and Preprocessor. CGS 3460, Lecture 39 Apr 17, 2006 Hen-I Yang

Unit 1: Introduction to C Language. Saurabh Khatri Lecturer Department of Computer Technology VIT, Pune

EL2310 Scientific Programming

Draft. Chapter 1 Program Structure. 1.1 Introduction. 1.2 The 0s and the 1s. 1.3 Bits and Bytes. 1.4 Representation of Numbers in Memory

A software view. Computer Systems. The Compilation system. How it works. 1. Preprocesser. 1. Preprocessor (cpp)

Conduite de Projet Cours 4 The C build process

Basic C Programming. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

The C Preprocessor. Richard M. Stallman, Zachary Weinberg. For gcc version (GCC)

CMPSC 311- Introduction to Systems Programming Module: Build Processing

CSCI 171 Chapter Outlines

SISTEMI EMBEDDED. The C Pre-processor Fixed-size integer types Bit Manipulation. Federico Baronti Last version:

Slide Set 5. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #54. Organizing Code in multiple files

CS133 C Programming. Instructor: Jialiang Lu Office: Information Center 703

C Programming Review CSC 4320/6320

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

SISTEMI EMBEDDED. The C Pre-processor Fixed-size integer types Bit Manipulation. Federico Baronti Last version:

The C Preprocessor. Richard M. Stallman, Zachary Weinberg. For gcc version (pre-release) (GCC)

The C Preprocessor. for GCC version Richard M. Stallman Zachary Weinberg

Programs. Function main. C Refresher. CSCI 4061 Introduction to Operating Systems

Problem Solving and 'C' Programming

Hello, World! in C. Johann Myrkraverk Oskarsson October 23, The Quintessential Example Program 1. I Printing Text 2. II The Main Function 3

Preprocessing directives are lines in your program that start with `#'. The `#' is followed by an identifier that is the directive name.

CSE 374 Programming Concepts & Tools. Brandon Myers Winter 2015 C: Linked list, casts, the rest of the preprocessor (Thanks to Hal Perkins)

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

This course has three parts. Elements of C programming. Arithmatic and Error analysis. Some algorithms and their implementation

Programming. Data Structure

Language Translation. Compilation vs. interpretation. Compilation diagram. Step 1: compile. Step 2: run. compiler. Compiled program. program.

#include. Practical C Issues: #define. #define Macros. Example. #if

G52CPP C++ Programming Lecture 6. Dr Jason Atkin

Compiler, Assembler, and Linker

Appendix A. The Preprocessor

A. Year / Module Semester Subject Topic 2016 / V 2 PCD Pointers, Preprocessors, DS

CSc 520 Principles of Programming Languages

Appendix. Grammar. A.1 Introduction. A.2 Keywords. There is no worse danger for a teacher than to teach words instead of things.

Programing in C. Pierre-Alain FOUQUE

1 You seek performance 3

Reliable C++ development - session 1: From C to C++ (and some C++ features)

#include <stdio.h> int main() { printf ("hello class\n"); return 0; }

This exam is to be taken by yourself with closed books, closed notes, no calculators.

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

More on C programming

3 PREPROCESSOR. Overview. Listing 3-0. Table 3-0.

C Programming. The C Preprocessor and Some Advanced Topics. Learn More about #define. Define a macro name Create function-like macros.

University of Passau Department of Informatics and Mathematics Chair for Programming. Bachelor Thesis. Andreas Janker

Compilation, Disassembly, and Profiling (in Linux)

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 12, FALL 2012

Oregon State University School of Electrical Engineering and Computer Science. CS 261 Recitation 2. Spring 2016

C and C++ 2. Functions Preprocessor. Alan Mycroft

COMP322 - Introduction to C++

LC-3 Assembly Language

Final CSE 131B Spring 2004

Design and development of embedded systems for the Internet of Things (IoT) Fabio Angeletti Fabrizio Gattuso

CS3157: Advanced Programming. Outline

Outline. Compiling process Linking libraries Common compiling op2ons Automa2ng the process

Tutorial: Compiling, Makefile, Parallel jobs

For Teacher's Use Only Q No Total Q No Q No

Creating a C++ Program

Symbols, Compilation Units, and Pre-Processing

Chapter 1 INTRODUCTION

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

BİL200 TUTORIAL-EXERCISES Objective:

Transcription:

C Preprocessor Prabhat Kumar Padhy 1

C Preprocessor? Creating C program, Compiling and Runnings. Create using some editor Compilation gcc test.c (or) gcc o test test.c Running./test C Preprocessor The preprocessor offers various features called Preprocessor directive Begin with # symbol These can be placed anywhere in the program Use of preprocessor programs easier to develop easier to read easier to modify C code more transportable between different machine architectures. 2

4 step process C? Preprocessing cpp hello.c > hello.i Compilation gcc -S hello.i Assembly as -o hello.o hello.s Linker gcc -o hello.exe hello.o 3

Macro expansion? Macro expansion A simple macro is kind of abbreviation It is a name which stands for a fragment of code. It can also be referred as manifest constants. Ex1 foo = X; #define X 4 bar = X; Ex2 #define BUFSIZE 1020 #define TABLESIZE BUFSIZE EX3 for finding min of two numbers #define min(x, Y) ((X) < (Y)? (X) : (Y)) 4

Macros? Standard Macros #define PI 3.1415 float r=6.25; float area; area = PI*r*r; printf( \narea = %f,area); return 0; Macros with Arguements #define PI 3.1415 #define AREA(x) (PI*x*x) float r1=6.25, r2=2.5,a; a= AREA(r1); printf( \narea = %f,a); a= AREA(r1); printf( \narea = %f,a); return 0; Predefined Macro Standard Predefined Macro FILE corresponds to the name of current input file LINE current input line no Usefule in generating error messages DATE, TIME etc.. Non-standard predefined Macro unix, BSD, sun etc are example of some non-standard predefined macro for the machine names.. 5

Macro Vs Function? Macro call the preprocessor and replaces macro template with macro expansion In function call, control is passed to a function along with certain arguments, some calculation is performed and value is returned. Which is better? Macros make the program run faster but increases program size, whereas functions make the program smaller and compact but increases execution time. Programmer should do judicious job, by doing a trade off between these two. Too many macros will unnecessarily make the code size longer. 6

File Inclusion? File inclusion #include <file> or #include file header.h, file contains char *test (); program.c, file contains int x; #include "header.h" main () printf (test ()); the output generated by the C preprocessor for `program.c' as input would be int x; char *test (); main () printf (test ()); 7

Macros? In the above example, hellomake.h file is included which is a user defined header file. Similarly depending on the need many files can be defined as part of any project 8

Conditional Compilation? Conditional Compilation In a macro processor, a conditional is a directive that allows a part of the program to be ignored during compilation, on some conditions. Usage?? A program may need to use different code depending on the machine or operating system it is to run on. You may want to be able to compile the same source file into two different programs. A conditional whose condition is always false is a good way to exclude code from the program but keep it as a sort of comment for future reference. Syntax of conditional Usage?? The #if directive The `#else' Directive The `#elif' Directive 9

Conditional Compilation? The #if directive #if expression controlled text #endif /* expression */ The #else directive #if expression text-if-true #else /* Not expression */ text-if-false #endif /* Not expression */ The #elif directive #if X == 1.. #elif X == 2... #else /* X!= 2 and X!= 1*/... #endif /* X!= 2 and X!= 1*/ 10

Conditonal and Macro? EX1 EX2 #if BUFSIZE == 1020 printf ("Large buffers!\n"); #endif /* BUFSIZE is large */ #if defined (vax) defined (ns16000) EX3 #ifdef name is equivalent to `#if defined (name)'. #ifndef name is equivalent to `#if! defined (name)' 11

Miscellaneous Directive? #undef Directive #include <file> or #include file #def A 3 #undef A #pragma Directive: This is another special purpose directive which can be used to turn on / off certain features. It varies from compiler to compiler For ex: write assembly language statements in C program. 12

Miscellaneous? Miscellaneous directives such as line control, erroring out etc.. EX1 #ifdef FILE #error "Won't work on this file. " #endif EX2 #if HASH_TABLE_SIZE % 2 == 0 HASH_TABLE_SIZE % 3 == 0 \ HASH_TABLE_SIZE % 5 == 0 #error HASH_TABLE_SIZE should not be divisible by a small prime #endif 13

short number,sum; int bignumber,bigsum; char letter; main() Examples / Questions? Ex-1 #define NO #define YES int i=5,j; if(i>5) j = YES; else j = NO; printf( %d,j); return 0; EX - 2 #define HELLO(m) printf( m ) #define NEXTLINE printf( \n ): HELLO( The World!! ); NEXTLINE; HELLO( It is going to End ); Ex-3 #define ISUPPER(x) (x>= 65 && x<= 90) #define ISLOWER(x) (x>= 97 && x<= 122) #define ISALPPHA(x) (ISUPPER(x) ISLOWER(x)) int main() char ch = + ; if(isalpha(ch)) printf( ch contains an alphabet ); else printf( ch does not contain an alphabet ); 14

short number,sum; int bignumber,bigsum; char letter; main() Examples / Questions? Ex-4 #define THIS #define THAT #ifdef THIS #ifdef THAT printf( Truth is hard to digest ); #else printf( But once realized, hard to forget ); #endif return 0; EX - 5 #define CUBE(x) (x*x*x) int a,b=3; a= CUBE(b++)/b++; Printf( a=%d b=%d,a,b); #define CUBE(x) (x*x*x) int a,b=3; a= CUBE(++b)/++b; Printf( a=%d b=%d,a,b); Ex-6 #define THIS #define THAT #ifdef THIS && THAT printf( Truth is hard to digest ); #else printf( But once realized, hard to forget ); #endif return 0; 15

short number,sum; int bignumber,bigsum; char letter; main() Examples / Questions? Ex-7 #define COND (a >==65 && a<= 90) char a= R ; if(cond) printf( UPPER CASE ); else printf( LOWER CASE ); EX - 8 #define AAA(format, var) printf( var=%format\n,var); int i=3; float a=3.14; AAA(d,i); AAA(f,a); Ex-9 #define DATATYPE char far * DATATYPE s; s= 0xb8000000; *s = A ; return 0; return 0; 16

Questions? 17