Programming C/C++ on Eclipe Trình bày: Ths HungNM C/C++ Training.

Slides:



Advertisements
Similar presentations
CS 11 C track: lecture 7 Last week: structs, typedef, linked lists This week: hash tables more on the C preprocessor extern const.
Advertisements

Compilation and Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)
Programming III SPRING 2015 School of Computer and Information Sciences Francisco R. Ortega, Ph.D. McKnight Fellow and GAANN Fellow LECTURE #3 Control.
Programming C/C++ on Eclipe Trình bày : Ths HungNM C/C++ Training.
CSc 352 An Introduction to the C Preprocessor Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
Structure of a C program
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 17 - The Preprocessor Outline 17.1Introduction 17.2The #include Preprocessor Directive 17.3The.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction.
CS 202 Computer Science II Lab Fall 2009 October 1.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
1 CSE 303 Lecture 13b The C preprocessor reading: Programming in C Ch. 13 slides created by Marty Stepp
An Introduction to C Programming Geb Thomas. Learning Objectives Learn how to write and compile a C program Learn what C libraries are Understand the.
The Preprocessor #include #define N 10 C program Preprocessor Modified C program Preprocessor Object codes.
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.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Pre-Processor Commands Gabriel Hugh Elkaim Spring 2013.
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)
Macros. There are three basic phases for C programming. preprocessing, compiling, and linking. C input file is first passed to a preprocessing program.
Separate Compilation. A key concept in programming  Two kinds of languages, compilation (C, Pascal, …) and interpretation (Lisp, …, Matlab, Phython,
The Structure of a C++ Program. Outline 1. Separate Compilation 2. The # Preprocessor 3. Declarations and Definitions 4. Organizing Decls & Defs into.
CS 450 MPX P ROJECT A Quick C Review. P OINTERS AND ADDRESSES IN C Check Manual I2 from Dr. Mooney’s website for a complete review of C topics you will.
Chapter 13 Programming in the Large Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. TA: 鄭筱親 陳昱豪.
Introduction to Exception Handling and Defensive Programming.
Chapter 13 C Preprocessor C How to Program, 8/e ©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved.
C Hints and Tips The preprocessor and other fun toys.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
CPS120: Introduction to Computer Science Functions.
Engineering Computing I Chapter 4 Functions and Program Structure.
CPS120: Introduction to Computer Science Lecture 14 Functions.
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.
1 Remember: Examination is a chance not ability. By ILTAF MEHDI, IT LECTURER, MIHE, KATR-E-PARWAN BRANCH, KABUL.
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.
Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Chapter 7: Preprocessing.
C Programming Day 3. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Storage Class Specifiers Every variable in a C.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessor Midterm Review Lecture 7 Feb 17, 2004.
THE PREPROCESSOR
The Preprocessor Directives Introduction Preprocessing – Occurs before program compiled Inclusion of external files Definition of symbolic constants.
Chapter 14: The Preprocessor Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 14 The Preprocessor.
 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
Adv. UNIX:pre/111 Advanced UNIX v Objectives of these slides: –look at the features of the C preprocessor Special Topics in Comp. Eng.
C PREPROCESSOR. Introduction  It is a program that processes our source program before it is passed to the compiler.  Preprocessor commands (often known.
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Program Files and Preprocessing Directives Program files and.
2. C FUNDAMENTALS. Example: Printing a Message /* Illustrates comments, strings, and the printf function */ #include int main(void) { printf("To C, or.
Pre-Compiler Directives for TTCN-3 Ina Schieferdecker in response to CR5089 and 5090.
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
Unit 10 Code Reuse. Key Concepts Abstraction Header files Implementation files Storage classes Exit function Conditional compilation Command-line arguments.
13 C Preprocessor.
CISC105 – General Computer Science
INC 161 , CPE 100 Computer Programming
Chapter 13 - The Preprocessor
14. THE PREPROCESSOR.
Pre-processor Directives
Preprocessor C program → Modified C program → Object Code
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).
Preprocessor.
Programming in C Miscellaneous Topics.
CSc 352 An Introduction to the C Preprocessor
Programming in C Miscellaneous Topics.
C Preprocessor Seema Chandak.
Macro A fragment of code which has been given a name.
CSE 341 Lecture 22 Macros; extending Scheme's syntax
Corresponds with Chapter 5
Preprocessor Directives and Macros Chapter 21
Presentation transcript:

Programming C/C++ on Eclipe Trình bày: Ths HungNM C/C++ Training

Training C/C++ EcoSoftware 2 The Preprocessor o Introduction preprocessor. o Preprocessing o Logical Preprocessor Directives o Standard Preprocessing Macros o Error Generation o Using the assert() Macro

Training C/C++ EcoSoftware 3 Introduction preprocessor o The preprocessor is powerful tool. o It also can be a source of hard-to-find bugs. o It can be easily be misused to create program that are almost impossible to understand. o The preprocessor is controlled by processing directive that begin a # character. Modified C program C Program Object code

Training C/C++ EcoSoftware 4 Preprocessing o Including Header Files  A header file is any external file whose contents  It use of the #include preprocessor directive.  Syntax : #include o External Variables and Functions  A program that’s made up of several source files  want to use a global variable that’s defined in another file.  Declaring the variable as external to the current file using the extern keyword.  Syntax : extern datatype namevariable; Example : extern int number;

Training C/C++ EcoSoftware 5 Preprocessing o Macro definition.  The #define directive defines a macro.  The #undefine directive remove a macro definition.  Syntax : #define identifer replacement-list  Example : #define PI #define Black White  Advance macro definition. It make program easier to read. It make program easier to modify It help avoid inconsistencies and topographical error. –Example : Numerical constant like : , but some time

Training C/C++ EcoSoftware 6 Preprocessing o Macro definition.  Advance macro definition Making minor changes to syntax of C program. –Example : #define BEGIN { –#define END } Renaming types. –Exampe : #define BOOL int Controlling conditional compilation. –Example : #define DEBUG

Training C/C++ EcoSoftware 7 Preprocessing o Macros That Look Like Functions  Allows parameters to be specified, which may themselves be replaced by argument values,  Syntax : #define identifer(x1,x2,..,xn) replacement list  Example : #define max(x, y) x>y ? x : y Call : result = myval>99 ? myval : 99; It mean : result = max(myval, 99);

Training C/C++ EcoSoftware 8 Preprocessing o Preprocessor Directives on Multiple Lines  using the statement continuation character, \.  Example : #define min(x, y) \ ((x)<(y) ? (x) : (y)) o Strings As Macro Arguments  Example: #define MYSTR "This string"  Call : printf("%s", MYSTR);

Training C/C++ EcoSoftware 9 Preprocessing o Joining Two Results of a Macro Expansion  you may wish to generate two results in a macro and join them together  Using two characters ## serves to separate the parameters.  Syntax : #define join(a, b) a##b  Example : strlen(join(var, 123)); It will be result : strlen(var123);

Training C/C++ EcoSoftware 10 Logical Preprocessor Directives o Conditional Compilation  Syntax : #if defined identifier #endif  Or #if !defined identifier #endif  Or #if !defined block1 #define block1 /* Block of code you do not */ /* want to be repeated. */ #endif

Training C/C++ EcoSoftware 11 Logical Preprocessor Directives o Directives Testing for Specific Values  Using #if directive to test the value of a constant expression.  Syntax : #if constant_expression #endif  Example : #if CPU == Pentium4 printf("\nPerformance should be good." ); #endif

Training C/C++ EcoSoftware 12 Logical Preprocessor Directives o Multiple-Choice Selections  Using #if, #elseif, #else derective for excecute statements.  Syntax : #if constant_expression #elseif constant_expression #else #endif  Example : #if CPU == Pentium4 printf("\nPerformance should be good." ); #else printf("\nPerformance may not be so good." ); #endif

Training C/C++ EcoSoftware 13 Standard Preprocessing Macros o Using __DATE__ macro provides a string representation of the date. o Using __TIME__, provides a string containing the value of the time. o Using __FILE__, A string literal containing the name of the file being compiled. o __LINE__ A decimal constant containing the current source line number. o __STDC_VERSION__ : This macro expands to the C Standard's version number. o Example :  printf("\nProgram last compiled at %s on %s", __TIME__, __DATE__ );

Error Generation o A preprocessor error directive causes the preprocessor to generate an error message and causes the compilation to fail. o Syntax :  # error token-sequence o Example : #define BUFFER_SIZE 255 #if BUFFER_SIZE < 256 #error "BUFFER_SIZE is too small." #endif generates the error message: BUFFER_SIZE is too small. Training C/C++ EcoSoftware 14

Using the assert() Macro o The assert() macro is defined in the standard library header file. o Using #ndefine derectives for Switch on or off assertions.  Syntax: #undef NDEBUG #include  Example: Training C/C++ EcoSoftware 15 #undef NDEBUG #include int main(void) { int y = 5; for(int x = 0 ; x < 20 ; x++) { printf("\nx = %d y = %d", x, y); assert(x<y); } return 0; }

Training C/C++ EcoSoftware 16 Thank You End