Unit 4 Preprocessor Directives

Similar documents
OBJECT ORIENTED PROGRAMMING USING C++

Unit 4. Input/Output Functions

fpp: Fortran preprocessor March 9, 2009

Appendix A. The Preprocessor

Computers Programming Course 5. Iulian Năstac

Chapter 7: Preprocessing Directives


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

Tutorial No. 2 - Solution (Overview of C)

C Preprocessor. Prabhat Kumar Padhy

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

Computer Programming & Problem Solving ( CPPS ) Turbo C Programming For The PC (Revised Edition ) By Robert Lafore

Problem Solving and 'C' Programming

Advanced C Programming Topics

Programming for Engineers C Preprocessor

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

Rule 1-3: Use white space to break a function into paragraphs. Rule 1-5: Avoid very long statements. Use multiple shorter statements instead.

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

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and

PROGRAMMAZIONE I A.A. 2017/2018

Errors During Compilation and Execution Background Information

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

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

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

MODULE 10 PREPROCESSOR DIRECTIVES

Creating a C++ Program

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

CS113: Lecture 7. Topics: The C Preprocessor. I/O, Streams, Files

A Fast Review of C Essentials Part II

MODULE 5: Pointers, Preprocessor Directives and Data Structures

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Chapter 21: Introduction to C Programming Language

EDIABAS BEST/2 LANGUAGE DESCRIPTION. VERSION 6b. Electronic Diagnostic Basic System EDIABAS - BEST/2 LANGUAGE DESCRIPTION

Presented By : Gaurav Juneja

LESSON 4. The DATA TYPE char

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

A Fast Review of C Essentials Part I

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

BLM2031 Structured Programming. Zeyneb KURT

Outline. Computer Programming. Preprocessing in C. File inclusion. Preprocessing in C

Preface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS

Unit 6 Files. putchar(ch); ch = getc (fp); //Reads single character from file and advances position to next character

Data Types and Variables in C language

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

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

UNIT- 3 Introduction to C++

COMsW Introduction to Computer Programming in C

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

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

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

Conditional Compilation

COMP322 - Introduction to C++

BİL200 TUTORIAL-EXERCISES Objective:

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

Chapter 8 Character Arrays and Strings

It is necessary to have a single function main in every C program, along with other functions used/defined by the programmer.

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

Functions. P. S. Suryateja startertutorials.com [short domain - stuts.me] 2

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

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

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

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

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

today cs3157-fall2002-sklar-lect05 1

Computers Programming Course 6. Iulian Năstac

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

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:

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

COMPILER CONSTRUCTION LAB 2 THE SYMBOL TABLE. Tutorial 2 LABS. PHASES OF A COMPILER Source Program. Lab 2 Symbol table

UNIT IV 2 MARKS. ( Word to PDF Converter - Unregistered ) FUNDAMENTALS OF COMPUTING & COMPUTER PROGRAMMING

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

SWEN-250 Personal SE. Introduction to C

Preview from Notesale.co.uk Page 6 of 52

ME 461 C review Session Fall 2009 S. Keres

UNIT-V CONSOLE I/O. This section examines in detail the console I/O functions.

CS240: Programming in C

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

Intermediate Programming, Spring 2017*

IECD Institute for Entrepreneurship and Career Development Bharathidasan University, Tiruchirappalli 23.

A First Book of ANSI C Fourth Edition. Chapter 9 Character Strings

C: How to Program. Week /June/18

Functions. Functions are everywhere in C. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

Multiple Choice Questions ( 1 mark)

Basics of Programming

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).

UNIT IV-2. The I/O library functions can be classified into two broad categories:

UIC. C Programming Primer. Bharathidasan University

Subject: PROBLEM SOLVING THROUGH C Time: 3 Hours Max. Marks: 100

.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar..

Modifiers. int foo(int x) { static int y=0; /* value of y is saved */ y = x + y + 7; /* across invocations of foo */ return y; }

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

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

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

CS3157: Advanced Programming. Outline

Computer Programming Unit v

PART I. Part II Answer to all the questions 1. What is meant by a token? Name the token available in C++.

1 Lexical Considerations

C Programming Multiple. Choice

Transcription:

1 What is pre-processor? The job of C preprocessor is to process the source code before it is passed to the compiler. Source Code (test.c) Pre-Processor Intermediate Code (test.i) Compiler The pre-processor gets the source code (test.c file) as input and creates expanded intermediate source code (test.i file). This expanded source code is then passed to compiler for compilation. The Pre-processor accepts source code as input and is responsible for Removing comments. Interpreting special pre-processor directives denoted by #. 2 List out various pre-processor directives. The preprocessor offers several features called preprocessor directives. Each of these preprocessor directives begins with a # symbol. The directives can be placed anywhere in a program but generally placed at the beginning of a program. Following are the preprocessor directives: Preprocessor Directives Directive Description Macro expansion #define #undef Used to define a macro Used to undefine a macro File inclusion directives #include Inserts a particular header from another file Conditional Directives Miscellaneous Directives #ifdef #ifndef #if #else #elif #endif #error #pragma Returns true if this macro is defined Returns true if this macro is not defined Tests if a compile time condition is true The alternative for #if #else an #if in one statement Ends preprocessor conditional Prints error message on stderr Issues special commands to the compiler, using a standardized method 1

