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.

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

Chapter Five Functions
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
FunctionsFunctions Systems Programming. Systems Programming: Functions 2 Functions   Simple Function Example   Function Prototype and Declaration.
 2007 Pearson Education, Inc. All rights reserved C Functions.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 17 - The Preprocessor Outline 17.1Introduction 17.2The #include Preprocessor Directive 17.3The.
 2007 Pearson Education, Inc. All rights reserved C Functions.
© 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.
 2000 Prentice Hall, Inc. All rights reserved. Functions in C Outline 1Introduction 2Program Modules in C 3Math Library Functions 4Functions 5Function.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 19 - The Preprocessor Outline 19.1 Introduction 19.2 The #include Preprocessor Directive 19.3.
Computer Science 210 Computer Organization Introduction to C.
Chapter 4 Functions and Program Structure Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Functions in C Outline 1Introduction 2Program Modules in C 3Math Library Functions 4Functions 5Function Definitions 6Function Prototypes 7Header Files.
CMSC 1041 Functions II Functions that return a value.
Engineering Computing I Chapter 4 Functions and Program Structure.
CSCI 171 Presentation 8 Built-in Functions, Preprocessor Directives, and Macros.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessing Lecture 12 April 7, 2005.
Lecture 1 cis208 January 14 rd, Compiling %> gcc helloworld.c returns a.out %> gcc –o helloworld helloworld.c returns helloworld.
Chapter 21: The Standard Library Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 21 The Standard Library.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Introduction As programmers, we don’t want to have to implement functions for every possible task we encounter. The Standard C library contains functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 5.3Math Library Functions Math library functions –perform.
UniMAP SemI-09/10EKT120: Computer Programming1 Week 5 – Functions (1)
CS 261 – Recitation 2 Fall 2013 Oregon State University School of Electrical Engineering and Computer Science.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
 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
C PREPROCESSOR. Introduction  It is a program that processes our source program before it is passed to the compiler.  Preprocessor commands (often known.
1 TOPICS TO DISCUSS : FUNCTIONS TYPES OF FUNCTIONS HEADER FILES PRESENTED BY : AVISHEK MAJUMDAR(837837) GUNJAN AGARWAL(856587) SATYAPRIYA DEY(856624)
L131 Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips rand( ) math library functions Reading Sections.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
INC 161 , CPE 100 Computer Programming
Functions, Part 2 of 2 Topics Functions That Return a Value
Chapter 13 - The Preprocessor
14. THE PREPROCESSOR.
C Short Overview Lembit Jürimägi.
CSC113: Computer Programming (Theory = 03, Lab = 01)
Deitel- C:How to Program (5ed)
Chapter 5 - Functions Outline 5.1 Introduction
Pre-processor Directives
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
Chapter 5 - Functions Outline 5.1 Introduction
Lexical Elements, Operators, and the C Cystem
Functions Declarations CSCI 230
Preprocessor C program → Modified C program → Object Code
Chapter 6 - Functions Outline 5.1 Introduction
Lexical Elements, Operators, and the C Cystem
بنام خدا زبان برنامه نویسی C (21814( Lecture 4 Chapter 5
C Preprocessor(CPP).
Preprocessor.
Functions, Part 2 of 3 Topics Functions That Return a Value
Assignment Operators Topics Increment and Decrement Operators
Assignment Operators Topics Increment and Decrement Operators
C Preprocessor Seema Chandak.
Chapter 11: The Preprocessor
Assignment Operators Topics Increment and Decrement Operators
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions that return a value
Functions in C Math Library Functions Functions Function Definitions
Preprocessor Directives and Macros Chapter 21
Presentation transcript:

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 anywhere in the program n No “;” in the end preprocessor compiler

CGS 3460 Preprocessor Directives n Macro definition l#define, #undef n File inclusion l#include n Conditional Compilation l#if, #ifdef, #ifndef, #elseif, #else

CGS 3460 Advantages n Easy to lDevelop program lRead programs lModify programs n C code more transportable between different machine architectures

CGS 3460 #define n To define constants or any macro substitution. #define E.g. #define FALSE 0 #define TRUE !FALSE n To undefined a macro. E.g. #undef FALSE lA macro must be undefined before being redefined to a different value.

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

CGS 3460 File inclusion n #include directive lInclude the contents of another file at the point where the directive appears. n Why need file inclusion lUse built-in functions E.g., printf(), rand(); lReuse code defTime.h struct date{ int day; char month[10]; int year; }; struct date{ int day; char month[10]; int year; }; struct date today; #include “defTime.h” → struct date today; Your code

CGS 3460 File inclusion formats n #include lUsed for system header files lFile contain function prototypes for library functions,, etc n #include "file" lUsed for header files of your own program looks for a file in the current directory first then system directories if not found

CGS 3460 Standard C libraries n Built-in with your compiler n Detailed information lhttp:// n stdio.h lcore input and output capabilities of C printf functionprintf scanf function

CGS 3460 Math Library Functions n Math library functions lperform common mathematical calculations E.g. exp(), pow(), sqrt(), fabs(), sin(), cos(). l#include n Format for calling functions lFunctionName( argument, …, argument ); All math functions return data type double lE.g.: printf( "%.2f", sqrt( ) ); lArguments may be constants, variables, or expressions n Compile lgcc yourfilename.c –lm –o yourfilename.exe

CGS 3460 stdlib.h n A variety of utility functions lrand function A function to generate a pseudo-random integer number Return value is in the range 0 to RAND_MAX Example: #include int i = rand(); lmemory allocation lprocess control

CGS 3460 string.h n All the string handling functions lstrcpy lstrcat lstrcmp

CGS 3460 Custom header files n Steps for creating custom header files lCreate a file with function prototypes Save as a.h file. E.g.: filename.h lLoad in other files with #include "filename.h" n Advantage lReuse functions and data structure declaration

CGS 3460 Conditional compilation n Useful when lmachine-dependencies lDebugging lsetting certain options at compile-time. n Expressions l#if expression l#ifdef expression (same as #if defined ) l#ifudef l#elif and #else l#endif

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