The Preprocessor Underlying C Language Features Copyright © 2012 by Yong-Gu Lee

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Introduction to C Programming
C Language.
IT 325 OPERATING SYSTEM C programming language. Why use C instead of Java Intermediate-level language:  Low-level features like bit operations  High-level.
Files in C Rohit Khokher.
Programming and Data Structure
Programming Languages and Paradigms The C Programming Language.
Lecture 2 Introduction to C Programming
Introduction to C Programming
Structure of a C program
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 10: Appendices.
1 Lecture 9  Arrays  Declaration  Initialization  Applications  Pointers  Declaration  The & and * operators  NULL pointer  Initialization  Readings:
Unix Process Environment. main Function A C program starts execution with a function called main. The prototype for the main function is: int main (int.
Chapter 2: Introduction to C++.
COMP1170 Midterm Preparation (March 17 th 2009) Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education.
Basic Elements of C++ Chapter 2.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
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.
Chapter 4:Functions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 1 Functions Lecture 4 by Jumail Bin.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Macros. There are three basic phases for C programming. preprocessing, compiling, and linking. C input file is first passed to a preprocessing program.
Program Looping Making Decisions Copyright © 2012 by Yong-Gu Lee
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
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.
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
(continue) © by Pearson Education, Inc. All Rights Reserved.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Learners Support Publications Classes and Objects.
Chapter 13 C Preprocessor C How to Program, 8/e ©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
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.
CPS4200 Unix Systems Programming Chapter 2. Programs, Processes and Threads A program is a prepared sequence of instructions to accomplish a defined task.
The Cn Language over view The Cn language strongly on ANSI C. So if you are familiar with ANCI it is not so tough to deal with Cn language. Basic Data.
CS 261 – Data Structures Introduction to C Programming.
CS 261 – Data Structures C Pointers Review. C is Pass By Value Pass-by-value: a copy of the argument is passed in to a parameter void foo (int a) { a.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessor Midterm Review Lecture 7 Feb 17, 2004.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
THE PREPROCESSOR
The Preprocessor Directives Introduction Preprocessing – Occurs before program compiled Inclusion of external files Definition of symbolic constants.
© 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
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
Lecturer: Nguyen Thi Hien Software Engineering Department Home page: hienngong.wordpress.com Chapter 2: Language C++
CCSA 221 Programming in C CHAPTER 11 POINTERS ALHANOUF ALAMR 1.
C++ Functions A bit of review (things we’ve covered so far)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
C++ Lesson 1.
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Introduction to C Programming
C Basics.
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).
Introduction to C Programming
Classes and Objects.
Programming in C Miscellaneous Topics.
Programming in C Miscellaneous Topics.
2. Second Step for Learning C++ Programming • Data Type • Char • Float
Homework Finish up K&R Chapters 3 & 4
C Preprocessor Seema Chandak.
Submitted By : Veenu Saini Lecturer (IT)
The C Language: Intro.
What Is? function predefined, programmer-defined
Presentation transcript:

The Preprocessor Underlying C Language Features Copyright © 2012 by Yong-Gu Lee

The Preprocessor

The #define Statement Analogous to doing a search and replace with a text editor; in this case, the preprocessor replaces all occurrences of the defined name with its associated text. It is convention that all defined names be capitalized. #define TRUE 1 #define FALSE 0 #define PI You can reference other defined values in your definitions as long as everything is defined at the time the defined name is used in the program. #define PI #define TWO_PI 2.0 * PI or #define TWO_PI 2.0 * PI #define PI

#define IS_LEAP_YEAR year % 4 == 0 && year % 100 != 0 \ || year % 400 == 0... if ( IS_LEAP_YEAR )... Normally, the preprocessor assumes that a definition is contained on a single line of the program. If a second line is needed, the last character on the line must be a backslash character. This character signals a continuation to the preprocessor and is otherwise ignored. The same holds true for more than one continuation line; each line to be contin- ued must end with a backslash character.

Macros Definitions are frequently called macros. This terminology is more often applied to definitions that take one or more arguments. This macro, called SQUARE, simply squares its argument: #define SQUARE(x) x*x

The #import Statement #import "metric.h" The double quotation marks around the header filename instruct the preprocessor to look for the specified file in one or more file directories (typically, first in the directory that contains the source file, but the actual places the preprocessor searches can be specified in Xcode). Enclosing the filename within the characters instead, as in #import causes the preprocessor to look for the include file only in the special “system” header file directory or directories; the current directory will not be searched.

Conditional Compilation The #ifdef, #endif, #else, and #ifndef Statements #ifdef IPAD # define #else # define #endif

Underlying C Language Features

Arrays int x[50]; x[0] = 1; x[49] = 1; Initializing Array Elements int integers[5] = { 0, 1, 2, 3, 4 }; char word[] = { 'H', 'e', 'l', 'l', 'o', '!' };

Multidimensional array int M[4][5] = { { 10, 5, -3, 17, 82 }, { 9, 0, 0, 8, -7 }, { 32, 20, 1, 0, 14 }, { 0, 0, 8, 7, 6 } }; Indexing elements M[slow][fast] M[1][4]

Blocks Blocks are a recent extension to the C language. They are not part of the standard ANSI C definition and were added to the language by Apple, Inc. Blocks look and act a lot like functions. The syntax takes some getting used to. You can pass arguments to blocks, just like you can to functions. You can also return a value from a block. Unlike a function, a block can be defined inside a function or method, and gets to access any variables defined outside the block that are within its scope. In general, such variables can be accessed, but their values cannot be changed.There is a special __block modifier (that’s two under- score characters that precede the word block) that enables you to modify the value of a variable from inside the block.  Blocks can be passed as arguments to functions and methods, and in Part II,“The Foundation Framework,” you’ll learn about some of the methods that expect to see a block passed as an argument. One of the advantages of blocks is that they can be dispatched by the system for execution by other processors or by other threads within your application.

Example void printMessage (void) { NSLog is fun."); } Here’s a block that accomplishes the same task: ^(void) { NSLog is fun."); } A block is identified by a leading caret ^ character. It’s followed by the parenthesized argument list that the block takes. In our case, our block takes no arguments, so we write void just as we did in the function definition.

