The Preprocessor Chapter 8 in ABC. #define PI 3.14159 #define C2 99792.458 /* speed of light */ #define EOF (-1) #define MAXINT 2147483647 #define ITERS.

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

C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
Principles of Programming Fundamental of C Programming Language and Basic Input/Output Function 1.
1 Lecture 7  Fundamental data types in C  Data type conversion:  Automatic  Casting  Character processing  getchar()  putchar()  Macros on ctype.h.
Programming Character I/O. COMP102 Prog. Fundamentals I: Character I/O/ Slide 2 More on char Type l Constant Declaration: const char star = '*'; l Variable.
Structure of a C program
1 Review of Class on Oct Outline of Chapter 4  How to write a function?  Function Prototypes  Function Invocation  Function Definition  The.
Character I/O. COMP104 Character I/O Slide 2 Data Type: char * Constant declaration const char star = '*'; * Variable declaration char resp; * Variable.
Characters. COMP104 Lecture 21 / Slide 2 Data Type: char * Constant declaration const char star = '*'; * Variable declaration char resp; * Variable assignment.
. Compilation / Pointers Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)
Strings A special kind of array is an array of characters ending in the null character \0 called string arrays A string is declared as an array of characters.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 17 - The Preprocessor Outline 17.1Introduction 17.2The #include Preprocessor Directive 17.3The.
1 Character Processing How characters can be treated as small integers? How characters are stored and manipulated in a machine? How use is made of certain.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction.
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.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
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.
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)
CHAPTER 8 CHARACTER AND STRINGS
BBS514 Structured Programming (Yapısal Programlama)1 Character Processing, Strings and Pointers,
STRING Dong-Chul Kim BioMeCIS UTA 10/7/
The char Data Type A char is a one byte integer type typically used for storing characters. Example: char oneLetter = ’D’; We enclose the character in.
12 주 강의 The preprocessor. The use of #include #include #include “ filename ” ::: current directory  system-dependent places #include ::: system dependent.
#include int main(void) { printf("Hello, world!\n"); return 0; } entry point called on program start only one main( ) in any program # for preprocessor.
Chapter 13 C Preprocessor C How to Program, 8/e ©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved.
1 FUNCTIONS - I Chapter 5. 2 What are functions ? Large programs can be modularized into sub programs which are smaller, accomplish a specific task and.
CSCI 171 Presentation 8 Built-in Functions, Preprocessor Directives, and Macros.
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.
CSC 270 – Survey of Programming Languages
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessor Midterm Review Lecture 7 Feb 17, 2004.
More Pointers and Arrays Kernighan/Ritchie: Kelley/Pohl: Chapter 5 Chapter 6.
THE PREPROCESSOR
The Preprocessor Directives Introduction Preprocessing – Occurs before program compiled Inclusion of external files Definition of symbolic constants.
Lecture 18 CIS 208 Wednesday, March 30, Test2 April 15 th : Happy Tax day. Review: Wednesday, the 13 th.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
Characters and Strings
1 Lexical Elements, Operators, and the C System. 2 Outline Characters and Lexical Elements Syntax Rules Comments Keywords Identifiers Constants String.
2. C FUNDAMENTALS. Example: Printing a Message /* Illustrates comments, strings, and the printf function */ #include int main(void) { printf("To C, or.
CSE 251 Dr. Charles B. Owen Programming in C1 Strings and File I/O.
CSE 251 Dr. Charles B. Owen Programming in C1 Strings and File I/O.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
C Language Elements Preprocessor Directives # (sign for preprocessor directive commands) #include Standard header file (.h) Library.
1 Building a program in C: Preprocessor, Compilation and Linkage.
13 C Preprocessor.
Character Processing How characters can be treated as small integers?
CS31 Discussion 1E Jie(Jay) Wang Week5 Oct.27.
Chapter 13 - The Preprocessor
Unit-4, Chapter-2 Managing Input and Output Operations
Functions Separate Compilation
Chapter 8 - Characters and Strings
Introduction to Programming
Lexical Elements, Operators, and the C Cystem
פרטים נוספים בסילבוס של הקורס
Lexical Elements, Operators, and the C Cystem
Character Processing How characters can be treated as small integers?
Lab guide # 12 Review C Preprocessor And Q&A.
Std Library of C++.
Chapter 11: The Preprocessor
ENERGY 211 / CME 211 Lecture 9 October 10, 2008.
The Preprocessor.
C Characters and Strings
Week 2 - Friday CS222.
Preprocessor Directives and Macros Chapter 21
Presentation transcript:

The Preprocessor Chapter 8 in ABC

#define PI #define C /* speed of light */ #define EOF (-1) #define MAXINT #define ITERS 50 /* number of iterations */ #define SIZE 250 /* array size */ #define EPS 1.0e-9 /* a numerical limit */ Software 1, TAU - 8.1

#define EQ == #define do /* blank */ while (i EQ 1) do { while (i == i) {..... Software 1, TAU - 8.2

#define SQ(x) ((x) * (x)) SQ(7 + w) expands to ((7 + w) * (7 + w)) SQ(SQ(*p)) expands to ((((*p) * (*p))) * (((*p) * (*p)))) Software 1, TAU - 8.2

#define SQ(x) x * x SQ(a + b) expands to a + b * a + b #define SQ(x) (x) * (x) 4 / SQ (2) expands to 4 / (2) * (2) #define SQ (x) ((x) * (x)) SQ(7) expands to (x) ((x) * (x)) (7) Software 1, TAU - 8.3

#define SQ(x) ((x) * (x)); /* error */ because: if (x == 2) x = SQ(y); else ++x; Software 1, TAU - 8.3

#define min(x, y) (((x) < (y)) ? (x) : (y)) m = min(u, v) expands to m = (((u) < (v)) ? (u) : (v)) #define min4(a, b, c, d) min(min(a,b), min(c,d)) #define SQ(x) ((x) * (x)) #define CUBE(x) (SQ(x) * (x)) #define F_POW(x) sqrt(sqrt(CUBE(x)))) /* fractional power: 3/4 */ #undef identifier cc -E file.c Software 1, TAU - 8.4

/* cmp.c */ int cmp(const void *vp, const void *vq) { const double *p = vp; const double *q = vq; double diff = *p - *q; return ((diff >= 0.0) ? ((diff > 0.0) ? -1 : 0) : +1); } /* fill.c */ void fill_array(double *a, int n) { int i; srand(time(NULL)); /* seed rand() */ for (i = 0; i < n; ++i) a[i] = (rand() % 1001) / 10.0; } Software 1, TAU - 8.5

/* prn.c */ void prn_array(when val, double *a, int n) { int i; printf("%s\n%s%s\n", "---", ((val == before) ? "Before " : "After "), "sorting:"); for (i = 0; i < n; ++i) { if (i % 6 == 0) putchar('\n'); printf("%11.1f", a[i]); } putchar('\n'); } Software 1, TAU - 8.6

/* main.c */ #include #define N 11 /* size of the array */ enum when {before, after}; typedef enum when when; int cmp(const void *vp, const void *vq); /* compar. fct */ void fill_array(double *a, int n); void prn_array(when val, double *a, int n); int main(void) { double a[N]; fill_array(a, N); prn_array(before, a, N); qsort(a, N, sizeof(double), cmp); prn_array(after, a, N); return 0; } Software 1, TAU - 8.7

Before sorting: After sorting: Software 1, TAU - 8.8

/* compare.c */ #include "sort.h" int compare_fractional_part(const void *vp, const void *vq) { const float *p = vp, *q = vq; float x; x = fractional_part(*p) - fractional_part(*q); return ((x < 0.0) ? -1 : (x == 0.0) ? 0 : +1); } int lexico(const void *vp, const void *vq) { const char *p = vp, *q = vq; return (*p - *q); } Software 1, TAU - 8.9