3 What is macro? Explain #define with example. A macro is a fragment of code which has been a given a name. Whenever the name is used, it is replaced by the contents of the macro. You may define any valid identifier as a macro, even if it is a C keyword the pre-processor does not know anything about keyword. In Macro Substitution an identifier in a program is replaced by a pre defined string composed of one or more tokens. We can use the #define directive for this purpose. It has the following form: #define <identifier> <token> The definition should start with the keyword #define and should follow by identifier and a token with at least one blank space between them. The token may be any text and identifier must be a valid C name. The pre-processor replaces every occurrence of the identifier in the source code by token. There are different forms of macro substitution. The most common form is: 1) Simple macro substitution. 2) Argument macro substitution. Simple token replacement is commonly used to define constants. #define PI 3.1415926 #include<stdio.h> #define TEN 10 void main () int a=10; if (a == TEN) print ( The value of a is 10 ); The value of a is 10 Macro function provide faster program running speed of the program fragment. The preprocessor permits us to define more complex and more useful form of replacements it takes the following form( Argument macro substitution). #define identifier(f1,f2,f3...fn) token C source code below shows the appropriate use of macro Function. 2

#include<stdio.h> #include<conio.h> #define SQUARE(x) (x)*(x) int main() clrscr(); printf ( %d, SQUARE(10)); 100 4 What is #undef directive? If you have created a macro definition, you can use #undef to remove it. #undef directive causes a defined name to become undefined. This means the pre-processor will no longer make anymore text substitutions associated with that word. A defined macro can be undefined using following the statement: #undef identifier For example #undef VALUE would cause the definition of VALUE to be removed from the system. Un-defining macro is useful when we want to restrict the definition only to a particular part of a program. Also to change a definition, you must use #undef to undefine it and then use #define to reduce it. C Source code below shows the use of #undef pre-processor. #include<stdio.h> #define PI 3.14 #define AREA(x) (PI)*(x)*(x) int main () printf ( %f, AREA(10)); #undef PI /*PI can no longer be used*/ 314.000000 3

5 Explain #include directives. File inclusion directive causes one file to be included in another. We have already used file inclusion directive before. The pre-processor commands for file inclusion looks like this: #include File name OR #include <file name> If file name is enclosed within angle brackets, the file is searched for in the standard compiler include paths. If the file name is enclosed within double quotes, the search path is expanded to include the current source directory. If in case the file is missing or not found compiler creates error. Also it is a general standard that header files are include in angle bracket while user defined files are include in double quotes. #include <stdio.h> And #include myfile.c The above statement in C will include the file stdio.h and myfile.c in the program. Following C source code shows the use of #include pre-processor: #include <stdio.h> /*include a standard header file.*/ #include C:myfile.c /* Include user defined file.*/ int main () printf( Notice the use of #Include pre-processor ); 6 Explain predefined macros used in C language. There are five pre-defined ANSI C macros and are always available to the programmer for use as listed in following table. They cannot be undefined. Every pre-defined macro name is defined with two underscores as prefix and suffix. These macros are useful for finding the system information such as date, time, and file name and line number. A program is illustrated for testing these macros. Pre-defined macros in ANSI C: Pre-Defined Macros _DATE TIME LINE FILE STDC_ Function Displays system date in string format. Displays system time in string format. Displays line number as an integer. Displays current file name in string format. In ANSI C the value returned will be non-zero. 4

Following example illustrating use of pre-defined ANSI C Macros: int main () printf ( \nfile name and path:%s, FILE ); printf ( \ndate and Time created:%s %s, DATE, TIME ); printf ( \nline No:%d, LINE ); File Name and path: D:PRACTICE\MACRO.C Date and Time file created: Jan 26 2013 20:28:01 Line No: 6 7 List out Standard I/O Pre-defined Streams in stdio.h. There are numerous library functions available for I/O within the stdio.h file. These can be classified into three broad categories: Console I/O functions Functions to receive input from keyword and write output to VDU. File I/O functions Functions to perform I/O operations on floppy disk or hard disk. Console I/O functions Formatted scanf() printf() 8 List out pre-defined macros in ctype.h. Unformatted getch() putch() getche() getchar() putchar() gets() puts() The header file ctype.h also contains a set of macros. Following table lists all the macros available in ctype.h header file. These macros take an argument of integer type and return an integer. Macro isalpha(d) isupper(d) islower(d) isdigit(d) Returns True (!0) Value If d is a letter d is a capital letter d is a small letter d is a digit 5

isalnum(d) isxdigit(d) isspace(d) ispunct(d) isprint(d) isgraph(d) iscntrl(d) isascii(d) 9 Compare Macro and Function. d is a letter or digit d is a hexadecimal digit d is a space d is a punctuation symbol d is a printable character d is a printable, but not be a space d is a control character d is an ASCII code Macro Macros are just a text substitution tool. This increases size of your program. Best efficiency and result is achieved from a macro when they are short and frequently used. Macros are not executable. Function In C when one function makes call to another function it is done in several steps which takes time such as saving of system space, stack loading etc. This decreases size of your program. Best efficiency and result is achieved from a function when they are long and complex. Functions are executable code. 6