You can assign this block to a variable called printMessage, as long as the variable is properly declared: void (^printMessage)(void) = ^(void) { NSLog is fun."); }; To the left of the equal sign we specify that printMessage is a pointer to a block that takes no arguments and returns no value. Note that the assignment statement is terminated by a semicolon. Executing a block referenced by a variable is done the same way a function is called: printMessage ();

A block can access variables within the scope in which it’s defined. The value of that variable is the value it has at the time the block is defined. You can’t by default modify the value of a variable defined outside a block. int main (int argc, const char * argv[]) { int foo = 10; void (^printFoo)(void) = ^(void) { NSLog = %i", foo); }; foo = 15; printFoo (); } return 0; } :33: Chapt13[4148:707] foo = 10

__block modifier int main (int argc, const char * argv[]) { __block int foo = 10; void (^printFoo)(void) = ^(void) { = %i", foo); foo = 20; // ** THIS LINE GENERATES A COMPILER ERROR }; foo = 15; printFoo (); NSLog = %i", foo); } return 0; } :41: Chapt13[4213:707] foo = :41: Chapt13[4213:707] foo = 20

Struct int month = 7, day = 18, year = 2011; struct _date { int month; int day; int year; }; struct _date today; typedef struct _date date

Pointers Pointers enable you to effectively represent complex data structures, change values passed as arguments to functions and methods, and more concisely and efficiently deal with arrays. int x,count = 10; int *intPtr; intPtr = &count; x = *intPtr; *intPtr = 5; indirection operator address operator

Pointers and Structures struct date { int month; int day; int year; }; struct date today, *datePtr; datePtr = &today; datePtr->month = 9; datePtr->day = 25; datePtr->year = 2011; NSLog date is %i/%i/%.2i.", datePtr->month, datePtr->day, datePtr->year % 100); x->y == (*x).y

Pointers and Arrays #import int arraySum (int* array, int n); int arraySum (int* array, int n) { int sum = 0, *ptr; int *arrayEnd = array + n; for ( ptr = array; ptr < arrayEnd; ++ptr ) sum += *ptr; return (sum); } int main (int argc, const char * argv[]) { int values[10] = { 3, 7, -9, 3, 6, -1, 7, 9, 1, -5 }; NSLog sum is %i", arraySum (values, 10)); } return 0; } declaration of function :51: Chapt13[2941:707] The sum is 21

The sizeof Operator Objective-C provides an operator called sizeof that you can use to determine the size of a data type or object. The sizeof operator returns the size of the specified item in bytes. sizeof (int) sizeof (struct data_entry)

Command-Line Arguments int main (int argc, char *argv[]) { } argc tells number of arguments + 1 each arguments can be reference by argv[0] upto argv[argc-1]