/* main.c */ #include "sort.h" int main(void) { char a[M]; float b[N]; int i; srand(time(NULL)); FILL(a, M, "char"); PRINT(a, M, "%-2c"); qsort(a, M, sizeof(char), lexico); PRINT(a, M, "%-2c"); printf("---\n"); FILL(b, N, "float"); PRINT(b, N, "%-6.1f"); qsort(b, N, sizeof(float), compare_fractional_part); PRINT(b, N, "%-6.1f"); return 0; } q m z r h l a j o e t b k w l t z t v i e m h p f y b p s w a j a a b b e e f h h i j j k l l m m o p p q r s t t t v w w y z z Software 1, TAU

/* sort.h */ #include #define M 32 /* size of a[] */ #define N 11 /* size of b[] */ #define fractional_part(x) (x - (int) x) #define random_char() (rand() % 26 + 'a') #define random_float() (rand() % 100 / 10.0) #define FILL(array, sz, type) \ if (strcmp(type, "char") == 0) \ for (i = 0; i < sz; ++i) \ array[i] = random_char(); \ else \ for (i = 0; i < sz; ++i) \ array[i] = random_float() #define PRINT(array, sz, cntrl_string) \ for (i = 0; i < sz; ++i) \ printf(cntrl_string, array[i]); \ putchar('\n') int compare_fractional_part(const void *, const void *); int lexico(const void *, const void *); Software 1, TAU

Macros in stdio.h : #define getchar() getc(stdin) #define putchar(c) putc((c),stdout)

Macros ctype.h (takes int as argument, return int): isalpha(c) c is a letter isupper(c) c is an uppercase letter islower(c) c is a lowercase letter isdigit(c) c is a digit isalnum(c) c is a letter or a digit isxdigit(c) c is a hexadecimal digit isspace(c) c is a white space character ispunct(c) c is a punctuation character isprint(c) c is a printable character isgraph(c) c is printable, but not a space iscntrl(c) c is a control character isascii(c) c is an ASCII code

Also in ctype.h: Functions in ctype.h (take int, return int): toupper(c)corresponding uppercase value for c tolower(c)corresponding lowercase value for c Macro in ctype.h (takes int, returns int) : toascii(c)corresponding ASCII value

#if constant_integral_expression #ifdef indentifier #ifndef indentifier #endif #if defined(HP9000) || defined(SUN4) && !defined(VAX) /* machine_dependent code */ #endif #define DEBUG 1 #if DEBUG printf("debug: a = %d\n", a); #endif #include "everything.h" #undef PIE #define PIE "I like apple." Software 1, TAU

statement #if 0 more statements #endif and still more statements #elif constant_interal_expression #else #endif Software 1, TAU

Fives predefined macros (can’t be undefined) : __DATE__A string containing the current date __FILE__A string containing the file name __LINE__An integer representing the current line number __STDC__If the implementation follows ANSI C then the value is a nonzero integer __TIME__A string containing the current time

#define message_for(a, b) \ printf(#a " and " #b": We love you!\n") int main(void) { message_for(Carole, Debra); return 0; } After preprocessor: int main(void) { printf("Carole" "and " Debra" ": We love you\n"); return 0; } Software 1, TAU

#define X(i)x ## i X(1) = X(2) = X(3); is expanded to x1 = x2 = x3; #if A_SIZE < B_SIZE #error "Incompatible sizes" #endif Software 1, TAU

#include Void f(char *p, int n) { … assert (p != NULL); assert (n > 0 && n < 7); … }

/* assert.h */ #include #include /* for abort() */ #if defined(NDEBUG) #define assert(ignore) ((void) 0) /* ignore it */ #else #define assert(expr) \ if (!(expr)) { \ printf("\n%s%s\n%s%s\n%s%d\n\n", \ "Assertion failed: ", #expr, \ "in file ", __FILE__, \ "at line ", __LINE__); \ abort(); \ } #endif Software 1, TAU